How do I print the line that is the smallest element of an array?

Asked

Viewed 22 times

0

#include <iostream>

using namespace std;

int main()
{
    int m[4][4],i,j,menor;
    for(i=0;i<4;i++){
        for(j=0;j<4;j++){
           cin>>m[i][j];
           if (i == 0 and j == 0){
               menor=m[0][0];
           }
           if(m[i][j]<menor){
            menor=m[i][j];
           }
        }
    }

    cout<<"O menor numero e:"<<menor<<endl;
    return 0;
}

1 answer

0

If I understand your question correctly, you want the row number of the entered value and not the matrix. For both forms, you can monitor the inserts (variable insercoes) and save the value of the minor insert into a variable (in this case linhaDoMenorNumero) or update the variable with the current row index of its matrix (variable i):

int m[4][4], i, j, menor, linhaDoMenorNumero, insercoes = 0;

for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
       cin >> m[i][j];
       insercoes++;

       if (i == 0 and j == 0) {
           menor = m[0][0];
       }

       if(m[i][j] < menor) {
        // para encontrar a linha da matriz, basta substituir "insercoes" por "i"
        linhaDoMenorNumero = insercoes;
        menor = m[i][j];
       }
    }
}

cout << "O menor numero e: " << menor << endl;
cout << "Na linha: " << linhaDoMenorNumero << endl;
return 0;

I hope I’ve helped in some way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.