two dimensional arrays

advertisement
Types of Arrays:
- one dimensional
- two dimensional and
- multidimensional
ASSIGNING INITIAL VALUES TO ARRAYS
The declaration is preceded by the word static. The initial values are enclosed in braces, eg,
Example:
#include <stdio.h>
main()
{
int x;
static int values[] = { 1,2,3,4,5,6,7,8,9 };
static char word[] = { 'H','e','l','l','o' };
for( x = 0; x < 9; ++x )
printf("Values [%d] is %d\n", x, values[x]);
}
The previous program declares two arrays, values and word. Note that inside the squarebrackets
there is no variable to indicate how big the array is to be. In this case, C initializes the array to
the number of elements that appear within the initialize braces. So values consist of 9 elements
(numbered 0 to 8) and the char array word has 5 elements.
TWO DIMENSIONAL ARRAYS
A two-dimensional array is really nothing more than an array of arrays (a three-dimensional
array is an array of arrays of arrays). Think of your dinner. You could have a one-dimensional
list
of
everything
you
eat:
(lettuce, tomatoes, salad dressing, steak, mashed potatoes, string beans, cake, ice cream, coffee)
Or you could have a two-dimensional list of three courses, each containing three things you eat:
(lettuce, tomatoes, salad dressing) and (steak, mashed potatoes, string beans) and (cake, ice
cream,
coffee)
In the case of an array, our old-fashioned one-dimensional array looks like this:
int[] myArray = {0,1,2,3};
And a two-dimensional array looks like this:
int[][] myArray = { {0,1,2,3}, {3,2,1,0}, {3,5,6,1}, {3,8,3,4} };
For our purposes, it is better to think of the two-dimensional array as a matrix. A matrix can be
thought of as a grid of numbers, arranged in rows and columns, kind of like a bingo board. We
might write the two-dimensional array out as follows to illustrate this point:
int[][] myArray = { {0, 1, 2, 3},
{3, 2, 1, 0},
{3, 5, 6, 1},
{3, 8, 3, 4} };
Syntax:
<data-type> <array_nm> [row_subscript][column-subscript];
Example:
int a[3][3];
In above example, a is an array of type integer which has storage size of 3 * 3 matrix. The total
size would be 3 * 3 * 2 = 18 bytes.
It is also called as 'multidimensional array.'
Memory Allocation :
SIMPLE TWO DIMENSIONAL ARRAY PROGRAM
/* Program to demonstrate two dimensional array.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[3][3], i, j;
clrscr();
printf("\n\t Enter matrix of 3*3 : ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d",&a[i][j]); //read 3*3 array
}
}
printf("\n\t Matrix is : \n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("\t %d",a[i][j]); //print 3*3 array
}
printf("\n");
}
getch();
}
Output :
Enter matrix of 3*3 : 3 4 5 6 7 2 1 2 3
Matrix is :
3
4
6
7
1
2
5
2
3_
Limitations of two dimensional array :
o
o
We cannot delete any element from an array.
If we dont know that how many elements have to be stored in a memory in advance, then
there will be memory wastage if large array size is specified.
MULTI DIMENSIONAL ARRAYS
C programming language allows creating arrays of arrays known as multidimensional arrays. For
example:
float a[2][6];
Here, a is an array of two dimension, which is an example of multidimensional array. This array
has 2 rows and 6 columns
For better understanding of multidimensional arrays, array elements of above example can be
thinked of as below:
Initialization of Multidimensional Arrays
In C, multidimensional arrays can be initialized in different number of ways.
int c[2][3]={{1,3,0}, {-1,5,9}};
OR
int c[][3]={{1,3,0}, {-1,5,9}};
OR
int c[2][3]={1,3,0,-1,5,9};
Initialization Of three-dimensional Array
double cprogram[3][2][4]={
{{-0.1, 0.22, 0.3, 4.3}, {2.3, 4.7, -0.9, 2}},
{{0.9, 3.6, 4.5, 4}, {1.2, 2.4, 0.22, -1}},
{{8.2, 3.12, 34.2, 0.1}, {2.1, 3.2, 4.3, -2.0}}
};
Example:
C program to find sum of two matrix of order 2*2 using multidimensional arrays where,
elements of matrix are entered by user.
#include <stdio.h>
int main(){
float a[2][2], b[2][2], c[2][2];
int i,j;
printf("Enter the elements of 1st matrix\n");
/* Reading two dimensional Array with the help of two for loop. If there was an array of 'n'
dimension, 'n' numbers of loops are needed for inserting data to array.*/
for(i=0;i<2;++i)
for(j=0;j<2;++j){
printf("Enter a%d%d: ",i+1,j+1);
scanf("%f",&a[i][j]);
}
printf("Enter the elements of 2nd matrix\n");
for(i=0;i<2;++i)
for(j=0;j<2;++j){
printf("Enter b%d%d: ",i+1,j+1);
scanf("%f",&b[i][j]);
}
for(i=0;i<2;++i)
for(j=0;j<2;++j){
/* Writing the elements of multidimensional array using loop. */
c[i][j]=a[i][j]+b[i][j]; /* Sum of corresponding elements of two arrays. */
}
printf("\nSum Of Matrix:");
for(i=0;i<2;++i)
for(j=0;j<2;++j){
printf("%.1f\t",c[i][j]);
if(j==1)
/* To display matrix sum in order. */
printf("\n");
}
return 0;
}
Ouput
Enter the elements of 1st matrix
Enter a11: 2;
Enter a12: 0.5;
Enter a21: -1.1;
Enter a22: 2;
Enter the elements of 2nd matrix
Enter b11: 0.2;
Enter b12: 0;
Enter b21: 0.23;
Enter b22: 23;
Sum Of Matrix:
2.2 0.5
-0.9 25.0
Multi-dimensioned arrays
Multi-dimensioned arrays have two or more index values which specify the element in the array.
multi[i][j]
In the above example, the first index value i specifies a row index, whilst j specifies a column
index.
DECLARATION
int m1[10][10];
static int m2[2][2] = { {0,1}, {2,3} };
sum = m1[i][j] + m2[k][l];
NOTE the strange way that the initial values have been assigned to the two-dimensional array
m2. Inside the braces are,
{ 0, 1 },
{ 2, 3 }
Remember that arrays are split up into row and columns. The first is the row, the second is the
column. Looking at the initial values assigned to m2, they are,
m2[0][0] = 0
m2[0][1] = 1
m2[1][0] = 2
m2[1][1] = 3
Example:
#include <stdio.h>
main()
{
static int m[][] = { {10,5,-3}, {9, 0, 0}, {32,20,1}, {0,0,8} };
int row, column, sum;
sum = 0;
for( row = 0; row < 4; row++ )
for( column = 0; column < 3; column++ )
sum = sum + m[row][column];
printf("The total is %d\n", sum );
}
Download