Написати програму, яка перевіряє, чи є у введеному з клавіатури двовимірному масиві елементи з однаковим значенням
Ответы
Відповідь:
#include <iostream>
using namespace std;
int CountValueInArr(int arr[], int size, int value) {
int count = 0;
for (int i = 0; i < size; i++)
{
count += (arr[i] == value) ? 1 : 0;
}
return count;
}
bool IsTwise(int arr[3][3], int row, int col) {
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
int count = 0;
for (int i = 0; i < row; i++)
{
count += CountValueInArr(arr[i], col, arr[i][j]);
}
if (count >= 2)
{
return true;
}
}
}
return false;
}
void ShowArr(int arr[][3], int row, int col) {
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cout << " " << arr[i][j];
}
cout << endl;
}
}
int main()
{
int arr[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << " >> ";
cin >> arr[i][j];
}
}
ShowArr(arr, 3, 3);
if (IsTwise(arr, 3, 3))
{
cout << "are the same numbers" << endl;
}
else {
cout << "there are no identical numbers" << endl;
}
}