Pointers Programtask Slide74

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

#include <stdio.

h>
#define ROWS 3
#define COLS 4

/* 1 2 3 4
5 6 7 8
9 10 11 12
*/

void sumsOfRows(int *mat, int m, int n, int *res) {


for(int i = 0; i < m; i++) {
*(res + i) = 0;
for(int j = 0; j < n; j++) {
*(res + i) += *(mat + n * i + j);
}
}
}

int main() {
int matrix[ROWS][COLS];
int res[ROWS];

printf("Enter members of 3x4 matrix > ");


for(int i = 0; i < ROWS; i++) {
for(int j = 0; j < COLS; j++) {
scanf("%d", &matrix[i][j]);
}
}

sumsOfRows(&matrix[0][0], ROWS, COLS, &res[0]);


printf("Sums of rows: \n");
for(int i = 0; i < ROWS; i++) {
printf("%d\n", res[i]);
}

return 0;
}

You might also like