You didn't ask a question. Nonetheless, I'll comment on the code.
You should always indent your code to match the block structure. Otherwise it's too easy to mess up the blocks.
The numbers 10 and 8 are used as "magic constants" throughout the code. Define them as
1 2
|
constexpr int Rows=10;
constexpr int Cols = 8;
| |
at the top. If you haven't learned about constexpr then do:
1 2
|
const int Rows=10;
const int Cols = 8;
| |
Then use Rows and Cols instead of 10 and 8 throughout the program.
Repeater pointed out that
check()
is incorrect, but it also adds no value. Just use
grid[i][j] == grid[i][j+1]
where you call
check()
. But you don't actually need check() at all. See below.
Don't call
srand()
inside
generates()
. Otherwise calling it twice within the same second with result in the same results. Programs should call srand() only once, inside
main()
.
generates()
creates random numbers from 50 to 148, not 50 to 99. Also, you should ideally use the random number generators in <random> instead, although there's a bit of a learning curve to it. Also, use constexprs for the bounds of the random numbers too.
Line 12 has no effect since random is overwritten the first time through the loop
In
most_popular()
, your logic to decide if there is a single most popular number isn't right. You're basing it on whether two adjacent numbers have the same value. Also when you call check() on the last item in a row, it will access grid[i][j+1] will be out of bounds.
Fortunately, you don't need
check()
at all.
Also, you don't initialize total to zero.
To decide if there is a tie for the max number of items, you need to count the number of times that the max count shows up. That means any time
total[i] == maxi
, you want to increment the count. Just remember to reset the count to zero if
total[i] > maxi