The question is:
Write a program that prompts the user to input ten values between 80 and 85 and stores them in an array. Your program must be able to count the frequency of each value appears in the array.
Is there any more efficient ways to present the count for frequency number between 80 to 85? Below is the sample code which had been done in the simplest ways to count the frequency for each number.
The part which needs more efficient ways to present:
for(int i = 0; i < inputSize; i++) {
if(inputValue[i] == 80) {
tempCount80++;
} else if(inputValue[i] == 81) {
tempCount81++;
} else if(inputValue[i] == 82) {
tempCount82++;
} else if(inputValue[i] == 83) {
tempCount83++;
} else if(inputValue[i] == 84) {
tempCount84++;
} else if(inputValue[i] == 85) {
tempCount85++;
} else {
cout << "Error Accurs." << endl;
}
}
cout << 80 << " " << tempCount80 << endl;
cout << 81 << " " << tempCount81 << endl;
cout << 82 << " " << tempCount82 << endl;
cout << 83 << " " << tempCount83 << endl;
cout << 84 << " " << tempCount84 << endl;
cout << 85 << " " << tempCount85 << endl;
The original code is: -
#include <iostream>
using namespace std;
int main() {
const int inputSize = 10;
int inputValue[inputSize];
int tempCount80 = 0;
int tempCount81 = 0;
int tempCount82 = 0;
int tempCount83 = 0;
int tempCount84 = 0;
int tempCount85 = 0;
for(int i = 0; i < inputSize; i++) {
int tempValue = 0;
cout << "Please enter a number between 80 and 85: ";
cin >> tempValue;
if(tempValue > 79 && tempValue < 86) {
inputValue[i] = tempValue;
} else {
cout << "The number must be between 80 and 85" << endl;
do {
i--;
break;
} while(i > 0);
}
}
for(int i = 0; i < inputSize; i++) {
if(inputValue[i] == 80) {
tempCount80++;
} else if(inputValue[i] == 81) {
tempCount81++;
} else if(inputValue[i] == 82) {
tempCount82++;
} else if(inputValue[i] == 83) {
tempCount83++;
} else if(inputValue[i] == 84) {
tempCount84++;
} else if(inputValue[i] == 85) {
tempCount85++;
} else {
cout << "Error Accurs." << endl;
}
}
cout << 80 << " " << tempCount80 << endl;
cout << 81 << " " << tempCount81 << endl;
cout << 82 << " " << tempCount82 << endl;
cout << 83 << " " << tempCount83 << endl;
cout << 84 << " " << tempCount84 << endl;
cout << 85 << " " << tempCount85 << endl;
return 0;
}
The output is as expected, but need to find more efficient ways to solve the question.