program to add Elements above and below the main diagonal in a matrix

This is my program written to read, print a martix into it and then print the sum of the elements above & below the main diagonal.
I hope this is correct. Please help me!

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
34
35
36
37
38
39
40
41
42
43
44
#include<iostream.h>
void main()
{
	int a=0,b=0,i,j,size;
	int A[10][10];				//initialising matrix A

	cout<<"Enter side of square of Matrix A\n";
	cin>>size;

	cout<<"Enter Values into Matrix A of side(s) "<<size<<"\n";
	
	for(i=0; i<size; i++)			// Input Matrix A
	{
		for(j=0; j<size; j++)
		{
			cin>>A[i][j];
		}
	}
	
	cout<<"\nThe Given Matrix is\n"; // Output MAtrix A
	for(i=0; i<size; i++)
	{
		cout<<"\n";
		for(j=0; j<size; j++)
		{
			cout<<A[i][j];
		}
	}
	cout<<"\n";

	for(i=0; i<size; ++i)			// Loop to add elemnts below main diagonal
		for(j=0; j<size; ++j)
			if(i>j)
				a = a + A[i][j];

	for(i=0; i<size; ++i)			// Loop to add elements above main diagonal
		for(j=0; j<size; ++j)
			if(i<j)
				b = b + A[i][j];

	cout<<"\nSum of elements above the main diagonal is : "<<b;
	cout<<"\nSum of elements below the main diagonal is : "<<a<<endl;
}


if this program is correct then also tell me how to do the same program in a "Do - While" and "While" Loops.

And please tell me if there can be other variations to this program / programming methadology / logic / method and any other help will be appreciated highly.
I hope this helps

for(i=0; i<size; i++){

process

}


IS THE SAME THAN:


i=0; //Always make it 0 if you are using a while
do{


i++;
} while (i<size);
i=0; //Do it again so if a process needs it, it will be 0 again.


OR THE SAME THAN:

i=0; //Always make it 0 if you are using a while
while(i<size){

process

i++;
}
i=0; //Do it again so if a process needs it, it will be 0 again.

and a sub process

i=0;//Always make it 0 if you are using a while
j=0;
do{

do{
process
j++;

}while(j<size);
j=0; //Make it 0 so when i incresases it will start using j from 0 again

}while(i<size);
i=0; //Do it again so if a process needs it, it will be 0 again.




But using a for is always better... and also try to use functions to divide your program, this will help you a lot... and might reduce the size of your code...

But in general your process is fine, considering it is a simple algorithm you are using tabs in a good way.

I'd also recommed switching to codeblocks

And if you don't strictly need to input the values for the matrix, you could use a random generator so you don't waste your tame inputing 100 numbers... :D that can be done with stdlib.h and A[i][j]=rand()%yourmaximumvalue; insted of cin>>A[i][j]; where your maximum value is the maximum random number, ex: 10, will give you values from 0-9, 1+rand()%10; will give you from 1-10, etc...

have fun...
Last edited on
1. To find the sums we need to iterate only over the relevant values of j ( j>i or j<1).
2. We can compute the sums as we are reading in the values. For example,

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
34
int main()
{
    enum { N = 10 } ;
    int matrix[N][N] ;

    std::cout << "size of square matrix? "  ;
    int size ;
    if( std::cin >> size && size > 0 && size <= N )
    {
        int sum_above_diagonal = 0 ;
        int sum_below_diagonal = 0 ;
        int sum_on_diagonal = 0 ;

        for( int i = 0 ; i<size ; ++i )
        {
            for( int j = 0 ; j<i ; ++j )
            {
                std::cin >> matrix[i][j] ;
                sum_above_diagonal += matrix[i][j] ;
            }

            std::cin >> matrix[i][i] ;
            sum_on_diagonal += matrix[i][i] ;

            for( int j = i+1; j<size ; ++j )
            {
                std::cin >> matrix[i][j] ;
                sum_below_diagonal += matrix[i][j] ;
            }
           
            // print out the sums
        }
    }
}

Thanx danoc 93. appreciate your tym and info with which u provided me!!

and also JLBorges for writing a program for that. may I ask the reason why you have used " std:: " everywhere I mean I know its in Visual c++ latest version but why not include " namespace std ". Also why is this new addition of std:: I mean for what purpose!
Also JLBorges the reason ur program looks smaller than mine is because u have nested the entire section into a main loop whereas i didnt. Thanks for the idea anyway!

And danoc93... what if i do this program using the Call by Value or Call by Refernce methods of Function??
Topic archived. No new replies allowed.