Find the amount under and above the main diagonal EXAMPLE 1 2 3 4 5 6 7 8 9 above it will be 2 + 3 + 6 = 11 - correctly, and under 4 + 7 + 8 = 19, and gives 15 (7 + 8) Here is the code : `

#include <iostream> using namespace std; int main() { int S1, S2, i, j, N, a[10][10]; cout<<"N="; cin>>N; cout<<"Π’Π²Π΅Π΄ΠΈΡ‚Π΅ элСмСнты ΠΌΠ°Ρ‚Ρ€ΠΈΡ†Ρ‹ А \n"; for (i=0; i<N; i++) for (j=0; j<N; j++) cin>>a[i][j]; for (S1=i=0; i<N; i++) for (S2=j=0; j<N; j++) for (j=0; j<N; j++) //Ссли элСмСнт Π»Π΅ΠΆΠΈΡ‚ Π²Ρ‹ΡˆΠ΅ Π³Π»Π°Π²Π½ΠΎΠΉ Π΄ΠΈΠ°Π³ΠΎΠ½Π°Π»ΠΈ, Ρ‚ΠΎ Π½Π°Ρ€Π°Ρ‰ΠΈΠ²Π°Π΅ΠΌ сумму { if (j>i) { S1+=a[i][j]; } else { if (i>j) { S2+=a[i][j]; } } } cout<<"S1="<<S1<<endl; cout<<"S2="<<S2<<endl; return 0; } 
  • So why do you need S1= in for (S1=i=0; i<N; i++) (and similarly in the next cycle)? Do you understand that in S1 zero is placed after at the beginning of each new line? - HolyBlackCat
  • How to fix?? - MyFlow Synthetic
  • Assign zeros S1 and S2 not there, but before the cycles. - HolyBlackCat
  • After entering before the odds? - MyFlow Synthetic
  • Can you try before asking? - HolyBlackCat

1 answer 1

Problem area was

for (S1=i=0; i<N; i++)

for (S2=j=0; j<N; j++)

for (j=0; j<N; j++)

S2 = j = 0

At each iteration of the for (S1=i=0; i<N; i++) loop for (S1=i=0; i<N; i++) S2, the value was set to 0, i.e. the amount was reset

loop for (j=0; j<N; j++)

Turned out to be superfluous


I hope that the code led to a decent look :)

 #include <iostream> using namespace std; int main(void) { int sum1 = 0; int sum2 = 0; cout << "N = "; int N; cin >> N; cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ элСмСнты ΠΌΠ°Ρ‚Ρ€ΠΈΡ†Ρ‹ А: \n"; // Π’Ρ‹Π΄Π΅Π»Π΅Π½ΠΈΠ΅ динамичСской памяти для массива int** arr = new int* [N]; for (int i = 0; i < N; ++i) arr[i] = new int [N]; for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) cin >> arr[i][j]; for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) if (j > i) sum1 += arr[i][j]; // Ссли элСмСнт Π»Π΅ΠΆΠΈΡ‚ Π²Ρ‹ΡˆΠ΅ Π³Π»Π°Π²Π½ΠΎΠΉ Π΄ΠΈΠ°Π³ΠΎΠ½Π°Π»ΠΈ, Ρ‚ΠΎ Π½Π°Ρ€Π°Ρ‰ΠΈΠ²Π°Π΅ΠΌ сумму else if (i > j) sum2 += arr[i][j]; cout << "S1=" << sum1 << endl; cout << "S2=" << sum2 << endl; // ОсвобоТдСниС Π²Ρ‹Π΄Π΅Π»Π΅Π½ΠΎΠΉ памяти для массива for (int i = 0; i < N; ++i) delete[] arr[i]; delete[] arr; return 0; }