Creating a frequency distribution

Hello,
my homework assignment has me create a program that finds all numbers in an array that show up exactly 5 times. I am trying to solve this issue by making a frequency distribution via two loops and two arrays, but I am having trouble getting my loop to not recount a number it has already counted.

For example, if you enter ten 1’s into the “entered Numbers” array I want it to store a count of 10 in frequencyarray[1].
Instead it is storing
frequencyarray[0]10
frequencyarray[1]9
frequencyarray[2]8
ect...

Any help you guys can give will be appreciated.

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
 
#include <iostream>

using namespace std;

void enternumber(long[], int);
int main()
{ int size;
int numbers5=0;

 cout << "How many numbers?  ";
   cin >>size;


   long enteredNumbers[10000];
   long frequencyArray[10000];



enternumber(enteredNumbers,size);

for (int x=0
     ; x<size;x++)

{
    for ( int y=x; y<size; y++)
    {
        if (enteredNumbers[x]  == enteredNumbers[y] ){ frequencyArray[x]++; }
    }
}

for (int z=0; z<size;z++){cout<<frequencyArray[z];
        if (frequencyArray[z]==5) {numbers5++;}

        }
cout << "The number of values that show up 5  times is " << numbers5;
}

void enternumber(long enteredNumbers[],int f)
{
    for ( int count=0; count<f; count++)
   {
       cout << "Enter an Integer:  ";
       cin >> enteredNumbers[count];
   }
}

 
Last edited on
are you allowed to do hash tables?
cannot do hash tables unfortunately
Don't use a double-loop. (You wind up counting the same number more than once.)

A single loop will do.

BTW, you are creating a frequency table (called a histogram), not a distribution.

Hope this helps.
duoas you are a god send

thank you so much
here is the working code in case anyone was wondering

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
#include <iostream>

using namespace std;

void enternumber(long[], int);
int main()
{ int size;
int numbers5=0;

 cout << "How many numbers?  ";
   cin >>size;


   long enteredNumbers[10000];
   long frequencyArray[10000];



enternumber(enteredNumbers,size);

for (int x=0
     ; x<size;x++){
if (enteredNumbers[x]  == enteredNumbers[x] ){ frequencyArray[enteredNumbers[x]]++; }
    }


for (int z=0; z<size;z++){cout<<frequencyArray[z];
        if (frequencyArray[z]==5) {numbers5++;}

        }
cout << "The number of values that show up 5  times is " << numbers5;
}

void enternumber(long enteredNumbers[],int f)
{
    for ( int count=0; count<f; count++)
   {
       cout << "Enter an Integer:  ";
       cin >> enteredNumbers[count];
   }
}

 
Plus a little formatting
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
#include <iostream>

using namespace std;

void enternumber(long[], int);

int main()
{
  int size;
  int numbers5=0;

  cout << "How many numbers?  ";
  cin >>size;

  long enteredNumbers[10000];
  long frequencyArray[10000];

  enternumber(enteredNumbers,size);

  for (int x=0; x<size; x++)
  {
    if (enteredNumbers[x]  == enteredNumbers[x] )
    {
      frequencyArray[enteredNumbers[x]]++;
    }
  }

  for (int z=0; z<size; z++)
  {
    cout<<frequencyArray[z];
    if (frequencyArray[z]==5)
    {
      numbers5++;
    }
  }
  cout << "The number of values that show up 5  times is " << numbers5;
}

void enternumber(long enteredNumbers[],int f)
{
  for ( int count=0; count<f; count++)
  {
    cout << "Enter an Integer:  ";
    cin >> enteredNumbers[count];
  }
}
Topic archived. No new replies allowed.