T
T
tatarantino2017-05-16 01:52:15
Arrays
tatarantino, 2017-05-16 01:52:15

How to display the row number of a matrix?

The essence of the task is to swap the string specified from the keyboard with the string with the minimum element in the array.
It is necessary that the variable "y" takes the values ​​of the "i-th" element of the matrix. It seems that everything was done like that, but it still does not work. The row number, i.e. "y" takes a different value
Here is the code:

#include <iostream>
#include <iomanip>
using namespace std; 
int main()
{   setlocale(LC_ALL,"Russian") ;
    const int str = 3;
    const int sto = 3;
    int min = 0;
    int matrix[str][sto];
    
    cout << "Введите элементы матрицы: " << endl;
    for (int i = 0; i < str; i++)
        for (int j = 0; j < sto; j++)
        cin >> matrix[i][j];
           
   
    cout << " \n";
    cout << "Ваша матрица: " << endl;
     cout << "\n";
    for (int i = 0; i < str; i++) 
    {
        for (int j = 0; j < sto; j++)
           
            cout << setw (4) <<matrix[i][j];
        cout << endl;
    }
 
    int y;
    min = matrix[0][0]; 
    for (int i = 0; i < str; i++) 
    {
        for (int j = 0; j < sto; j++) 
        {
            if (matrix[i][j] < min && matrix[i][j]>=0)
               min = matrix[i][j]; 
               y=i;
               y++;
                
        }
        
    }
    
    cout << "Минимальное значение: "<< min;
    cout << "\n";
    cout << "Cтрока с минимальным значением: "<< y;
    cout << "\n";
    
    
    
  int x;
  cout << endl;
  cout <<"Введите строку, которую нужно поменять: \n";
  cin >> x;
  
  
  x--;
  y--;
  
   
  
  for (int i=0; i < str; i++) 
      for (int j = 0; j < sto; j++) 
      {
          if (i == x)
          {
              int z = matrix[i][j];
              matrix[i][j] = matrix[y][j];
              matrix[y][j] = z;
          }
      }
  cout << "\n"; 
  cout << "Результат: \n";
  cout << endl;
 
  for (int i = 0; i < str; ++i) {
    for (int j = 0; j < sto; ++j) {
      cout << setw (4) << matrix[i][j];
    }
    cout << endl;
  }

    
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question