0

Subroutine work

Please, i need help with this. I don't know how to do this. The given matrix M(4,4). Find the product of the elements in each row of the matrix. Find the product of the elements in line in the form of a subroutine. Derive this matrix and the result.

13th Dec 2021, 10:39 PM
Devenage
Devenage - avatar
7 odpowiedzi
+ 3
Call dob() inside the outer loop, but you'd need to adjust the output of the product. This assumes the matrix has a static size of 4 (rows and columns). If matrix size varies, edit is in order. #include <stdlib.h> #include <stdio.h> #include <time.h> void dob(int[4]); int main() { srand(time(NULL)); int A[4][4]; printf("Matrix:\n"); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { A[i][j] = rand() % 100; printf("%i\t", A[i][j]); } dob(A[i]); printf("\n"); } return 0; } void dob(int a[4]) { int res = a[0] * a[1] * a[2] * a[3]; printf("Product: %d\n", res); }
14th Dec 2021, 6:59 AM
Ipang
+ 4
Show your attempt.
13th Dec 2021, 11:08 PM
Michal Doruch
+ 1
To get into all the matrix cases you should set a "for statement" inside an other "for statement". Try to do it and we will see.
14th Dec 2021, 12:42 AM
TheWitcher
TheWitcher - avatar
+ 1
What do you mean by "Derive this matrix and the result."?
14th Dec 2021, 4:03 AM
Ipang
+ 1
Ipang Print this matrix and result
14th Dec 2021, 6:30 AM
Devenage
Devenage - avatar
0
Martin Taylor TheWitcher Michal Doruch Im studying at university but im bad in C. #include <stdlib.h> #include <stdio.h> #include <math.h> #include <time.h> void dob(int a, int b, int c, int d); int main(){ srand ( time(NULL) ); int A[4][4]; printf("Matrix:\n"); for(int i=0; i<4; i++){ for(int j=0; j<4; j++){ A[i][j]=rand()%100; printf("%i\t", A[i][j]); } printf("\n"); } dob(A[0][1],A[0][2],A[0][3],A[0][4]); return 0; } void dob(int a, int b, int c, int d){ int res = a*b*c*d; printf("\nProduct:%d\n", res); }
14th Dec 2021, 1:28 AM
Devenage
Devenage - avatar