Matrix problem

Hello!
My c++ code is not working properly, and i have no ide what is wrong.

This is the problem.

You have square matrix size 9×9.

The part of the problem is: You check every element from upper triangle of matrix (the diagonal is not included), and if it's greater than every element from lower triangle of matrix(the diagonal is not included), you add it to a new sum. So, the new sum is the sum of all elements from upper triangle of matrix, which are greater than every element from lower triangle of matrix. This is my code;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  
 int i,j,k,l;
    int S=0;

    int b[10][10];

    for (i=1; i<=9; i++){
            cout <<" Insert elements from  "<<i<<". row"<< endl ;
            for(j=1; j<=9;j++){
               cin >> b[i][j];

            }

    }
  for (i=1; i<=8;i++){
   for(j=i+1; j<=9;j++){
        for(k=2; k<=9; k++){
            for(l=1; l<=k-1; l++){
            if( b[i][j]>b[k][l]){
                S=S+b[i][j];
            } else {S=S+0;}

            }
        }

   }
 }


 cout <<" SUM S"<<endl;

 cout <<S;
Last edited on

This is the problem.

You have square matrix size 9×9.

Then why have you defined your matrix with a size of 10 by 10?

Arrays in C/C++ start at zero and end at size - 1. You need to get used to this fact and not try to force the language to work the way you think it should.

Next until you have the logic fixed I recommend you start with a matrix that contains fixed data, instead of asking the user to fill in the array.

Next in the following snippet the else statement is not actually doing anything. Is that what you intend? If it is then why not just eliminate that else clause.

1
2
3
            if( b[i][j]>b[k][l]){
                S=S+b[i][j];
            } else {S=S+0;}


As it stands now you need to show what you inputted into the program, the output the program is producing, and what exactly you think the output should be with the input provided.
If you find the highest value in your lower triangle while the numbers are going in, you can store it in a variable, and compare the upper triangle values with that, instead of redoing the same loop. . .

I assume the "upper triangle" (excluding the diagonal) consists of row 1, columns 1 to 8, row 2, columns 1 to 7, etc.

If so, then your loop can be something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
int max = 0, matrix = 9; // so sometime in the future we can make the matrix a different size. . .

for (conditions and inputs)
{ 
	// store input to matrix element;
	//if  the current row > 1 && current column > matrix size + 2 - current row && current input value > max)
		//max = current input value
}

int rows = matrix -1 //Actually we're wasting row 0 and column 0 here,
				//but for the sake of example:
// scan each row from 1 through 8 (matrix - 1)
{ 
	//Each row length is equal to matrix size - row number
	// for (int col = 1 ; col < matrix - row; col++)
	{
		// if the value at current location is greater than max:
			// add value to sum
	{
}
Last edited on
Topic archived. No new replies allowed.