0

Sum above the main diagonal

/*Calculate the total sum of element that are located above the main diagonal in a given NxN square table. Input First line N (1<=N<=100). Then NxN table is given(all number are integers) Output The total sum of element that are located under main diagonals Sample input: 3 1 2 3 1 5 6 1 1 8 Sample output: 11 What is wrong?*/ #include <iostream> using namespace std ; int main (){ int n ,sum; cin >> n ; int aza[100][100]; for (int i=0;i<n;i++){ for(int j=0;j<n;j++){ cin >> aza[i][j]; } } for(int i = 0; i < n; i++){ for(int j = 0; j < i; j++){ sum += aza[i][j]; } } cout << sum; }

3rd Oct 2020, 11:16 AM
Azat Malgazhdar
Azat Malgazhdar - avatar
4 odpowiedzi
0
How sample output is 11.. May I know?
3rd Oct 2020, 11:58 AM
Jayakrishna 🇮🇳
0
2 + 3 +6 =11
3rd Oct 2020, 12:02 PM
Azat Malgazhdar
Azat Malgazhdar - avatar
0
#include <iostream> using namespace std ; int main (){ int n ,sum; cin >> n ; int aza[100][100]; for (int i=0;i<n;i++){ for(int j=0;j<n;j++){ cin >> aza[i][j]; } } for(int i = 0; i < n; i++){ for(int j = i+1; j < n; j++){ //changed here.Azat Malgazhdar sum += aza[i][j]; } } cout << sum; }
3rd Oct 2020, 2:28 PM
Jayakrishna 🇮🇳
0
Maybe this could help
3rd Oct 2020, 3:39 PM
Prashant Pandey
Prashant Pandey - avatar