Thread: small problems .. need help ..

1. small problems .. need help ..

Hi All ..

I'm having small problems with this program ..

The program suppose to print :

1. Main Diagonal Elements and its Sum .

2. Inverse Diagonal Elements and its Sum .

3. Max and Min element and their positions .

4. The Elements Upper the Main Diagonal .

5. The Elements Under the Main Diagonal .

This is the code :

Code:
```#include <iostream>
#include <conio>
#include <iomanip>

int main()
{
int A[5][5]={{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}};
int i , j ,  sum=0 , r_n=0 , c_n =0 , r_m = 0 , c_m = 0 , up=0 , low=0;
int min = A[0][0];
int max = A[0][0];

cout<< "Array Elements Are" << endl;
for (i=0;i<5;i++)
{ for (j=0;j<5;j++)
{ cout<<  setw(5) << A[i][j];
}
cout<< "\n";
}

for (i=0;i<5;i++)
{ for (j=0;j<5;j++)
if (i==j)
{
sum+=A[i][j];
cout<< "Main Diagonal Elements are : " << A[i][j] << endl;
}

}

for (i=0;i<5;i++)
{ for (j=0;j<5;j++)
{ if (A[i][j] > max)
{
max= A[i][j];
r_m= i;
c_m= j;
}
if (A[i][j]< min)
{
min=A[i][j];
r_n=i;
c_n=j;
}
}
}

for (i=0;i<5;i++)
{  for (j=0;j<5;j++)
{     if ((i!=j) && (j > i) )
{
up+=A[i][j];
cout<< "The upper Elements are : " <<  A[i][j]<< endl;
}
{
if (j<i)
{
low+=A[i][j];
cout<< "The Lower Elements are : "<< A[i][j]<< endl;
}
}
}
}

cout<< "\n\nSum of Main Diagonal Elements is : " << sum;
cout<< endl << "The Minimum Element =" << min << " At Row " << (r_n+1) << " Col " << (c_n+1);
cout<< endl << "The Maximum Element =" << max << " At Row " << (r_m+1) << " Col " << (c_m+1);
cout<< "\nSum of Upper Elements : " << up << endl;
cout<< "\nSum of Lower Elements : " << low << endl;

getch();
return 0;
}```
__________________________

The FIRTS PROBLEM is in this loop :

Code:
```for (i=0;i<5;i++)
{ for (j=0;j<5;j++)
if (i==j)
{
sum+=A[i][j];
cout<< "Main Diagonal Elements are : " << A[i][j] << endl;
}

}```
The OUTPUT is :

Code:
```Main Diagonal Elements are : 1
Main Diagonal Elements are : 7
Main Diagonal Elements are : 13
Main Diagonal Elements are : 19
Main Diagonal Elements are : 25```
it will repeat the statements : Main Diagonal ......

How can I fix it ??

it's the same problem in upper , lower Elements ..

I know it's noob problem but I really don't know how to fix it ><" ...

_________________________

The SECOND PROBLEM is I dunno how to print the INVERSE DIAGONAL ...

2. It's because you have the cout statement within the for loop, so each time through it is printing the statement.

3. And for the inverse diagonal I would imagine it's simply a case of reversing the for loops? I'm not an expert so maybe somebody else can explain better.

Maybe something along these lines:

Code:
```for(j = 4; j > 0; j--){
for (i = 0; i < 5; i++){
cout << A[i][j] << " ";
j--;

}
}```

4. The FIRTS PROBLEM is in this loop :
Code:
```for (i=0;i<5;i++)
{ for (j=0;j<5;j++)
if (i==j)
{
sum+=A[i][j];
cout<< "Main Diagonal Elements are : " << A[i][j] << endl;
}

}```
The OUTPUT is :

Code:
```Main Diagonal Elements are : 1
Main Diagonal Elements are : 7
Main Diagonal Elements are : 13
Main Diagonal Elements are : 19
Main Diagonal Elements are : 25```
it will repeat the statements : Main Diagonal ......

How can I fix it ??
The problem is the repeated output of "Main Diagonal"? Then don't put that part of the output in the loop. Put it once before the loop like the prior code that displays all the array elements.

5. THX 4 your Help .. IT worked

still the Inverse Diagonal .. if there is any help !!

6. Originally Posted by Kawaii JoJo
THX 4 your Help .. IT worked

still the Inverse Diagonal .. if there is any help !!
Look at my code above, I put something there that should help. I don't think it's perfect though as i'm learning as well, but it works.