Searching an array

Ok, this is a homework question but I am not asking for someone to write my program. I just need a little help. This program is giving the user 20 random numbers and then one more random number. The first 20 are placed in an array and you must search through the array to tell you how many times the last random number is repeated. When I compile it gives me the wrong number for the amount of times it is repeating. Can someone give me some advice? Here's my code:


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
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;



void getRandom(int randomArray[], int n);
void printInts(int randomArray[], int n);


int main()


{


int randomArray[20];
int ranNum;
int index;
int counter;

getRandom(randomArray, 20);
printInts(randomArray, 20);

srand(static_cast<unsigned int>(time(0)));
ranNum = (rand( ) % (10 - 1 + 1)) + 1;



cout << "Your last random number is " << ranNum <<endl;

counter == 0;

for(int index=0; index<20; index++)

{

if(ranNum == randomArray[index])

{
           counter == counter++;
}
}

if (counter > 0)

{
cout << "Your last random number repeats " << counter << " times" <<endl;
}


else

{
             cout << "Your random number did not occur at all" <<endl;
}


//Pause to read output
system ("pause");


return 0;

}


void getRandom(int randomArray[], int n)

{


for(int i=0; i<20; i++)

{
        srand(static_cast<unsigned int>(time(0)));
randomArray[i] = (rand( ) % (10 - 1 + 1)) + 1;

}
}


void printInts(int randomArray[], int n)

{
     cout << "Your first 20 random numbers are " <<endl;
     
for(int i=0; i<20; i++)

{
        randomArray[i] = (rand( ) % (10 - 1 + 1)) + 1;

cout << randomArray[i] <<endl;
}
}



Last edited on
I'm a novice c++ programmer myself, but shouldn't it be 'counter = 0' and 'counter++' for lines 34 and 43?

Also: why rand( ) % (10 - 1 + 1)) + 1 when you could just go rand()%10 + 1?
You should also only seed the random number generator once.

Currently, you're seeding it 20 times in a loop inside the getRandom function.
counter == 0;

this is comparison not assignment..
make it.
counter = 0;
I have 3 words for you: std::count in http://www.cplusplus.com/reference/algorithm/count/
Instead of the assignment operator you are using the comparision operator. So this

counter == 0;

is not an assignmnet 0 to counter. It is a comparision counter with 0. So you shhould write

counter = 0;

Again this is not an assignment

counter == counter++;

Use simply

counter++;

Also you have declared functions as haveing two parameters

void getRandom(int randomArray[], int n);
void printInts(int randomArray[], int n);


However inside their bodies instead of using parameter n you are using mafic number 20. You should use n insetad of 20 in loops in these functions.

Thanks everyone. Finally got it up and going.
Topic archived. No new replies allowed.