Nov 20, 2009

C Programming: Arrays - A Clear Description of Arrays

free web hosting
Open Discussion > MODERATED AREA > Computers > Programming Languages > C/C++ Programming

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.

 

 

 


Comment/Reply (w/o sign-up)

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.

 

 

 


Comment/Reply (w/o sign-up)

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

Comment/Reply (w/o sign-up)

(G)Rodolfo
A better terminology
C Programming: Arrays

Your post about arrays considers the name of the array as a variable. Further, you indicate that variables can only hold a single value.  I believe these views are inconvenient in the computer science world.  First, multivalued variables exists all the time, both, in mathematics and in systems programming.  In fact, we use one all the time known as $PATH.  The $PATH environment variable has a list of values separated by a field demarcation character.  It is called a multivalued variable.  In complex variables, the square root of  any number is a multivalued variable.

A better way to view arrays is  to consider them as a special case of a "grouping" name.  The C/C++ language offer other examples of "grouping names" such as, the names of "enum" declarations and struct/class declarations. 

In the case of C/C++, the name of the array stands for a group of objects (not just variables).  Furthermore, in C/C++ the array name refers to a single object/variable: a pointer that contains the memory address of the first object/variable of the array.  Consequently, unlike the arrays of traditional computer languages, the arrays in C/C++ are elaborated constructs instead of being a single primitive of the computer language. Many important implications result from this point. 

First, an array implemented by pointers need not be contiguously allocated in memory; thereby, providing extraordinary flexibility to memory management strategies. 

Second, there are no bound checks--no "subscript out-of-range errors".  Consequently, the programmer is responsible for accessing the data correctly and doing its own range checks.  This flexibility empowers the programmer to gain execution speed by avoiding redundant range checks. Consider the case of five arrays of the same size that are accessed using the same set of subscripts.  In the traditional languages, the compiler emits code into the executable to perform five range checks.  In C/C++ using pointers, the programmer need only do one instead of five  range check of the subscripts; thereby, substantially increasing the speed of execution.

Third, accesses may be carried out bypassing the storage map equations, which are very slow in multidimensional situations.

Lastly, the array may be broken down as it is not atomic.  Consequently, more efficient access methods may be implemented by re-casting the pointer.

 In summary, it is best to consider an array name as the name of a single block of objects/variables, where each object/variable may still be accessed individually.

-reply by Rodolfo

 


Comment/Reply (w/o sign-up)

(G)amir
programming
C Programming: Arrays

You are required to Write a program that takes character values in two different arrays as input From user. After getting input, make a third array and merge both these array In third array. Output array must be sorted in ascending order.

   Detailed Description:

1.  Declare three character type arrays.

2.  Use two arrays to take input from user.

o       Take input as char type.

o       Take 10 values in each array.

3.  Merge both these input arrays.

o       If an input character is in both arrays, then It should appear once in resulted array.  

4.  Store result in third array.

5.  Sort resulted array in ascending order.

o       Ascending order means string values starting From ‘a’ will come first, and then starting from ‘b’ and so on.

6.  Display them after sorting

Sample Output

Enter values in First array:  a h b c u v I j k e

Enter values in Second array: y u d f g k I w q a

Merged array:  a y h u b d c f g v k I j w q e

Sorted Array in Ascending order:  a b c d e f g h I j k q U v w y

solve it 

-reply by amir

Comment/Reply (w/o sign-up)



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*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : c, programming, arrays, clear, description, arrays

  1. I Want To Learn Programming
    (7)
  2. Finding The Rgb Color Of An Image
    Using any programming language (6)
    hello friends i am doing a project , in that i am inneed to fine the mean RGB value of a particular
    image , is there any program in any language which can do this ? the input to the program will be
    any image ( can be linked in the program) and the putput must be the mean RGB value of that
    particular image , is this Possible ? If so please post the Coding , no matter in what ever language
    it may be written.Thanks in Advance....
  3. Why Must We Learn Object Oriented Programming
    help me to understand the benefit of OOP (8)
    As a software laboratory assistant, I teach the class of Object Oriented Programming. Everything
    went well until one day a student of mine ask me a simple problem like this Q/A: him : Why must we
    use Object oriented Programming? I thought the usual one could do almost everything. me : Well, in
    Object Oriented Programming we could make a class, so basically eveything could be made to an
    'object' in our mind. him : That's why I'm asking, why must we made everything an
    object? What's the difference? me : In Object Oriented Programming, Encapsulation ....
  4. Alright, I'm Taking Computer Programming As A Class In School.
    (10)
    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.....
  5. 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....
  6. C Programming Video Tutorials
    This is for all the memebers out there looking or need some mroe help (3)
    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 ....
  7. 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.....
  8. 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. ....
  9. Beginners Guide To C/c++ Programming?
    (3)
    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....
  10. 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?....
  11. 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 trivial) prog....
  12. 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....
  13. 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
    ....
  14. 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,....
  15. Win32: Dialog Box And Accelerator
    Win32 API programming (3)
    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?....
  16. 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....
  17. 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.......
  18. 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 ....................
  19. 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....
  20. 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....
  21. Life In Programming?
    wheres this headed? (12)
    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 ....
  22. 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.....
  23. 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.
    ....
  24. Good Books On C++ Game Programming
    I need some books! (5)
    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' /> ....
  25. 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....
  26. 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....
  27. 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?....
  28. A Question About C++ Programming Under Linux
    (8)
    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....
  29. Simple C++ Programs
    programming C++ (16)
    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
    (4)
    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.....

    1. Looking for c, programming, arrays, clear, description, arrays
Similar
I Want To Learn Programming
Finding The Rgb Color Of An Image - Using any programming language
Why Must We Learn Object Oriented Programming - help me to understand the benefit of OOP
Alright, I'm Taking Computer Programming As A Class In School.
Discussion On Dynamic Programming
C Programming Video Tutorials - This is for all the memebers out there looking or need some mroe help
C Or C++ Easy Programming Generator - You need a program?
How Many of You Use the C Programming language?
Beginners Guide To C/c++ Programming?
C/c++ Programming Experince - How long does it take before programming useful programs?
Detailed C Beginner Tut - basis for learning any application programming language
Dos Programming, Undocumented Dos, And Dos Secrets
Dos Game Programming In C For Beginners
256-color Vga Programming In C Site
Win32: Dialog Box And Accelerator - Win32 API programming
Programming A Malloc
Where To Start Learning C++ Programming Language - Resources for C++
Never Ending Books For C++ Programming - C++ Programming Books
Vesa Programming Viewer In D O S - Graphics in C/C++
Vesa Programming Viewer In C - Graphics in C
Life In Programming? - wheres this headed?
Programming Help
Where Can I Learn Linux Programming? - any good resources for linux programming
Good Books On C++ Game Programming - I need some books!
Game Programming
D Programming Language
C Most Popular Programming Language - Look at title :)
A Question About C++ Programming Under Linux
Simple C++ Programs - programming C++
C Programming Help

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


C Programming: Arrays - A Clear Description of Arrays

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com