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
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