Lecture 5: 2-D Arrays & Functions
Objectives of this lecture
q Learn how to declare, initialize, and process 2-D
arrays
q Be able to pass 2-D array as a parameter to a function
q Be able to apply 2-D array in problem solving
Declaration
q C allow the declaration of array of any dimension
q 2-D array can be visualized as a matrix or table of
data elements consisting of rows and columns
q Declaration is similar to 1-D array except that the
size of both rows and columns must be specified, each in its own bracket.
q Example: int grades[100][6]
q The first size indicates the number of rows, the
second the number of columns
q The indexing starts from 0. 0…99 & 0…5 in the
above example
Initialization
q 2-D array can be initialized at declaration, using
either of the following methods:
1.
int X[3][2] = { {17,22}, {-33,65}, {14,31}}
2.
int X[3][2] = {17, 22, -33, 65, 14, 31}
q Each of these methods inserts the data into the array
row-wise.
q In either of these methods, if the data is not enough,
the remaining elements are set to zero.
q Data can also be read into a 2-D array using nested
loops. This can be row-wise or
column-wise as shown below:
q Row-wise
for (row=0;
row<rsize, row++)
for
(col=0; col<csize; col++)
scanf(“%d”, &a[row][col]);
q Column-wise
for (col=0;
col<csize, col++)
for
(row=0; row<rsize; row++)
scanf(“%d”, &a[row][col]);
Example 1:
Problem: The data for three students in three exams
of ICS201 is shown in the table below:
EXAM I
|
EXAM II
|
EXAM III
|
50
|
75
|
60
|
90
|
45
|
70
|
80
|
55
|
67
|
Write a C program
that prints the above data with additional column containing the average of
each student and additional row containing the average of each exam. The program should also print the overall
average.
Reasoning: We use a 2-D array to store the data. We then use nested loops row-wise
and column-wise to compute the required averages
Input:
no input
Ouput:
the averages of each student and each exam.
Process: average = sum of grades / no of exams (or
students)
Solution:
Step 1: LOOP no of students
a. initialize sum to zero
b. LOOP number of exams
sum=sum+grade[j]
c. student[i] average = sum/no. of exams
step
2: LOOP number of exams
a. initialize sum to zero
b. LOOP number of students
Sum = sum+grade[i]
c. exam [j] average =sum/no. of students
Step
3: initialize sum to zero
Step 4: LOOP number of students
LOOP number of exams
Sum=sum+grade[i][j]
Step
5: average=sum/(no. students * no. of exams)
Step
6: LOOP number of students
a. LOOP number of exams
OUTPUT grade[i][j]
b. OUTPUT student[i] average
Step
7: LOOP number of exams
OUTPUT exam average
Step
8: OUTPUT overall average
Step
9: STOP
/* computes the average grades of students in 3 exams.
also computes the average
grade in each exam. */
#include <stdio.h>
#define NEXAMS 3
#define NSTUDS 3
main()
{ int row, col;
float grade[NSTUDS][NEXAMS]={{50.0,75.0,60.0},
{90.0,45.0,70.0},
{80.0,55.0,67.0}};
float avg_stu[NSTUDS], avg_exm[NEXAMS],sum,avg;
for (row=0; row<NSTUDS;
row++)
{ sum=0;
for (col=0;
col<NEXAMS; col++)
sum+=grade[row][col];
avg_stu[row]=sum/NEXAMS;
}
for (col=0; col<NEXAMS;
col++)
{ sum=0;
for (row=0;
row<NSTUDS; row++)
sum+=grade[row][col];
avg_exm[col]=sum/NSTUDS;
}
sum=0
for (row=0; row<NSTUDS;
row++)
for (col=0; col<NEXAMS;
col++)
sum+=grade[row][col];
avg=sum/(NEXAMS*NSTUDS);
for (row=0; row<NSTUDS;
row++)
{ for (col=0; col<NEXAMS; col++)
printf("%7.2f",grade[row][col]);
printf("%7.2f\n",avg_stu[row]);
}
for (col=0; col<NEXAMS;
col++)
printf("%7.2f",avg_exm[col]);
printf("\n\nThe overall
average = %5.2f\n",avg);
return 0;
}
Passing multi-dimensional
array as a parameter
q This is similar to 1-D array - the argument is the array name followed by brackets for each of
the dimensions
q However, the sizes of all the dimensions except the
last, must be specified
q As Actual parameter, we just use the array name.
Example 2:
Problem: Modify example 1 above so that it reads the
number of students (maximum 30) and to include the following functions:
Ø
a function get_data
to read the data
Ø
a function avr_row
that returns the average of a given row
Ø
a function avg_col
that returns the average of a given column
Ø
a function average
that returns the overall averge.
/* computes the average grades of students in 3 exams.
also computes the average
grade in each exam. */
#include <stdio.h>
#define NEXAMS 3
#define MAXSTD 30
void get_data(float grade[][NEXAMS], int n);
float avg_row(float grade[][NEXAMS], int i, int colsize);
float avg_col(float grade[][NEXAMS], int j, int rowsize);
float average(float grade[][NEXAMS], int rowsize, int colsize);
main()
{ int row, col, n;
float grade[MAXSTD][NEXAMS],
avg_stu[MAXSTD], avg_exm[NEXAMS];
printf("Enter number of
students >");
scanf("%d",&n);
get_data(grade,n);
for (row=0; row<n; row++)
avg_stu[row]=avg_row(grade,row,3);
for (col=0; col<NEXAMS;
col++)
avg_exm[col]=avg_col(grade,col,n);
for (row=0; row<n; row++)
{ for (col=0; col<NEXAMS; col++)
printf("%7.2f",grade[row][col]);
printf("%7.2f\n",avg_stu[row]);
}
for (col=0; col<NEXAMS;
col++)
printf("%7.2f",avg_exm[col]);
printf("\n\nThe overall
average = %7.2f\n",average(grade,n,NEXAMS));
return 0;
}
/* reads the grades of n students in 3 exams */
void get_data(float grade[][NEXAMS], int n)
{ int row,col;
float sum, avg;
printf("Please enter
grades for %d students row-wise\n",n);
printf("In the form
exam1 exam2 exam3\n");
for (row=0; row<n; row++)
scanf("%f%f%f",&grade[row][0],&grade[row][1],&grade[row][2]);
}
/* returns the average of a given row of a 2D array */
float avg_row(float grade[][NEXAMS], int i, int colsize)
{ int col;
float sum=0.0, avg;
for (col=0; col<colsize;
col++)
sum+=grade[i][col];
avg=sum/colsize;
return avg;
}
/* returns the average of a given col of a 2D array */
float avg_col(float grade[][NEXAMS], int j, int rowsize)
{ int row;
float sum=0.0, avg;
for (row=0;
row<rowsize; row++)
sum+=grade[row][j];
avg=sum/rowsize;
return avg;
}
/* Computes the average of elements in a 2-d array */
float average(float grade[][NEXAMS], int rowsize, int colsize)
{ int row, col;
float sum=0, avg;
for (row=0;
row<rowsize; row++)
for (col=0; col<colsize; col++)
sum+=grade[row][col];
avg=sum/(rowsize*colsize);
return avg;
}