A
A
Archpriest Metapop2020-10-08 03:16:15
Project management
Archpriest Metapop, 2020-10-08 03:16:15

A simple C++ program doesn't work correctly. How to fix?

As planned, the program should take the row number in a double square array and compare the row with the column with the corresponding number, but something went wrong.

#include <iostream>
using namespace std;
int main() {
const int n = 1000;
int a,b, i, j;
bool str= false;
float arr[n][n];
cout << "Введите размерность:\n";
cin >> a;
cout << "Заполнение массива:\n";

for (int i = 0; i < a; i++) {
    for (int j = 0; j < a; j++) {
        cout <<"Введите элемент ["<<i+1<<","<<j+1<<"]:";
        cin >> arr[i][j];
        }
    }
for (int i = 0; i < a; i++) {
    for (int j = 0; j < a; j++)
        cout << arr[i][j]<<" ";
        cout << "\n";
        }
cout << "Введите номер строки: \n";
cin >> b;
    for (int i = 0; i < a; i++) {
        for (int j = 0; j < a; j++)
            if (arr[b-1][j] == arr[i][b-1]) str = true;

    }
    if (str == false) cout <<"Строка и столбец не совпадают";
    if (str == true) cout <<"Строка и столбец совпадают";
}

What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2020-10-08
@caramel14

Corrected the answer. Replaced your double loop with i,j in row-to-column comparison with a single loop. Is the matrix square?

...
bool str= true;
...
for (int i = 0; i < a; i++) {
    if (arr[i][b-1] != arr[b-1][i]) str = false;
}
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question