Jul 26, 2008

C Programming: Arrays - A Clear Description of Arrays

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

free web hosting

C Programming: Arrays - A Clear Description of Arrays

jayron
C programming provides a capability that enables a user to design a set of similar data types, called Arrays.

For understanding the arrays properly, let us consider the following program
CODE

main()
{
int x;
j=5
j=10;
printf("\nj= %d",x);
}

No doubt, this program will print the value of j as 10. This is because when a value 10 is assigned to j, the earlier value of j, i.e. 5, is lost. Thus, ordinary variables are capable of holding only one value at a time. However, there are situations in which we would want to store more than one value at a time in a single variable.

For example, suppose we wish to arrange the percentage marks obtained by 100 students in ascending order. In such case we have two options to store these marks in memory.

1) Construct 100 variables to store percentage marks obtained by 10 differ students, i.e. each variable containing one student;s marks.

2)Construct one variable (called Array or subscripted variable) capable of storing or holding all the hundred values.

Obviously, the second alternative is better. A simple reason for this is, it would be much easier to handle one variable than handling 100 different variables. Moreover, there are certain logics that cannot be dealt without the use of an array. An array is a collective name given given to a group of 'similar quantities'. These similar quantities could be percentage marks of 100 students, or salaries of 300 employees, or age of 500 employess. What is important is that the quantity must be 'similar'. Each member in the group is referred to by its position in the group. For example, assume the following group of numbers which represent percentage marks obtained by five students.
CODE

perc = {48, 88, 34, 23, 96};


If we want to refer to the second number of the group, the usual notation used is perc(2) similarly, the fourth number of the group is referred as perc(4). However, in C, the fourth number is referred as perc[3]. This is because in C the counting of elements begin with 0 and not with 1. Thus, in this example perc[3] refers to 23 and perc[4] refers to 96. In general, the notation would be perc[i], where, i can take a value 0,1,2,3 or 4, depending on the position of the element being regerred. Here perc is the subscripted variable (array), whereas 1 is its subscirpt.

Thus, an array is a collection of similar elements. These similar elements could be all ints, or all floats, or all chars, etc. Usually, the array of characters is a called a string, whereas an array of ints or floats is simply called an array. Remember that all elements of any given array must be the same type, i.e. we cannot have an array of 10 numbers, of which 5 are ints and 5 are floats.

 

 

 


Reply

jayron
Array Declaration

To begin with, like other variables an array needs to be declared so that the compiler will know what kind of an array and how large an array we want. An example of declaration is:
CODE

int marks[30];

Here int specifies the type of variable, just as it does with ordinary variables and the word 'mark' specifies the name of the variable. the [30] however is something to stress on. The number 30 tells how many elements of the type int will be in our array. This number is often called the 'dimension' of the array. The brackets ( [] ) tells the compiler that we are dealing with an array.

Accessing Elements of an Array

Once an array is declared, let us see how individual elements in the array can be referred. This is done with subscript, the number in the brackets following the array name. This number specifies the element’s position in the array. All the array elements are numbered, starting with 0. Thus marks[2] is not the second element of the array, but the third. In our program we are using variable I as a subscript to refer to various elements of the array. This variable can take different values and hence can refer to the different elements in the array in turn.. This ability to use variables as subscripts is what makes arrays so useful.

Entering Data into an Array
Here is the section of code that places data into an array:

CODE

for(i=0;i<=29;i++)
  {
     printf(“\nEnter Marks”);
     scanf(“%d”,&marks[i]);
  }


The for loop cause the process of asking for and receiving a student’s marks from the user to be repeated 30 times. The first time through the loop, I has a value 0, so the scanf() statement will cause the value type to be stored in the array element marks[0], the first element of the array. The process will be repeated until I becomes 29. This is the last time though the loop, which is a good thing, because there is no array element like marks[30].

