Not sure how to go about outputing the occurance of an integer in an array

Basically for a college assignment I have to create a menu by creating an array and outputting to the screen a number of options which the user can pick such as

press 1 for the smallest number in the array
press 2 for largest number
press 3 for the average number in the array
etc

using a switch statement

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
 int big, small, maxCount = 0;
	int list[12];
	int option;
	int sum = 0;
	big = small = list[0];

	cout << "Enter 12 numbers : ";
	for (int i = 0; i < 12; i++)
	{

		cin >> list[i];

	}

	do
	{

		cout << "\t\tMenu\n";
		cout << "\t0.Display\n";
		cout << "\t1.Total\n";
		cout << "\t2.Average\n";
		cout << "\t3.Largest\n";
		cout << "\t4.Smallest\n";
		cout << "\t5.Occurance of value\n";
		cout << "\t99.Exit\n";
		cout << "\t\t Option ? ";
		cin >> option;


		switch (option)
		{
		case 0:

			cout << "Contents\n";


			for (int i = 0; i < 12; i++)
			{
				cout << list[i] << endl;

			}
			break;

		case 1:

			cout << "Total";
			cout << "\n\n";

			for (int i = 0; i < 12; i++)
			{


				sum += list[i];

			}

			cout << sum;

			break;

		case 2:

			cout << "average";

			for (int i = 0; i < 12; i++)
			{
				sum += list[i];

			}

			cout << setprecision(1) << fixed << sum / 12;

			break;

		case 3:

			cout << "Largest";

			for (int i = 0; i < 12; i++)
			{
				if (list[i] > big)
				{
					big = list[i];
				}

			}

			cout << big;

			break;

		case 4:

			cout << "Smallest";

			for (int i = 0; i < 12; i++)
			{
				if (list[i] < small)
				{
					small = list[i];
				}

			}

			cout << small;

			break;

		case 5:

			cout << "Occurances of value";

			for (int i = 0; i < 12; i++)
			{
				int count = 1;
				for (int j = i + 1; j < 12;j++)
			
					if (list[i] == list[j])
						count++;
			
				if (count > maxCount)
					maxCount = count;

				for (int i = 0; i<5; i++)
				{
					int count = 1;
					for (int j = i + 1; j<5; j++)
						if (list[i] == list[j])
							count++;
					if (count == maxCount)
						cout << list[i] << endl;
				}
			}

			break;


case 5 in the switch statement is the one i need help with, basically i need to enter a value and output to the screen the number of times it appears in the array but im not sure how to go about approaching it, i formulated that code in case 5 from another person online but i dont entirely understand it, Help would be much appreciated
You're overly complicating it.
1
2
3
4
5
6
7
8
  int val;
  int cnt = 0;  
  cout << "Enter value to search for: ";
  cin >> val;
  for (int i=0; i<12; i++)
    if (list[i] == val)
        cnt++; 
  cout << val << " occurs " << cnt << " times" << endl;

Last edited on
thank you :)
Topic archived. No new replies allowed.