ICS 103: Computer Programming in C
Handout-16
Topic: 2-D
Arrays
Instructor:
M. Waheed Aslam.
Objective:
·
To know what is 2-D array?
·
To know how to declare and
reference 2-D array.
·
To know how to read and
print 2-D array using for loops.
·
To know what is row-major
and column-major order of 2-D array.
·
Just like single
dimensional arrays we can have multidimensional arrays which are arrays of
arrays.
·
The
multidimensional arrays can be visualized as a matrix.
·
The
two-dimensional arrays can be declared as single dimension arrays as:
#define SIZE 5
#define SIZE1 25
int grades [SIZE] [10];
char letter [10] [4];
float xyz [100] [SIZE1];
·
The two-dimensional arrays need two sizes as name[m][n].
·
The total size of the array is the product of the two sizes m x n.
·
The first
element is always name[0][0] and the last element is name[m-1][n-1].
·
The two dimensional array elements are ordered by row-wise as name[0][0], name[0][1], name[0][2]…, name[0][9],
name[1][0], name[1][1],…, name[4][8], name[4][9].
·
The two dimensional array elements are ordered by column-wise as name[0][0],
name[1][0], name[2][0]…, name[4][0], name[0][1], name[1][1],…, name[3][9], name[4][9].
In the above
two-dimensional array:
·
In the first case the grades is an integer array of size 5 X 10 = 50.
·
The elements can be referenced by using 2 subscripts as grades[1][9], grades[4][9].
·
The first
element is grades[0][0] and the last
element is grades[4][9]. Similarly, the letter is an array of characters of
size 40 with last element being
letter[9][3]; xyz is an array of float
of size 2500 with last element being
xyz[99][24].
The two-dimensional
arrays can be initialized as
int x [3] [2] = { 12, 56, 67, 4, 6 ,78 };
where
the x[0][0] = 12, x[0][1] = 56, x[1][0] = 67, x[1][1] = 4, x[2][0] = 6, x[2][1]
= 78.
·
As with single dimension arrays if the initial values
are less than the size of array the remaining values are set to 0.
The
two-dimensional arrays can also be initialized by using for loop.
To initialize, read and
print two-dimensional arrays we need two for loops.
·
The first loop controls the first subscript and
·
The second loop controls the second
subscript.
Consider the following
example:
int abc[5][10] , i, j
;
for ( i = 0 ; i < 5 ; ++ i )
for ( j = 0; j <
10 ; ++j )
{ printf (“Enter the [%d][%d] element \n”, i, j
);
scanf (“ %d ”,
&abc[i][j] ) ;
}
In the above case the array is read in row-major order.
To read in column-major order
just reverse the two for loops of two subscripts as
for ( j = 0 ; j < 10 ; ++ j )
for ( i = 0;
i < 5 ; ++i )
{
printf (“Enter the
[%d][%d] element \n”, i, j );
scanf (“ %d ”, &abc[i][j] ) ;
}
Similarly for printing in row-major order
for ( i = 0 ; i < 5 ; ++ i )
for ( j = 0; j < 10 ; ++j )
{
printf (“The abc[%d][%d] element is %d
\n”, i, j, abc[i][j] );
}
and printing in column-major order
for ( j = 0 ; j < 10 ; ++ j )
for ( i = 0; i < 5 ; ++i )
{
printf (“The abc[%d][%d] element is %d
\n”, i, j, abc[i][j] );
}
Solved Problem for 2-d array:
/*Matrix
Addition */
#include<stdio.h>
#define row 10
#define col 10
int main()
{
int i,j,k, a[row][col],
b[row][col], s[row][col] = {0};
int rowsa, colsa, rowsb,
colsb;
printf("Enter
number of rows for Matrix 1: ");
scanf("%d",
&rowsa);
printf("Enter
number of columns for Matrix 1: ");
scanf("%d",
&colsa);
printf("Enter the
%d elements of Matrix 1 : \n", rowsa*colsa);
for(j=0;j<rowsa; j++)
// reading matrix a
{
for(k=0;k<colsa;k++)
{
scanf("%d", &a[j][k]);
}
}
puts("\n");
printf("Enter
number of rows for Matrix 2: ");
scanf("%d",&rowsb);
printf("Enter
number of columns for Matrix 2: ");
scanf("%d",&colsb);
printf("Enter the
%d elements of Matrix 2 : \n", rowsb*colsb);
for(j=0;j<rowsb; j++)
// reading matrix b
{
for(k=0;k<colsb;k++)
{
scanf("%d", &b[j][k]);
}
}
/* Addition
of two matrices */
for(j=0; j<rowsb; j++)
{
for(k=0; k<colsb; k++)
{
s[j][k]=a[j][k]+b[j][k];
}
}
/*Print sum of two matrices */
printf("The sum of
two matrices is : \n");
for(j=0; j<rowsa; j++)
{ for (k=0; k<rowsb; k++)
printf("%5d
", s[j][k]);
printf("\n");
}
return 0;
} // end of
main
Another Solved Example for Double-subscripted array
#include<stdio.h>
const int
students = 3; // number of students
const int
exams = 4; // number of exams
int minimum(
int [][ exams ], int, int );
int
maximum(int [][ exams ],
int, int );
float
average( int [], int );
void
printArray( int [][ exams ],
int, int );
int main()
{
int studentGrades[ students ][ exams ] =
{ { 77, 68, 86, 73 },
{ 96, 87, 89, 78 },
{ 70, 90, 86, 81 } };
printf("The array is:\n");
printArray( studentGrades, students, exams
);
printf("\n\nLowest grade is: %d ",minimum( studentGrades, students,
exams ));
printf("\nHighest grade is: %d",
maximum( studentGrades, students, exams ));
printf("\n");
for ( int person = 0; person < students;
person++ )
printf("The average grade for
student %d is %.2f\n", person,
average( studentGrades[ person ],
exams ));
return 0;
}
// Find the
minimum grade
int minimum(
int grades[][ exams ],
int pupils, int tests )
{
int lowGrade = 100;
for ( int i = 0; i < pupils; i++ )
for ( int j = 0; j < tests; j++ )
if ( grades[ i ][ j ] < lowGrade )
lowGrade = grades[ i ][ j ];
return lowGrade;
}
// Find the
maximum grade
int maximum(
int grades[][ exams ],
int pupils, int tests )
{
int highGrade = 0;
for ( int i = 0; i < pupils; i++ )
for ( int j = 0; j < tests; j++ )
if ( grades[ i ][ j ] > highGrade )
highGrade = grades[ i ][ j ];
return highGrade;
}
// Determine
the average grade for a particular student
float
average( int setOfGrades[], int tests
)
{
int total = 0;
for ( int i = 0; i < tests; i++ )
total += setOfGrades[ i ];
return ( float ) total / tests;
}
// Print the
array
void
printArray( int grades[][ exams ],
int pupils, int tests )
{
printf(" [0] [1]
[2] [3]");
for ( int i = 0; i < pupils; i++ ) {
printf("\nstudentGrades [%d]
",i);
for ( int j = 0; j < tests; j++ )
printf("%d ",grades[ i ][ j ]);
}
}