random numbers

i want to create a function that will loop 10 times and see how many of each number occurred but i think im missing something for my code will not compile but i don't know what?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iomanip>
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
    {
	
        const int n=5,ntimes=10000;
        int freq[n]={0};
        short int rndseq[]={19,50,78,161,252};
    
        retrand(freq,n,ntimes);
    
    cout<<rndseq[i]<<" occured "<<
    freq[i]<<" times"<<endl   

}


What does your retrand() function look like?

[edit] BTW, you can use the count() STL algorithm to count the number of times a number appears in a sequence.
1
2
3
4
5
6
7
8
#include <algorithm>
...

int random_sequence[ 10 ] = { 19, -7, 2, 38, -7, 5, 10024, 2, 5, 9 };

cout << "-7 appears "
     << count( random_sequence, random_sequence +10, -7 )
     << " times\n";
Last edited on
hmmm its still not working for me?

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <iomanip>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <conio.h>
#include <cstdlib>
#include <ctime>
using namespace std;
void Menu();
int getN();
void def(int);
void problem1();
void problem2();
void multiple();
void retrand();
void problem5();
void problem6();
void problem7();
void multiple();    
    int main(int argv,char *argc[])
    {
	int inN;
        do{
         Menu();
         inN=getN();
         switch(inN){
          case 1:    problem1();break;
          case 2:    problem2();break;
          case 3:    multiple();break;
          case 4:    retrand();break;
          case 5:    problem5();break;
          case 6:    problem6();break;
          case 7:    problem7();break;
          default:   def(inN);}
        }while(inN<8);
        return 1;
    }
    void Menu()
    {
           cout<<"Type 1 for problem 1"<<endl;
           cout<<"Type 2 for problem 2"<<endl;
           cout<<"Type 3 for problem 3"<<endl;
           cout<<"Type 4 for problem 4"<<endl;
           cout<<"Type 5 for problem 5"<<endl;
           cout<<"Type 6 for problem 6"<<endl;
           cout<<"Type 7 for problem 6"<<endl;
           cout<<"Type 8 to exit \n"<<endl;
    }
    int getN()
    {
           int inN;
		   cin>>inN;
           return inN;
    }
    void problem1()////////////////////////
    {
     
  	  
    void problem1 ();
     unsigned short n,s=0;
    cout<<"please enter a number ";
    scanf ("%d",&n);
    while (n!= 0)
    {
        s = (10 * s) + n % 10;
        n = n / 10;
    }
    printf ("The Reversed Number is %d",s);
    getch ();  
        system("CLS");	
        
	 		
    }
    void problem2()/////////////////////////
    {
    int hidenum,guess,stop = 0;
    char choice;
    srand(time(NULL));
    hidenum = rand() % 1000 + 1;
     while(hidenum != guess)
     {
          cout<<"guess a number from 1 to 1000: "<<endl;
          cin >>guess; 
    if(hidenum < guess) 
    {
           cout<<"The secret number is lower"<<endl;
           stop++;
    } 
    if(hidenum > guess)
    {
           cout<<"the secret number is higher"<<endl;
           stop++;
    }
    if(hidenum == guess)
    {
           cout<<"you did it you won!!!"<<endl;
           system("pause");
           cout<<"would you like to play agian y for yes n for no?"<<endl;
           cin >>choice; 
              if(choice == 'Y'|| choice == 'y')    
                  {
                      cout<<"yahh thats the spirit!!"<<endl;
                      system("pause");
                      system("CLS");
                      return  problem2();
                  }
              if (choice == 'N'|| choice == 'n')    
                 {
                      cout<<"noooooo.. you lose!!"<<endl;
                      system("pause");
                      system("CLS");
                      return  Menu();
                 }
     }
     if(stop > 9)
     {
           cout<<"you failed to find the number"<<endl;
           cout<<"would you like to play agian y for yes n for no?"<<endl;
           system("pause");
           cin >>choice;
              if(choice == 'Y'|| choice == 'y')    
                  {
                      cout<<"yahh thats the spirit!!"<<endl;
                      system("pause");
                      system("CLS");
                      return  problem2();
                  }
              if (choice == 'N'|| choice == 'n')    
                  {
                      cout<<"noooooo.. you lose!!"<<endl;
                      system("pause");
                      system("CLS");
                      return  Menu();
                  }  
     }
    }
    }
    void multiple(int x, int y)
    {
    
   {
     if(y%x == 0)
     system("pause");
     return Menu();
     
   }
    
    }
    void retrand()////////////////////////
    {
	
        const int n=5,ntimes=10000;
        int freq[n]={0};
        short int rndseq[]={19,50,78,161,252};
    
        retrand(freq,n,ntimes);
    
    cout<<rndseq[i]<<" occured "<<
    freq[i]<<" times"<<endl;
    
    
    }
    void problem5()////////////////////////////
    {	
    }
    void problem6()
	{	   	   
   	}
    void problem7()
	{	   
	}
	void def(int inN)
    {
     
      {
           cout<<"You typed "<<inN<<" to exit the program"<<endl;
      }
    
    }
    void multiple()
    {
    
    int multiple(int,int);
    int a,b,ans;
      cout<<endl;
       cout<<"Enter two numbers : ";
       cin>>a>>b;
       ans=multiple(a,b);
    if(ans==0)
       cout<<b<<" is a multiple of "<<a;
    else
       cout<<b<<" is not a multiple of "<<a;
    
    cout<<endl;
    
    }

    void retrand()
    {
          
    short int rndseq[ 10000 ] = { 19,50,78,161,252 };

     cout << "50 appears "
     << count( rndseq[i], freq[i] +10000, 50 )
     << " times\n";

     cout << "78 appears "
     << count( rndseq[i], freq[i] +10000, 78 )
     << " times\n";   

    cout << "161 appears "
     << count( rndseq[i], freq[i] +10000, 161 )
     << " times\n";

    cout << "252 appears "
     << count( rndseq[i], freq[i] +10000, 252 )
     << " times\n";

  
    }

