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!
#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=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...
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,
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??