С использованием указателей:
#include
void replaceZeroRows(int Q[3][3]) {
for (int i = 0; i < 3; ++i) {
if (Q[i][i] == 0) {
int* row = Q[i];
for (int j = 0; j < 3; ++j) {
row[j] = 1;
}
}
}
}
int main() {
int Q[3][3] = {
{0, 2, 3},
{4, 0, 6},
{7, 8, 0}
};
std::cout << "Initial matrix Q:" << std::endl;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
std::cout << Q[i][j] << " ";
}
std::cout << std::endl;
}
replaceZeroRows(Q);
std::cout << "\nMatrix Q after replacing zero rows:" << std::endl;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
std::cout << Q[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
Без указателей:
#include
void replaceZeroRows(int Q[3][3]) {
for (int i = 0; i < 3; ++i) {
if (Q[i][i] == 0) {
for (int j = 0; j < 3; ++j) {
Q[i][j] = 1;
}
}
}
}
int main() {
int Q[3][3] = {
{0, 2, 3},
{4, 0, 6},
{7, 8, 0}
};
std::cout << "Initial matrix Q:" << std::endl;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
std::cout << Q[i][j] << " ";
}
std::cout << std::endl;
}
replaceZeroRows(Q);
std::cout << "\nMatrix Q after replacing zero rows:" << std::endl;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
std::cout << Q[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}