Search code examples
c++for-loopmatrixnested-loopsmin

When processing a matrix, the c++ code, finding in each of its lines, does not write out a line with the minimum amount, but simply the first line


I need to write a code, with instruction: Write a program that finds the row with the minimum amount in the matrix. If there are several such lines, find the first such line. My code:

#include <iostream>
using namespace std;
int main() {
    int n, m, h;
    int arr[100][100];
    cin >> n >> m;
    const int q=m;
    int ar[q];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> arr[i][j];
        }
        
    }
    for (int i = 0; i<n; i++){
        int sum = 0;
        for (int j = 0; j<m; j++){
            sum += arr[i][j];
        }
        for (int y=0; y<q; y++){
            ar[y] = sum;
        }
    }
    int sammin = ar[0];
    for (int i = 0; i<q; i++){
        if (ar[i] < sammin){
            h = i;
        }
    }
    for (int j = 0; j<m;j++){
        cout << arr[h][j] << " ";
    }
    return 0;
}

With input data:

4 5

1 3 2 54 234

75 12 3 46 9

13 26 56 9 12

14 90 897 6 34

The code writes out the first line when it should output a line with 2 index


Solution

  • For starters variable length arrays like that:

    const int q=m;
    int ar[q];
    

    are not a standard C++ feature. Instead you should use standard container std::vector<int>.

    Your matrix has n rows. So the array ar has to had n elements instead of m.

    This inner for loop:

    for (int y=0; y<q; y++){
        ar[y] = sum;
    }
    

    fills all elements of the array ar with the same value of sum in each iteration of the outer loop. Instead of the inner for loop you need just to write:

    arr[i] = sum;
    

    As the array due to the inner loop contains all elements equal to the sum of elements of the last row of the matrix and the sum is maximum for the inputted matrix then the variable h is not being changed within this loop:

    int sammin = ar[0];
    for (int i = 0; i<q; i++){
        if (ar[i] < sammin){
            h = i;
        }
    }
    

    and stays equal to 0.

    To find sums of rows you could use standard algorithm std::accumulate declared in header <numeric>. To find the row with the minimum sum you could use standard algorithm std::min_element declared in header <algorithm>.

    Here is a minimal demonstration program:

    #include <iostream>
    #include <vector>
    #include <iterator>
    #include <algorithm>
    #include <numeric>
    
    int main()
    {
        size_t n, m;
    
        std::cin >> n >> m;
    
        std::vector<std::vector<int>> matrix( n, std::vector<int>( m ) );
    
        for (auto &row : matrix)
        {
            for (auto &item : row)
            {
                std::cin >> item;
            }
        }
    
        std::vector<long long int> ar( n );
    
        for (size_t i = 0; const auto &row : matrix)
        {
            ar[i++] = std::accumulate( std::begin( row ), std::end( row ), 0ll );
        }
    
        auto it = std::min_element( std::begin( ar ), std::end( ar ) );
    
        auto pos = std::distance( std::begin( ar ), it );
    
        for (const auto &item : matrix[pos] )
        {
            std::cout << item << ' ';
        }
        std::cout << '\n';
    }