for (i = 0; i <= n; i++){
for (j = 0; j <= m; j++){
if (a[i][j] > a[0][0]){
a[0][0] = a[i][j];
row = i;
column = j;
}
}
}
cout << "Highest value is row " << row << " and column " << column;
for (i = 0; i <= n; i++){
for (j = 0; j <= m; j++){
if (a[0][0] > a[i][j]){
a[0][0] = a[i][j];
row = i;
column = j;
}
}
}
cout << "\nLowest value is row " << row << " and column " << column;
Sample Output:
1 2 3
4 5 6
7 8 9
Highest value is row 3 column 3
Lowest value is row 1 column 1
My output:
1 2 3
4 5 6
7 8 9
Highest value is row 3 column 3
Lowest value is row 0 column 1//this part is always fixed no matter the place is
the lowest goes. how to fix this? thank you
What you have said is the same in my code... and a[lowest] is the same as a[0].. it has the same logic in it and i tried what you did and it's still the same... :)
int main()
{
int a[10][10], n, m, i, j, s, tr = 0, row, column;
cout << "Enter order of matrix: ";
cin >> n >> m;
//input
cout << "\n\nEnter " << n << " x " << m << " matrix\n\n";
for (i = 1; i <= n; i++){
for (j = 1; j <= m; j++)
cin >> a[i][j];
}
//given matrix
cout << " Given matrix: \t\t\t row sum \r\n";
for (i = 1; i <= n; i++){
s = 0;
for (j = 1; j <= m; j++){
cout << setw(10) << a[i][j];
s += a[i][j];
}
cout << setw(15) << s;
cout << "\n";
}
//col sum
cout << "Col Sum:\n";
for (j = 1; j <= n; j++)
{
s = 0;
for (i = 1; i <= m; i++){
s += a[i][j];
}
cout << setw(15) << s;
cout << "\n";
}
//trace
cout << "Trace:\n";
for (i = 1; i <= m; i++){
for (j = 1; j <= n; j++){
if (i == j){
tr += a[i][j];
}
}
}
cout << setw(15) << tr;
if (m != n){
cout << "\nNot a perfect matrix!\n";
}
{
int flag;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
if (a[i][j] != 1 && a[j][i] != 0)
{
flag = 0;
break;
}
}
}
if (flag == 1)
cout << "\nIt is identity matrix \n";
else
cout << "\nIt is not an identity matrix \n";
}
for (i = 0; i <= n; i++){
for (j = 0; j <= m; j++){
if (a[i][j] > a[0][0]){
a[0][0] = a[i][j];
row = i;
column = j;
}
}
}
cout << "Highest value is row " << row << " and column " << column;
for (i = 0; i <= n; i++){
for (j = 0; j <= m; j++){
if (a[0][0] > a[i][j]){
a[0][0] = a[i][j];
row = i;
column = j;
}
}
}
cout << "\nLowest value is row " << row << " and column " << column;
system("pause>0");
return 0;
}
I still don't understand TT_TT I'm kinda new in this kind of thing so it's so hard. i crie
ok one thing to note is that you're correctly starting your indexing at 1 in all the calculations except the highest and lowest checks. There you start at 0, which will throw off your results.
1 2 3 4 5 6 7 8 9
for (i = 0; i <= n; i++){
for (j = 0; j <= m; j++){
if (a[i][j] > a[0][0]){
a[0][0] = a[i][j];
row = i;
column = j;
}
}
}
The above should look like:
1 2 3 4 5 6 7 8 9
for (i = 1; i <= n; i++){
for (j = 1; j <= m; j++){
if (a[i][j] > a[0][0]){
a[0][0] = a[i][j];
row = i;
column = j;
}
}
}
if you guys read his full code, you'll see what he's using a[0][0] for, and it does work, though it's a pretty strange way to go about it. the only problem that was stopping the code from working was his starting the lowest and highest searches with indexes 0 instead of 1.
if you guys read his full code, you'll see what he's using a[0][0] for, and it does work
No, it only works by accident. Since he isn't initializing a[0][0] it could contain anything. For example, if you add a[0][0] = 100; immediately after defining a, then the output I get is:
Given matrix: row sum
1 2 3 6
4 5 6 15
7 8 9 24
Col Sum:
12
15
18
Trace:
15
It is not an identity matrix
Highest value is row 2 and column 2293040
Lowest value is row 1 and column 1
Also he needs to initialize flag.
Also the indices at lines 49 & 51 are wrong: they should start from 1.
Also the indices for trace are wrong: i should go to n and j should go to m, not the other way around.