- 2
How to calculate the sum in the matrix, diagonally, and everything below the diagonal (below it)
Cod: #include<iostream> #include<stdlib.h> #define n 3 #define m 3 using namespace std; int main () { int i,j; int a[n][m],q,w; for(i=0;i<n;i++) for (j=0;j<m;j++) a[i][j]=rand()%10; for(i=0;i<n;i++) { for(j=0;j<m;j++) cout<<a[i][j]<<" "; cout<<endl; } for(i=0;i<n;i++) {q=0; for (j=0;j<m;j++) if(i==j) q=(i+j); cout<<"Сума 1= "<<q<<"\n"; } for(i=0;i<n;i++) {w=0; for (j=0;j<m;j++) if(i<j) w=(i+j); cout<<"Сума 2 = "<<w<<"\n"; } return 0; }
18 ответов
0
Sin Cara M
I have modified the code to separate sum of diagonal elements with sum of elements under the diagonal.
Number of elements can be obtained just by multiplying <n> by <m> I guess ...
+ 3
Ipang Well, the task was not said, so I think 70,50, 30 (and everything under it, ie 80,90,60)
+ 3
Ipang Ok, thank you, if I know I will definitely say)
+ 2
Ipang Here still the variant that 10,50,90 (and 40,70,80) can approach
+ 2
Ipang Thank you very much, everything works😊
+ 2
Thank you very, very much😊 Ipang
+ 1
There should be a sum of 1 - 10,50,90, and the sum 2 - 40,70,90. And the number must be recorded Ipang
+ 1
How do you say the number to count? What will it look like in the code? Ipang
0
Imagine we have this,
10 20 30
40 50 60
70 80 90
Which elements are you trying to sum?
I didn't understand "everything below the diagonal", using the above matrix, which elements are "below diagonal"?
0
Sin Cara M
I managed to work it out like this.
Can you please check it out ...
https://code.sololearn.com/cQc80Y1d30qj/?ref=app
0
idk, I don't do sum of cubes
- 1
Sin Cara M
I need time to think this through. Let me get back to you later. Ping me if you got a solution, I would like to learn too ...
- 1
You're welcome Sin Cara M 👌
- 1
And no it turned out you need to find the sum of the diagonal, ie it will be the sum of 1, and the sum of 2 will be what is under it, and still need the number of elements in the matrix (there will be 9) but how to write them😅😅 Ipang
- 1
I misunderstood the objective. I thought sum of 1 was
10
40 50
70 80 90
And sum of 2 was
30
50 60
70 80 90
Hahaha LMAO
- 1
Where it says
dia += a[i][j];
Or
under += a[i][j]
That are parts which decides whether an element lies on diagonal slot or underneath it. You can `cout a[i][j];` near those lines, to print the values Sin Cara M
- 1
You're welcome Sin Cara M 👌
- 1
#include