Programming For Engineering
Assignment -3-
Arrays
Consider a 5-by-20 integer array grades:
a) Write a declaration for grades?
➡int grades[5][20];
b) How many rows does the array have?
Rows 5
c) How many columns does the array have?
Columns=20
d) How many elements does the array have?
5 Rows 20 Columns 100 elements
e) Write the names of all elements in the first column of the array?
grades[0][0]
grades[1][0]
grades[2][0]
grades[3][0]
grades[4][0]
f) Write the name of the element in the third row and second column of the array?
grades[2][1]
g) Write a single statement to assign the value 100 to the element in the first row and
second column?
grades[0][1] =100;
h) Write a nested loop to get all the elements from the keyboard?
int main(){
int grades[5][20];
for (int i=0; i<5; i++) {
for (int j=0;j<20; j++) {
scanf("%d", &grades[i][j]);
}
}
return 0;
}
i) Write a nested for statement to initialise all elements to zero?
for (int i=0; i<5; i++) {
for (int j=0; j<20; j++) {
grades[i][j]=0;
}
}
j) Write a statement that copies the values from an array double mathGrades[20] into the
elements of the first row of marks?
}
for (int i=0;i< 20; i++) {
grades[0][i] mathGrades[i];
k) Write a series of statements that determine and print the highest value in the first row
of grades?
int highest grades[0][0];
for (int i=1;i<20; i++) {
if (grades[0][i]> highest) {
highest grades[0][i];
}
}
printf("Highest value in the first row: %d\n", highest);
l) Write a statement to display the elements in column 2 of the array?
for (int i = 0; i < 5; i++) {
printf("%d\n", grades[i][1]);
}
m) Write a statement to calculate the average of the elements in the first row?
➡double sum = 0;
for (int i=0; i<20; i++) {
sum += grades[0][0];
}
double average sum/20;
printf("Average of the elements in the first row: %f\n", average);
n) Write a series of statements that prints the array grades in a tabular format?
for (int i=0;i< 5; i++) {
for (int j=0;j<20; j++) {
printf("%d\t", grades[][]);
}
printf("\n");
}