Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> C Programming: Arrays, A Clear Description of Arrays
jayron
post Jul 2 2006, 11:57 AM
Post #1


Newbie [Level 1]
*

Group: Members
Posts: 22
Joined: 6-June 06
Member No.: 24,795



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.

This post has been edited by jayron: Jul 2 2006, 12:32 PM
Go to the top of the page
 
+Quote Post
jayron
post Jul 2 2006, 12:32 PM
Post #2


Newbie [Level 1]
*

Group: Members
Posts: 22
Joined: 6-June 06
Member No.: 24,795



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.
Go to the top of the page
 
+Quote Post
gdinod
post Jun 9 2007, 03:11 AM
Post #3


Newbie
*

Group: Members
Posts: 2
Joined: 9-June 07
Member No.: 44,451



Clear description of arrays for beginners! Whats your reference? book? site?
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. A Question About C++ Programming Under Linux(7)
  2. What Is A T_string?(13)
  3. What Is Neuro Linguistic Programming(19)
  4. Good Books On C++ Game Programming(2)
  5. What Programming Languages Should I Choose?(17)
  6. How Can I Learn Assembly(14)
  7. Life In Programming?(11)
  8. Free Tutorial (web Programming) + Free Web Hosting(10)
  9. Win32: Dialog Box And Accelerator(1)
  10. What Is Programming Language Of Google Etc.?(21)
  11. Java Helpdesk(8)
  12. How Many of You Use the C Programming language?(23)
  13. Programming In Glut (lesson 6)(2)
  14. Alright, I'm Taking Computer Programming As A Class In School.(10)
  15. The Best Game Programming Languages?(25)
  1. I'm Looking For Help With Programming (i Think)(3)
  2. Python(8)
  3. Php And Mysql Programming(2)
  4. Arrays And List Boxes(2)
  5. Learn Java Programming Language Online Step By Step(1)
  6. What Is Genetic Programming?(3)
  7. A Prelude To Programming(10)
  8. Best Programming Language For Win Vista?(3)
  9. Making A Picture Viewer Website(3)
  10. Project Euler(1)
  11. Short Info About About Apache Maven.(0)
  12. Any Way To Get Clear, Fresh Skin In 4 Days?(3)
  13. Clearing Your Ie Tif(0)


 



- Lo-Fi Version Time is now: 5th September 2008 - 08:33 PM