What number is used the most

Hi,
I am doing program to read 5 numbers from user and outup the number that is used the most of the time.
For example user input is 1,2,3,4,1 the output would be 1 or 6,5,5,5,6 output is 6.

But as I tried to make it work I have more and more problems.

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
  #include <stdio.h>

void main()
{
	int i = 0; int cisla[6]; int pocty[50]; int y; int z; z = 0; int k; k = 0;

	while (true)
	{   if (i < 15)
		{  pocty[i] = 0; i++; }
		else
		{ break; }
	} i = 0;
	while (i < 5)
	{   printf("zadej cislo ");
		scanf_s("%i", &cisla[i]);
		i = i + 1;
	}
	i = 0; int min = pocty[0]; y = 0;
	while (true)
	{
		if(k>4)
		{break;	}
		else {
			if (cisla[k] == cisla[1] || cisla[k] == cisla[2] || cisla[k] == cisla[3] || cisla[k] == cisla[4])
			{
				k = 0;
				while (true)
				{
					
					if (z > 4)
					{
						i = 0; y = 0; z = 0;
						while (i < 4)
						{
							if (pocty[z] > min)
							{
								min = cisla[i];  y++;
							}
							i++; z++;
						}
					}
					else if (y > 4)
					{
						i++; z = 0; y = 0;
					}
					else if (cisla[z] == cisla[y])
					{
						pocty[i] = pocty[i] + 1;
						y++;
						printf("k");
					}
					else
					{
						y = 5;
					}
				}

			}
		}
		k++;
	}
	z == 0; y = 0;
	while (true)
	{
		if (z > 5)
		{
			break;
		}
		else if (y > 5)
		{
			z++; y = 1;
		}
		else if (pocty[z] == pocty[y])
		{
			k++;
			y++;
		}
		else
		{
			y = 5;
		}
	}
	
	
	if (k > 0)
	{ printf("Je stejny pocet u vicero cisel.");	}
	else if (0 == cisla[0] || 0 == cisla[1] || 0 == cisla[2] || 0 == cisla[4]) { printf("Nejpouzivanejsi cislo je %i", min); }
	else if(min == 0){ printf("Kazde cislo je jine nebo je stejny pocet u vicero cisel."); }
	else { printf("Nejpouzivanejsi cislo je %i", min); }
	
}
Here is a much more C++ way of doing it. It makes use of existing C++ data structures.

A map is used to keep track of the actual values entered, and how many times they're entered.

Then, an existing standard algorithm, max_element, is used to iterate over that map, and find the key-value pair with the greatest value. The greatest value is determined according to the lambda function pass to the max_element function. The max_element function returns an iterator to the winning pair, and that iterator is used to get the winning key (->first)

Note also that the main function is of form int main(). void main() is wrong. Just plain wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <map>
#include <iostream>
#include <algorithm>
#include <utility>

int main()
{
  std::map<int, int> values;
  
  int inputCount = 5;  // Change this value to take in as many as you like
  while (inputCount--)
  {
      int input;
      std::cin >> input;
      values[input]++; // Each time user enters a number, increment the count of that number
  }
  
  std::cout << "Most frequent: " << std::max_element(values.begin(),  // From here...
                                                     values.end(),  // to here...
                                                     [](const std::pair<int, int>& p1, const std::pair<int, int>& p2) 
                                                       {return p1.second < p2.second; })->first;

}

Last edited on
But if there are different numbers or two numbers twice it ouputs only the first one
That's correct.

If you have different requirements, you need to say what they are. I am not psychic.

I thought this was an error:
or 6,5,5,5,6 output is 6.

because in that set of number, the most common number is 5, not 6, but maybe you don't actually mean "What number is used the most" ?
Last edited on
I really want the out put to be the most common number
Okey dokey. That's what the code does. What do you want to happen if there is more than one most common number? What is the output in the following cases:

7

1 2 3

1 1 2 2 3 3

1 1 2 3 4 4

-1 1 2 2
out : 7
out : all are only once
out : 1 and 4
out : 2
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
#include <map>
#include <iostream>
#include <algorithm>
#include <utility>

int main()
{
  std::map<int, int> values;
  
  int inputCount = 5;  // Change this value to take in as many as you like
  while (inputCount--)
  {
      int input;
      std::cin >> input;
      values[input]++; // Each time user enters a number, increment the count of that number
  }
  
  int top =  std::max_element(values.begin(),  // From here...
                                                     values.end(),  // to here...
                                                     [](const std::pair<int, int>& p1, const std::pair<int, int>& p2) 
                                                       {return p1.second < p2.second; })->second;

  for (const auto& pairing : values)
  {
    if (pairing.second == top) std::cout << pairing.first << " ";
  }
}
Thank you very much. I appreciate that.
Topic archived. No new replies allowed.