- 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; }

30th May 2020, 1:40 PM
Viktor Kizera
Viktor Kizera - avatar
18 Réponses
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 ...
30th May 2020, 6:05 PM
Ipang
+ 3
Ipang Well, the task was not said, so I think 70,50, 30 (and everything under it, ie 80,90,60)
30th May 2020, 2:34 PM
Viktor Kizera
Viktor Kizera - avatar
+ 3
Ipang Ok, thank you, if I know I will definitely say)
30th May 2020, 2:41 PM
Viktor Kizera
Viktor Kizera - avatar
+ 2
Ipang Here still the variant that 10,50,90 (and 40,70,80) can approach
30th May 2020, 2:35 PM
Viktor Kizera
Viktor Kizera - avatar
+ 2
Ipang Thank you very much, everything works😊
30th May 2020, 4:37 PM
Viktor Kizera
Viktor Kizera - avatar
+ 2
Thank you very, very much😊 Ipang
30th May 2020, 7:45 PM
Viktor Kizera
Viktor Kizera - avatar
+ 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
30th May 2020, 6:02 PM
Viktor Kizera
Viktor Kizera - avatar
+ 1
How do you say the number to count? What will it look like in the code? Ipang
30th May 2020, 6:11 PM
Viktor Kizera
Viktor Kizera - avatar
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"?
30th May 2020, 2:28 PM
Ipang
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
30th May 2020, 3:39 PM
Ipang
0
idk, I don't do sum of cubes
1st Jun 2020, 1:43 PM
DrNefardio
DrNefardio - avatar
- 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 ...
30th May 2020, 2:39 PM
Ipang
- 1
You're welcome Sin Cara M 👌
30th May 2020, 4:38 PM
Ipang
- 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
30th May 2020, 4:55 PM
Viktor Kizera
Viktor Kizera - avatar
- 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
30th May 2020, 5:19 PM
Ipang
- 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
30th May 2020, 6:21 PM
Ipang
- 1
You're welcome Sin Cara M 👌
30th May 2020, 8:50 PM
Ipang
- 1
#include
31st May 2020, 10:25 AM
ShaBil Mohammed
ShaBil Mohammed - avatar