write c programme to accept a 2-D array from the user and find the sum of the forst and the last row in the 2-D array
Answers
Answer:
#include <stdio.h>
int main()
{
int rows , collumns;
printf("Enter the number of rows in the matrix:");
scanf("%d" , &rows);
printf("Enter the number of collumns in the matrix:");
scanf("%d" , &collumns);
int arr[rows][collumns];
printf("Enter all elements in the matrix- \n");
for(int i = 0; i < rows; i++){
for(int j = 0; j < collumns; j++){
scanf("%d" , &arr[i][j]);
}
}
printf("---THE MATRIX YOU ENTERED--- \n");
for(int i = 0; i < rows; i++){
for(int j = 0; j < collumns; j++){
printf("%d " , arr[i][j]);
}
printf("\n");
}
int firstRowSum = 0 , lastRowSum = 0;
for(int i = 0; i < rows; i++){
firstRowSum = firstRowSum + arr[0][i];
}
for(int i = 0; i < rows; i++){
lastRowSum = lastRowSum + arr[rows-1][i];
}
printf("The sum of all the elements in the first row is %d \n" , firstRowSum);
printf("The sum of all the elements in the last row is %d \n" , lastRowSum);
return 0;
}
Explanation: