Program not counting numbers right

The program asks you to enter random numbers, then it will output the numbers from greatest to least, along with the number of each. For example if you input "1 3 3 10 1 3" It should output something like this:

Numbers: Amount:
1 2
3 3
10 1


But it's doing something more like this:

Numbers: Amount:
1 2
3 2
10 1
3 1

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


void inputNum(int& count, int array[]);
void fillArray(int a[], int size, int& num);
void sort(int a[], int num);
void swapValues(int& num1, int& num2);
void countNum(int array1[], int& i, int& numCount, int size);

int main()
{
	int arrays[25], size = 0, numCount;

	inputNum(size, arrays);
	sort(arrays, size);

	cout << "\nNumbers:\tAmount:\n";

	for (int i = 0; i < size; i++)
	{
		countNum(arrays, i, numCount, size);
		cout << arrays[i] << "\t\t" << numCount << endl;
	}
	system("pause>nul");
}

void inputNum(int& count, int array[])
{
	int i = 0, input;
	cout << "This program sorts numbers from highest to lowest and counts how many there are of each. \n \nPlease enter 25 numbers or enter 100 to stop earlier: \n \n";
	cin >> input;
	while (input != 100 && i < 25)
	{
		array[i] = input;
		i++;
		cin >> input;
	}
	count = i;
}

void sort(int a[], int num)
{
	for (int index = 0; index < num - 1; index++)
	{
		for (int index2 = index + 1; index2 < num - 1; index2++)
		{
			if (a[index] < a[index2])
				swapValues(a[index], a[index2]);
		}
	}
}

void swapValues(int& num1, int& num2)
{
	int temp = num1;
	num1 = num2;
	num2 = temp;
}



void countNum(int a[], int& i, int& numCount, int size)
{
	int index = i + 1;
	numCount = 1;
	for (index; index < size; index++)
	{
		if (a[i] == a[index])
		{
			numCount++;
			i++;
		}
		else return;
	}
}

I took your code and added some "print array" for debugging. (The "RNG user" types 20 numbers, all in range [1..20].) Your sort() has an issue. Might take couple runs to spot it.
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
#include <random>
#include <chrono>
#include <iostream>
using std::cout;
using std::endl;

void inputNum(int& count, int array[]);
void fillArray(int a[], int size, int& num);
void sort(int a[], int num);

constexpr int NN {20};

int main() {
  int arrays[NN+5] {};
  int size = 0;

  inputNum(size, arrays);
  for ( auto x : arrays ) std::cout << ' ' << x; std::cout << '\n';
  sort(arrays, size);
  for ( auto x : arrays ) std::cout << ' ' << x; std::cout << '\n';
  return 0;
}

void inputNum(int& count, int array[]) {
  unsigned seed1 = std::chrono::system_clock::now().time_since_epoch().count();
  std::default_random_engine generator(seed1);
  std::uniform_int_distribution<int> distribution(1,20);
  for (int i=0; i < NN; ++i) array[i] = distribution(generator);
  count = NN;
}

void sort(int a[], int num) {
  for (int index = 0; index < num - 1; index++) {
    for (int index2 = index + 1; index2 < num - 1; index2++) {
      if (a[index] < a[index2]) std::swap(a[index], a[index2]);
    }
  }
}



Note: the following:
1
2
3
4
5
6
7
cin >> input;
while (input != 100 && i < 25)
{
	array[i] = input;
	i++;
	cin >> input;
}

could be written:
1
2
3
4
5
6
while ( i < 25 && // no point to read, if we already have 25 
        std::cin >> input && // the >> can fail, so test for it
        input != 100 )
{
  array[i++] = input; // only use post-increment where it makes a difference
}

Topic archived. No new replies allowed.