i've made this prog but it is not printing the correct output. plzzz help
#include%26lt;iostream.h%26gt;
#include%26lt;conio.h%26gt;
void main()
{
clrscr();
int a[3][3],max,min;
cout%26lt;%26lt;"enter the matrix: ";
for(int i=0;i%26lt;3;i++)
{ for(int j=0;j%26lt;3;j++)
{ cin%26gt;%26gt;a[i][i]; }
}
max=a[0][0];
min=a[0][0];
for(i=0;i%26lt;3;i++)
{ for(int j=0;j%26lt;3;j++)
{ if(a[i][j]%26lt;min)
min=a[i][j];
if(a[i][j]%26gt;max)
max=a[i][j];
}
}
cout%26lt;%26lt;"the max character is: "%26lt;%26lt;max%26lt;%26lt;"\m"%26lt;%26lt;"the min character is: "%26lt;%26lt;min;
getch();
}
Program in C++ to create a 3 by 3 array and extract the maximum and the maximum alongwith the original matrix
the line
{cin %26gt;%26gt;a[i][i];}
should read
{cin %26gt;%26gt;a[i][j];}
In the part where you are entering the matrix. Otherwise you are just reading into the diagonals of the matrix and leaving the off-diagonals empty.
Reply:You need to print out the matrix, but I don't see that anywhere.
Within your two nested for loops, you need to add a cout statement (or two.) The first would be immediately after the second for and would be something like:
cout %26lt;%26lt; " " %26lt;%26lt; a[i][j];
the next would be between the two braces after the max= line and it would look like:
cout %26lt;%26lt; endl;
This prints the item in each matrix position while you're searching for the largest number.
Also, initialize max and min to 0, not a[0][0]
It looks like you're reading data into a[i][i]... that should be a[i][j], right?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment