Code is not displaying the expected popular value as well as its number of occurence.

Write your question here.
The program should generate random number between 50 to 99,and store them in a 2D array.Check for the most popular value,display the popular value and its number of occurence,if there is a tie then it display the entire values of the 2D array with their respective occurence.Then it changes all even numbers to odd.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include<cstdlib>
#include<ctime>

using namespace std;
int grid[10][8];

void generates(){      //A procedure used to generate random numbers between 50 and 99 for 2D array grid.

int range=99;
int lowest_value=50;
int random=std::rand()%range+lowest_value;

std::srand(std::time(0));

for(int i=0;i<10;i++){
    for(int j=0;j<8;j++){

      random=std::rand()%range+lowest_value;
       grid[i][j]=random;
    }
}
}

bool check(int i,int j){        //A function desgin to check if there is a pair of numbers in the 2D array grid.
if (grid[i][j]==grid[i][j+1]){
    return true;
} else{
return false;
}
}

void most_popular(){       //Check the most popular number in grid.
int total[50],maxi=-1,pos=0;
bool IsTrue=true;

for(int i=0;i<10;i++){
    for(int j=0;j<8;j++){

        IsTrue =check(i,j);
        total[grid[i][j]-50] +=1;     //Stores the the value in 2D array grid in its respective slot in 1d array total
    }                                 //For example if grid[0][4]=56
}                                     //total[56-50] +=1

for(int i=0;i<50;i++){

    if(total[i]>maxi){

        maxi=total[i];
        pos=i;
    }
}
if(!IsTrue){

    std::cout<<"Most popular value is:"<<pos+50 <<" with "<<maxi<<" occurence.";

}else{for(int i=0;i<50;i++){

std::cout<<"Number " << i+50 <<"has "<<total[i]<< "occurence.";
}
}
}

void odd(){
for(int i=0;i<10;i++){
    for(int j=0;j<8;j++){
        if(grid[i][j] % 2==0){
            grid[i][j]++;
        }
    }
}
}

void output(){
for(int i=0;i<10;i++){
    for(int j=0;j<8;j++){
        std::cout<<grid[i][j]<< " ";
    }
    std::cout <<endl;
}
}
    int main()
{
    generates();
  output();
  std::cout <<endl;
  most_popular();
  std::cout<<endl;
  odd();
  output();
    return 0;
}
Last edited on
grid[i][j]=grid[i][j+1]


= is not the same as ==
Last edited on
= is not the same as ==

Oh thanks i haven't seen this mistake.But sadly the issue with the most popular value and its occurence is still here,it doesn't output what is expected and gives a very large number of occurence way bigger than the 2D array's range.
Your array for keeping track of the totals:

int total[50]

What is the starting value of total[0] ? What is the starting value of total[17] ?
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
Thanks alot,however what i meant by tie is to check if the number next to it has the same value,sorry for the confusion.

For the last item in a row if i do an if(j>8){j=8} in check() will it solve the issue?

Also like you said,its true that in generates() the random numbers are between 50 to 148 and not between 50 to 99,you mention "random number generators in <random>instead"is there any link where i can get a tutorial that explain me how to do it?I would be grateful.

For your last paragraph,if i increment count every time total[i]== maxi and reset count to zero if total[i]>maxi how will it check who has the most occurrence because what i intended to do with line 45-52 is to check what value occurred the most and store that value's index in variable pos,and everytime a value with more occurrence appear its value's index will be stored in pos.
I removed maxi completely and used total[pos] instead. I did the check like this:
1
2
3
4
5
6
7
8
    for (int i = 1; i < Range; i++) { // assume that total[0] is max
        if (total[i] > total[pos]) {
            pos = i;
            count = 0;
        } else if (total[i] == total[pos]) {
            ++count;
        }
    }

Hi dhayden,interesting,if i do it this way i wil just have to check in void most_popular if count is less 0 or greater than 0 then code what is necessary.
Topic archived. No new replies allowed.