0

I was trying to do the following: Have a matrix, print the entire thing out, print at end of every row the biggest element of said row and print at the bottom of every column the smallest element of said column. I'm pretty much a beginner at C++.

So here's what I've done so far:

 #include <iostream>
    #include <iomanip>
    #define M 50
    #define N 50
    using namespace std;
    int main()
    {
        int m,n;        
        int a[M][N];
        int b[M],c[N];

        do {        
        cout<<"m=";
        cin>>m;
        cout<<endl<<"n=";
        cin>>n;
        cout<<endl;
        }
        while(m!=n);

        for(int i=0;i<m; i++) {
            for(int j=0; j<n; j++){
            cout<<"a["<<i<<"]["<<j<<"]=";
            cin>>a[i][j];
            }
        }
        int max_row;
        max_row=0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (a[i][j] > max_row) {
                    max_row = a[i][j];
                    b[i] = max_row;
                }
            }
        }

        for (int i=0; i<m; i++)
        { for(int j=0; j<n; j++){
            cout<<setw(3)<<a[i][j]<<"\t";
        }
        cout<<"|"<<b[i];
        cout<<endl;
        }
        for(int i=0; i<m; i++){
        cout<<setw(3)<<"-";}

        cout<<endl;

        for(int j=0; j<n; j++)
        {cout<<c[j]<<"\t";
        }

        system("pause");
    }

Most of the time the max_row are the correct ones such as this case:

  3       2       1     |3
  4       6       5     |6
  7       8       9     |9

Other times they get messed up and it goes like this:

  1       2       3     |3
  4      33       6     |33
  7       8       9     |-858993460

I really have no idea what causes it and since there are no error messages I got really confused. Also I have no idea how to make the min column ones. Any help would be appreciated.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Consider adding some comments that label the key parts of your program. At a glance, its hard to tell the purposes of the various loops, what `a`, `b`, and `c` are, etc. – Brian61354270 Feb 16 '20 at 20:13
  • 1
    -858993460 is 0xCCCCCCCC, a [magic value](https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations) signifying uninitialized stack memory, meaning the value `b[2]` was never initialized. You need to debug your program. – rustyx Feb 16 '20 at 20:18

1 Answers1

0

The problem with these loops

    max_row=0;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (a[i][j] > max_row) {
                max_row = a[i][j];
                b[i] = max_row;
            }
        }
    }

is that the value of max_row should be initialized with each iteration of the outer loop. Otherwise all rows after the first row deal with the maximum value of the previous row and in general can not have en element that is greater than the current value of max_row. So the corresponding element of the array b will not be initialized.

Also the user can enter for the matrix negative values in this case your program will output zeroes instead of maximum values.

To find maximum elements in rows and minimum elements in columns it is enough to have one pair of nested loops/

Here is a demonstrative program/

#include <iostream>
#include <iomanip>

int main() 
{
    const size_t N = 3;
    int a[N][N] =
    {
        { 1,  2, 3 },
        { 4, 33, 6 },
        { 7,  8, 9 }
    };

    int b[N], c[N];

    for ( size_t i = 0; i < N; i++ )
    {
        b[i] = a[i][0];
        c[i] = a[0][i];     
        for ( size_t j = 1; j < N; j++ )
        {
            if ( b[i] < a[i][j] ) b[i] = a[i][j];
            if ( a[j][i] < c[i] ) c[i] = a[j][i];
        }
    }

    for ( size_t i = 0; i < N; i++ )
    {
        for ( size_t j = 0; j < N; j++ )
        {
            std::cout << std::setw( 3 ) << a[i][j] << '\t';
        }
        std::cout << '|' << b[i] << '\n';
    }

    for ( size_t i = 0; i < N; i++ )
    {
        std::cout << std::setw( 3 ) << '-' << '\t';
    }
    std::cout << '\n';

    for ( size_t i = 0; i < N; i++ )
    {
        std::cout << std::setw( 3 ) << c[i] << '\t';
    }
    std::cout << '\n';

    return 0;
}

Its output is

  1   2   3 |3
  4  33   6 |33
  7   8   9 |9
  -   -   - 
  1   2   3 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335