In the scanf() statement, we have used the “address of” operator (&) on the element marks[i] of the array. In so doing, we are passing the address of this particular array element to the scanf() function, rather than its value, which is what scanf() requires.

 

 

 


Reply

gdinod
Clear description of arrays for beginners! Whats your reference? book? site?

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : programming arrays description arrays

  1. How Many of You Use the C Programming language? - (23)
    Just wanted to know how many of you are currently using C language for programming. If you can tell
    what sort of programs you do, it would be useful. ...
  2. Life In Programming? - wheres this headed? (9)
    Hey guys im 16 and really like programming (although im fairly new at it) ive already taken a year
    of basic html and what not. This year im taking a class in c++ and VB next year i hope to take java
    and php. However next year will be my senior year and i will have to start applying for colleges
    and im trying to convince my parents that there ARE jobs in computers its hard for them to do this
    they like to stay locked up in there own little world **cough cough** stone age **cough** so what i
    was wondering is what jobs are actually out there for this kinda stuff what are ...
  3. A Question About C++ Programming Under Linux - (7)
    Hello guys! i prepare to develop under linux using c++, but i don't know what tools i should
    use...some one tell me to use vim, but i think vim is so hard to use!can u recommend me some
    ide??? thank u very much...
  4. Good Books On C++ Game Programming - I need some books! (2)
    I am looking for some good books on game programming in c++ if any body out there has any
    suggestions feel free to post them. Oh by the way should i start out with an easier language like
    BASIC or should I just work on c++. Ineed some help people please reply fast. Tahnx
    David H /biggrin.gif' border='0' style='vertical-align:middle' alt='biggrin.gif' /> ...
  5. Detailed C Beginner Tut - basis for learning any application programming language (2)
    QUOTE The best way to learn programming is to dive right in and start writing real programs.
    This way, concepts which would otherwise seem abstract make sense, and the positive feedback you get
    from getting even a small program to work gives you a great incentive to improve it or write the
    next one. Diving in with ``real'' programs right away has another advantage, if
    only pragmatic: if you're using a conventional compiler, you can't run a fragment of a
    program and see what it does; nothing will run until you have a complete (if tiny or trivi...
  6. D Programming Language - (9)
    hi friends, surprised... was that a type???? It is not a type... I know this is a C/C++
    programming language forum.. Just wanted to share something I came across today... A new
    programming language based on C and C++ is being developed.. It would have all the power of C/C++,
    in other words as the spec says "retains the ability to write high performance code and interface
    directly with the operating system API's and with hardware." The specification of the language
    and alpha phase compilers are available at www.digitalmars.com I just browsed throught the spec a...
  7. Why Must We Learn Object Oriented Programming - help me to understand the benefit of OOP (4)
  8. Discussion On Dynamic Programming - (3)
    Dynamic Programming is one of the powerful programming approach now a day. DP applicable when sub
    problems share sub sub problems. (No independent sub problems). Solve each sub problem once, save
    the answer, and when needed use the previously computed result. Like for Fibonacci series. To get
    f(n+1) we need addition of f(n), f(n-1) and for f(n) we need f(n-1), f(n-2). so to get f(n+1) we
    need f(n) and f(n-1) and again for f(n) we need f(n-1) and f(n-2). In this approach we are
    calculating same thing more then once . That is a very bad idea. But what if we store the data...
  9. Vesa Programming Viewer In D O S - Graphics in C/C++ (3)
    Hello friends, I wanna make an image viewer in DOS...can any one help me how to deal with high
    resolution screens in DOS uning mouse using VESA interrupts. Please help.. thanks acumentech...
  10. Alright, I'm Taking Computer Programming As A Class In School. - (6)
    Is there anything I should personally be worried about or anything I really need to know? I'm
    pretty much good with computers and such and pretty much spend my whole life on them, lol....
  11. C Programming Video Tutorials - This is for all the memebers out there looking or need some mroe help (2)
    Hello per this is for the memeber that want to start programming in " C "... C is a
    very powerful Programming Lang In fact mayb to powerful.. Well there is 138 Videos in the
    TUT.. I Have uploaded the it so per .rar is per chapter /smile.gif"
    style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> P@ssword for ALL .rar files
    is : sid3arm chapter #1 = Introduction to C ** Download link ** chapter #2 = A
    basic C program ** Download link ** chapter #3 = Basic Elements of a C Program **
    Download link ...
  12. C Or C++ Easy Programming Generator - You need a program? (6)
    Hi i just had a stupid question on how to program is C or C++. I would like to know if there is any
    program like Photoshop to create C codes or how to put them together. If someone could show me it
    would be great. I appreciate it as i love computers and want to be like a wiz at it and also
    everything related to it. I know that this is crazy and i have heard that from people they tell me
    you're a GFX person stay there but i want to explore. Thanks if helped....
  13. Beginners Guide To C/c++ Programming? - (2)
    ok so I was wondering if anyone knows where I can find a good, c/c++ dummy friendly guide? my main
    reason for want to learn it so that I can make .cab files that do registry edits for ppc. I know
    this should be easy to do, Any help would be greatly appreciated. Thx in advanced, Mike...
  14. Dos Game Programming In C For Beginners - (1)
    There are a number of tutorials available for the intermediate game programmer, but there are very
    few good tutorials for beginners who have never drawn a pixel on the screen. A quick search on the
    net reveals hundreds of sites devoted to 3D, polygons, texture- mapping and other advance topics,
    but the beginner has no where to get started. This tutorial is for C programmers who want to get an
    introduction to game programming. find this tutorial at Dos Game Programming in C for Beginners
    ...
  15. Dos Programming, Undocumented Dos, And Dos Secrets - (3)
    This web site is devoted to DOS programming for those of us who still enjoy programming in DOS some
    of its containt are Disk/Files - Programming the disk and/or file info. DOS - DOS programs, DOS
    prompt, DOS compilers, etc. Encryption/Compression - Encryption and Compression. Games - Programming
    Games. Hardware/System programming - Memory, BIOS, CMOS, and other Hardware/System items.
    Input/Output Devices - Mice, Joystick, Keyboard, Modem, Printer, etc.. Misc - Item that don't
    fit in any/all the other subjects. Sound - Sound programming Video/Graphics - Graphics progr...
  16. C/c++ Programming Experince - How long does it take before programming useful programs? (3)
    I've been teaching my self C++, slowly but surely. Everything I try to program is just mediocre,
    and much simplier to use something already made. How long does it take before I can start
    programming useful decent programs?...
  17. 256-color Vga Programming In C Site - (0)
    what some say is the best VGA programming site on the web VGA Basics Setting the video mode,
    plotting a pixel, and mode 0x13 memory. Primitive Shapes & Lines Drawing lines, polygons,
    rectangles, and circles. Also, Bresenham's algorithm, fixed-point math and pre-computing tables.
    Bitmaps & Palette Manipulation The BMP file format, drawing bitmaps, and palette manipulation. Mouse
    Support & Animation Animation, mouse motion, and mouse button detection. Double Buffering, Page
    Flipping, & Unchained Mode Double buffering, page flipping, structure of unchained mode,...
  18. Win32: Dialog Box And Accelerator - Win32 API programming (0)
    ok my problem is how do i make or assign a keyboard accelerator with modal dialog boxes since the
    message loop is inside the function call of DailogBox() functions, unlike the modeless dialog box in
    which you are the one who will create the message loop for the dialog box... is it even possible?...
  19. Programming A Malloc - (1)
    A little background about what I am doing is this: I have an array which holds a linked list
    containing the memory that is currently taken from the system. Each index I have like 16 size
    memory or less can be stored only in index 0, index 1 is for memory inbetween 16-32, index 2 is for
    memory inbetween 32-64, etc all the way up to 2048 which is the max allocatable memory. Now the way
    my malloc is to work is that it first will run through my list to see if any memory is available
    that is greater than or equal to the size I need, if there is any at all it will return it...
  20. Where To Start Learning C++ Programming Language - Resources for C++ (4)
    I'm planning on learning c++ but I'm having some problems. First, I need a compiler.
    I've downloaded Digital Mars (www.digitalmars.com) but how the heck do I compile something????
    There's this other free compiler that loooks great but seems hard to install. Does anyone know a
    good freeware compiler??? Also does anyone know what some good tutorial resources are? I've
    found a few sites, but does anyone know a really good one? Or maybe a good book? But my main
    problem is with the compiler. Can't get anything done without one......
  21. Never Ending Books For C++ Programming - C++ Programming Books (3)
    /smile.gif' border='0' style='vertical-align:middle' alt='smile.gif' /> Hello Friends As I m
    doing MCA so I have so many ebooks for u. Anybody that wants C++ Programming Knowledge ,can get from
    these books,it includes all basic and expert features programming.So here is the collection Only For
    Trap17 Users.If u like this Post Please reply. Thanx ...................
  22. Vesa Programming Viewer In C - Graphics in C (2)
    Hello friends, I wanna make one image viewer program in C please help me how to deal with high
    resolution video using VESA programming. Thanks acumentech...
  23. Game Programming - (17)
    I'm looking into going into game programming and I want to know if there is another programming
    language I should learn to go along with C++? Or should I just learn C++(Master Blaster of C++,
    Master Shake of C++, Lord and Master of C++, Jedi Master of C++) and then learn(more)/master it in
    college? Thanks, Kvarner express...
  24. Programming Help - (7)
    Hi there, I have C++ programming in my college syllabus this semister.Once this paper+ Oral exams
    over i think i may loose touch with C++. Can you suggest me any good forum where i can keep in touch
    with C++.Any project where i can keep my creative juices flowing? PLs can you help me with this....
  25. Where Can I Learn Linux Programming? - any good resources for linux programming (2)
    Where can i learn linux system programming? Topic titles and descriptions are important. Try and
    make them as descriptive as possible. Linux Programming, Linux is not very descriptive. Renamed.
    ...
  26. C Most Popular Programming Language - Look at title :) (3)
    http://www.tiobe.com/tiobe_index/tekst.htm C is most popular programming language altought market
    share is ~2 %. The most projects is worked in other programming language like .NET and C++. I think
    C is too old. What do you think?...
  27. 3d Arrays [resolved] - (0)
    I'm making an oct tree to divide up a colour space. Each tree has 3 upperbounds, and 3
    lowerbounds, 1 pair for each axis. I store it's children in a 3D array. 2 by 2 by 2.
    m_pChildren is of type pointer to a pointer, and is initilized like this: Code: void
    COctTree::CreateChildren(void) { m_pChildren = new COctTree* ; for(int i=0;i {
    for(int j=0;j { for(int k=0;k { m_pChildren = new COctTree(); //Setup
    boundaries m_pChildren ->m_pLowBounds = m_pLowBounds + i * ((m_pHighBounds -
    m_pLowBounds )>>1); ...
  28. Introduce Some Website About C/c++programming - enjoy yourself (1)
    http://www.cstudyhome.com/ http://download.pchome.net/development/c/
    http://bbs.linux.org.cn/cgi-bin/forums.cgi?forum=5 ...
  29. Simple C++ Programs - programming C++ (13)
    what simple programs would a beginner should code in order to get some understanding the c++ along
    with leaning as well...
  30. C Programming Help - (1)
    Hey, does anyone know of any good direct sites that would help me in some scripting of c programming
    or maybe even a tutrorial that would help with some advanced scripting. Thanks....



Looking for c, programming, arrays, clear, description, arrays

Searching Video's for c, programming, arrays, clear, description, arrays
advertisement



C Programming: Arrays - A Clear Description of Arrays



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE