How Would You Find The Number Of Times A Certain Array Element Repeats

I have a program that prints 25 random number between 3 and 7. It also reverses the array after. How would I find how many times the number 5 appears in the array. I have written the function call, prototype, and the definition, but it only prints a 1 or a 0. Does anyone know why?

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
#include <time.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

// Function prototypes
void showArray ( int a[ ], int size ); // shows the array in the format "int a [ ] = { 3, 7, 4, ... ,5, 6, 3, 4, 7 } "
int count5 ( int a[ ], int size ); // returns how many times the number 5 appears in the array
// **************************************************************************************************************************************
int main ()
{
// Array and reverse array
    srand((int)time(NULL));
    int i=0;
    const int SIZE = 25;
    int randvalue[SIZE];


    cout << "Making an array of 25 random integers from 3 to 7!" << endl;

    for(; i < SIZE; i++)
    {
    randvalue[i] = rand () % 5 + 3; // random number between 3 to 7
    }
    cout << "Original array a [ ] = {";
    showArray(randvalue, SIZE);
    cout << "}" << endl;

    int j = SIZE-1;
    i = 0;

    while( i <= j)
    {
        swap(randvalue[i], randvalue[j]);
        i++;
        j--;
    }
    cout << "Reversed array a [ ] = {";
    showArray(randvalue, SIZE);
    cout << "}" << endl;
// *********************************************************************

// Function call for number of times 5 appears
    int return5 = count5 (randvalue, 25);
    cout << "The number 5 appears " << return5 << " times." << endl;
// **********************************************************************



    return 0;
}

// Function definition for ARRAY
void showArray ( int a[ ], int size )
{
    int sum = 0;
    for(int i = 0; i < size; i++)
        cout << a[i];
}


// Function definition for number of 5's
int count5 ( int a[ ], int size )
{
    int count = 0;
    for (int i = 0; i < size; i++) {
        if (a[i] == 5)
        count ++;
        return count;

    }
}
Last edited on
@Austinomical

Move line 69 past the closing right parenthesis on line 71. What's happening, is when a 5 is detected, line 67, count gets updated, but the next command is to return, or exit, the function, way before the i loop gets to the ending.
You only want to return count after going through the entire loop. Right now, you are returning count after a single iteration of the loop (whether a 5 is found or not). You want to go through the entire loop, incrementing count every time you see a 5. Only once you exit the loop should you return.

So move your return statement outside of the for-loop.
@asian7041 Thanks! That worked!
Good to hear :)
Topic archived. No new replies allowed.