In this program, the user will enter two digit positive and negative integers (i.e. -99 to +99) but your job is to only count the number of occurrences of each integer between [-9 and +9], inclusive. When the user types 100 or -100 the program should exit and print out the statistic of how many times each integer between [-9 and +9] occurred as well as how many numbers outside the range of [-9 to +9] occurred.
You may assume the user will never type in a number that has magnitude (absolute value) of 101 or more.
Your output should list each number from -9 to +9 on a separate line followed by a colon and a space (:) and then the number of times that number was entered (i.e. number of occurrences). After printing out those 19 lines, you should output a final line that reads Out Of Range: n where n is the actual number of values that were entered but outside the range -9 to +9. You must follow this format or our auto checker will not work and you will lose points.
Hints: Arrays act like tables mapping an index to some desired value. Here we want to map the integer we receive -9 to +9 to the number of times we've entered that value. However, array indexing must be 0 to n-1 (indexes cannot be negative).
Example 1
If the user entered:
-10 -1 5 7 99 5 100
Your program should output exactly the lines:
-9: 0
-8: 0
-7: 0
-6: 0
-5: 0
-4: 0
-3: 0
-2: 0
-1: 1
0: 0
1: 0
2: 0
3: 0
4: 0
5: 2
6: 0
7: 1
8: 0
9: 0
Out Of Range: 2
I know this code is wrong but could someone please modify it so it is correct. It is due in an hour. PLEASE HELP!!! THANK YOU!!!
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
|
#include <iostream>
#include <string>
using namespace std;
int main()
{
const size_t MAXSIZE{21};
bool num;
int Numbers[MAXSIZE]{};
int Count[MAXSIZE]{};
int number;
int index{};
int totalUsable = 0;
int totalEntered = 0;
for(int i = -9; i <= 9; i++)
{
Numbers[index++] = i;
}
while(num == 1)
{
for (int i = 0; i < 21; i++)
{
if (Numbers[i] == number)
{
Count[i]++;
totalUsable++;
}
break;
}
cin >> number;
totalEntered--;
}
for(int i = 0; i < 21; i++)
{
cout << Numbers[i] << ':' << ' ' << Count[i] << endl;
}
cout << "Out Of Range: "<< number;
return 0;
}
|