0% found this document useful (0 votes)
34 views1 page

Pointers Programtask Slide74

The document contains code to calculate the sums of rows in a 3x4 integer matrix. It defines macros for the number of rows and columns, declares a function to calculate row sums and stores them in a result array, and includes a main function that takes user input for the matrix, calls the row sum function, and prints the results.

Uploaded by

petra710386
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views1 page

Pointers Programtask Slide74

The document contains code to calculate the sums of rows in a 3x4 integer matrix. It defines macros for the number of rows and columns, declares a function to calculate row sums and stores them in a result array, and includes a main function that takes user input for the matrix, calls the row sum function, and prints the results.

Uploaded by

petra710386
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#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