Warm tip: This article is reproduced from serverfault.com, please click

Matrix condition not outputting result C++

发布于 2020-11-29 13:05:24

For every column that contains the minimum number of the whole matrix, I want to replace the column entirely with the minimum number.

e.g:

input
8 3 3 7         
2 1 6 1             
8 1 3 1             
9 1 7 1         

///////

output
8 1 3 1 
2 1 6 1 
8 1 3 1 
9 1 7 1 

I came up with this idea:

step 1: find minimum
step 2: create a bool variable and make it false (so active=false)
step 3: go through every column and check if it has the minimum number, if it does -> restart the loop 
and make the bool variable true (so active=true). If active==true, replace the column with the minimum number

Here's the code I wrote:

int main(){
    int i, j, n=4, m=4, v[101][101];
    for(i=0; i<n; i++){
        for(j=0; j<m; j++){
            cin>>v[i][j];
        }
    }
    int minv=1000000000;
    for(i=0; i<n; i++){
        for(j=0; j<m; j++){
            if(v[i][j]<minv){
                minv=v[i][j];
            }
        }
    }
    bool active=false;
    for(j=0; j<m; j++){
        for(i=0; i<n; i++){
            if(v[i][j]==minv){
                i=0;
                active=true;
            }
            if(active){
                v[i][j]=minv;
            }
        }
    }
    for(i=0; i<n; i++){
        for(j=0; j<m; j++){
            cout<<v[i][j]<<" ";
        }
        cout<<endl;
    }
}

My problem is that the compiler doesn't output anything. I assume it's the

for(j=0; j<m; j++){
        for(i=0; i<n; i++){
            if(v[i][j]==minv){
                i=0;
                active=true;
            }
            if(active){
                v[i][j]=minv;
            }
        }
    }

Piece of code. Could you guys help me out?

Questioner
dr 21
Viewed
1
670k 2020-11-30 13:05:43

You asked about the solution in C++. You tend to use static arrays that are more C practice. I took freedom and rewrite your code to a solution that is more of C++ nature.

#include <iostream>
#include <vector>

int main()
{
    std::vector<std::vector<int>> v = {
        {8, 3, 3, 7},
        {2, 1, 6, 1},
        {8, 1, 3, 1},
        {9, 1, 7, 1}};
    std::vector<int> min_row;
    int minv = v[0][0];
    //Here we find min value of matrix
    for (auto i : v)
        for (auto j : i)
            if (j < minv)
                minv = j;
    //Here we find columns that hold min value
    for (auto i : v)
        for (size_t j = 0; j < i.size(); j++)
            if (i.at(j) == minv)
                min_row.push_back(j);
    //After looping trough columns that has
    //min values we change all values to min
    for (auto j : min_row)
        //Notice here we used reference so we could actually change the value
        for (auto &i : v)
            i.at(j) = minv;
    //Here we just print out our matrix
    for (auto i : v)
    {
        for (auto j : i)
            std::cout << j << " ";
        std::cout << std::endl;
    }
    return 0;
}