Dec 1, 2015 at 11:43pm UTC
I created a menu and one of the options is to output the number of times a number appears in a 12 number array
for example if the numbers in the array
1,2,3,4,3,4,5,6,9,8,6,5
if you entered 3 for example the number outputted is 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 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
#include "stdafx.h"
using namespace std;
int main()
{
double big, small, countNumbers;
double list[12];
int option;
double 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 << "\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 << 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++)
{
}
break ;
case 99:
break ;
default :
cout << "Invalid option\n" ;
}while (option != 99);
return 0;
Last edited on Dec 1, 2015 at 11:46pm UTC
Dec 2, 2015 at 12:03am UTC
If you're trying to keep track of a number of occurrences of a certain piece of data within a container, create another container to keep track of the repeated variable.
However, what you're asking for in this case would be simpler. Just have a simple int counter, initialize it to 0, and for every time the data item that's asked for appears in the container, increment it.