Thursday, November 5, 2009
Types of arrays
Arrays are divided into two types:
1. Single Dimension Array
2. Multi Dimension Array
The arrays created in the previous session using the statement like “arr[5]” are single dimension arrays. The number within the square brackets [] signify the number of rows that the array will have. A size of 5 denotes that the array will have 5 rows inside it. What the statement above does not denote is that how many columns the array will have. Nothing is given regarding the number of columns. In such cases the compiler assumes the number of columns as 1. Therefore, the array will be created with 5 rows and a single column. Arrays having multiple columns but a single column are called single dimension array.
A multidimensional array on the other hand has multiple columns and multiple rows. Both the values are specified while the array is declared. For example, in the statement “int array[3][4]”, 3 denotes the number of rows and 4 is the number of columns, therefore this array is created as a matrix of 3 rows and 4 columns. A multidimensional array will have a storage capacity of “rows * columns” which means that the above array is capable of holding “3x4=12” values of integer type. Such types of arrays are quite handy when the data is to be stored in the form of a table. Do remember that like rows, the columns also start from 0.
Example 1 : Input and output in a multidimensional array.
void main()
{
int table[3][5],i,j;
for(i=0;i<3;i++)// the first loop will be for rows
{
for(j=0;j<5;j++)//for columns
{
printf(“\nEnter values : “);
scanf(“%d”,&table[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
printf(“%d”,table[i][j]);
}
printf(“\n”);
}
Description
In the above program an array named “table” is created with 3 rows and 5 tables. The first nested loop is used to get the values from the user and store it to the array. Note that both the outer loops are written to run from 0 to 2 because the rows are numbered like this. Similarly the inner loops are working as per the number of columns i.e. from 0 to 4.
The second nested loop is the output loop to print all the 15 values. We want the values to be printed in rows and columns therefore “\n” is being used after the printing of first row.
Let’s create some more programs using the same method.
Example 2: Program to print the sum of individual rows of an array.
void main()
{
float growth[4][8],sum;
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<8;j++)
{
printf(“\nEnter growth percentage of the company :”);
scanf(“%f”,&growth[i][j]);
}
}
for(i=0;i<4;i++)
{
sum=0;
f(j=0;j<8;j++)
{
sum+=growth[i][j];
}
printf(“\nSum of %d row is %f”,i+1,sum);
}
Description
The first loop again is an input loop to populate the array. The second loop first initializes the value of sum to 0 and starts adding the values of the array. When the second loop runs for the first time the value of i and j is 0 so the value of growth [0] [0] is added to sum. Being a nested loop the value of j is incremented to 1 while I remains at 0 therefore the next values to be added to sum is growth[0][1] and growth[0][2] and so on till growth[0][8]. When the inner loop completes its course by running eight times it terminates and prints the value of sum which will be the sum of all the values of the first row. After this the outer loop runs again repeating the whole process but since the value of i is now 1 the values of the second row will be added. Once the sum of one row is completed and printed it is very important that the value of sum is reset to 0 so that the sum of the next row starts from 0.
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment