#include <iostream>
using namespace std;
int main (){
int workspace[3][3]={{41,42,43},{42,46,47},{49,50,51}};
int most_frequent_number = 0;
int real_count_number = 0;
for (int i = 0; i <3; ++i)
{
for (int j = 0; j < 3; ++j)
{
int test_number = workspace[i][j];
int count_number = 0;
for (int p = 0; p < 3; ++p)
{
for (int l = 0; l < 3; ++l)
{
if (workspace[p][l] == test_number)
++count_number;
if (count_number > real_count_number)
most_frequent_number = test_number;
real_count_number = count_number;
if (count_number == real_count_number && most_frequent_number < workspace[i][j])
most_frequent_number = test_number;
real_count_number = count_number;
}
}
}
}
cout<<most_frequent_number;
}
I am working on a homework that I need to find out the most frequent number in the 2D array. However for this array I can't get the answer of 42 , instead I get 51 as the answer. Can anyone tell me where I do wrongly?