You have two times the function retrand() with different contents...
Also line 156: retrand(freq,n,ntimes); there is no overloaded function of retrand that takes 3 arguments...

Also int the multiple() function you have int multiple(int,int); But the only multiple(int, int) you have is void type, so change it to void multiple(int,int);
Last edited on
well i got all the bugs out but problem four still gives me 0 for how many times


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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <iomanip>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <conio.h>
#include <cstdlib>
#include <ctime>
using namespace std;
void Menu();
int getN();
void def(int);
void problem1();
void problem2();
void multiple();
void problem4();
void problem5();
void problem6();
void problem7();
void multiple();    
    int main(int argv,char *argc[])
    {
	int inN;
        do{
         Menu();
         inN=getN();
         switch(inN){
          case 1:    problem1();break;
          case 2:    problem2();break;
          case 3:    multiple();break;
          case 4:    problem4();break;
          case 5:    problem5();break;
          case 6:    problem6();break;
          case 7:    problem7();break;
          default:   def(inN);}
        }while(inN<8);
        return 1;
    }
    void Menu()
    {
           cout<<"Type 1 for problem 1"<<endl;
           cout<<"Type 2 for problem 2"<<endl;
           cout<<"Type 3 for problem 3"<<endl;
           cout<<"Type 4 for problem 4"<<endl;
           cout<<"Type 5 for problem 5"<<endl;
           cout<<"Type 6 for problem 6"<<endl;
           cout<<"Type 7 for problem 6"<<endl;
           cout<<"Type 8 to exit \n"<<endl;
    }
    int getN()
    {
           int inN;
		   cin>>inN;
           return inN;
    }
    void problem1()////////////////////////
    {
     
  	  
    void problem1 ();
     unsigned short n,s=0;
    cout<<"please enter a number ";
    scanf ("%d",&n);
    while (n!= 0)
    {
        s = (10 * s) + n % 10;
        n = n / 10;
    }
    printf ("The Reversed Number is %d",s);
    getch ();  
        system("CLS");	
        
	 		
    }
    void problem2()/////////////////////////
    {
    int hidenum,guess,stop = 0;
    char choice;
    srand(time(NULL));
    hidenum = rand() % 1000 + 1;
     while(hidenum != guess)
     {
          cout<<"guess a number from 1 to 1000: "<<endl;
          cin >>guess; 
    if(hidenum < guess) 
    {
           cout<<"The secret number is lower"<<endl;
           stop++;
    } 
    if(hidenum > guess)
    {
           cout<<"the secret number is higher"<<endl;
           stop++;
    }
    if(hidenum == guess)
    {
           cout<<"you did it you won!!!"<<endl;
           system("pause");
           cout<<"would you like to play agian y for yes n for no?"<<endl;
           cin >>choice; 
              if(choice == 'Y'|| choice == 'y')    
                  {
                      cout<<"yahh thats the spirit!!"<<endl;
                      system("pause");
                      system("CLS");
                      return  problem2();
                  }
              if (choice == 'N'|| choice == 'n')    
                 {
                      cout<<"noooooo.. you lose!!"<<endl;
                      system("pause");
                      system("CLS");
                      return  Menu();
                 }
     }
     if(stop > 9)
     {
           cout<<"you failed to find the number"<<endl;
           cout<<"would you like to play agian y for yes n for no?"<<endl;
           system("pause");
           cin >>choice;
              if(choice == 'Y'|| choice == 'y')    
                  {
                      cout<<"yahh thats the spirit!!"<<endl;
                      system("pause");
                      system("CLS");
                      return  problem2();
                  }
              if (choice == 'N'|| choice == 'n')    
                  {
                      cout<<"noooooo.. you lose!!"<<endl;
                      system("pause");
                      system("CLS");
                      return  Menu();
                  }  
     }
    }
    }
    void multiple(int x, int y)
    {
    
   {
     if(y%x == 0)
     system("pause");
     return Menu();
     
   }
    
    }
    void problem4()////////////////////////
    {
	  
 
   int random_sequence[ 10000 ] = { 19, 50, 78, 161, 252};

    
   cout << "19 appears "
     << count( random_sequence, random_sequence +10000, -19 )
     << " times\n";
   
   cout << "50 appears "
     << count( random_sequence, random_sequence +10000, -50 )
     << " times\n";
   
   cout << "78 appears "
     << count( random_sequence, random_sequence +10000, -78 )
     << " times\n";
   
   cout << "161 appears "
     << count( random_sequence, random_sequence +10000, -161 )
     << " times\n";
    
    cout << "252 appears "
     << count( random_sequence, random_sequence +10000, -252 )
     << " times\n";
    
    
    
    }
    void problem5()////////////////////////////
    {	
    
    }
    void problem6()
	{	   	   
   	}
    void problem7()
	{	   
	}
	void def(int inN)
    {
     
      {
           cout<<"You typed "<<inN<<" to exit the program"<<endl;
      }
    
    }
    void multiple()
    {
    
    int multiple(int,int);
    int a,b,ans;
      cout<<endl;
       cout<<"Enter two numbers : ";
       cin>>a>>b;
       ans=multiple(a,b);
    if(ans==0)
       cout<<b<<" is a multiple of "<<a;
    else
       cout<<b<<" is not a multiple of "<<a;
    
    cout<<endl;
    
    }

That's true...
You are looking for numbers: -19, -50, -78, -161, -252.
But you array has: 19, 50, 78, 161, 252.

In Duoas's example he has "-7" because in his array he has a value of -7...


Also in your void multiple() function you have int multiple(int,int); but the only multiple(int, int) you have is void... you can't have it there as int... Also you cann't return something from a void function. And more another function return Menu();. I'm wondering how it compiles without errors...
i want to create a function that will loop 10 times and see how many of each number occurred but i think im missing something for my code will not compile but i don't know what?


1
2
3
4
5
6
7
map<int,int> mList;

int newRandom = getRandomNumber();

mList[newRandom] += 1; // It defaults to 0

// Now, u can iterator through mList and get all the entries. 
Topic archived. No new replies allowed.