Allow the user to enter values between 1-9, up to 2 billion values. When the user enters a 0 report the frequency of each digit and terminates the program.
The following is a sample run.
Welcome to Brent's frequency analyzer. Enter some numbers between 1-9,
more than none, and less than 2,000,000,000. Enter a 0 to get the results.
Number: 5
Number: 8
Number: 5
Number: 3
Number: 7
Number: 5
Number: 0
Number Frequencies
Number: 1 2 3 4 5 6 7 8 9
Frequency: 0 0 1 0 3 0 1 1 1
#include <bits/stdc++.h>
usingnamespace std;
int main(){
int n = 1;
int counter = 0;
int intArray[100];
int freqArray[10];
cout << "Welcome to Kolton's frequency analyzer. Enter some numbers between 1-9,";
cout << " more than none, and less than 2,000,000,000. Enter a 0 to get the results." << endl;
while(n!=0){
cout << "Enter a number: ";
cin >> n;
if (n!=0){
intArray[counter] = n;
counter++;
}
}
for(int i = 0; i < counter; i++){
cout << intArray[i] << endl;
}
cout << "You entered " << counter << " numbers.\n";
cout << "The middle number entered is: " << intArray[counter/2] << " at index " << counter/2 << endl;
return 0;
}
You don't need to store the input numbers. If you did need to store them you would need space for two billion of them! I just typed the following in here so it's untested (and incomplete).
#include <iostream>
usingnamespace std;
int main() {
int freq[10] { }; // the braces initialize the array elements to 0
cout << "Enter up to two billion numbers from 1 to 9. Enter 0 to stop.\n";
while (true) {
int n;
cin >> n;
if (n == 0) break;
if (n > 0 && n < 10)
++freq[n];
else
cout << "Numbers must be from 1 to 9\n";
}
for (int i = 1; i < 10; ++i)
cout << freq[i] << ' ';
cout << '\n';
}
Don't use bits/stdc++.h. It's nonstandard and generally for retards only. Instead, include the actual C++ headers that you use.
#include <iostream>
int main() {
int freq[10] { };
std::cout << "Enter number(s) from 1 to 9. Enter 0 to stop.\n";
for (int n {}; (std::cin >> n) && (n != 0); )
if (n > 0 && n < 10)
++freq[n];
else
std::cout << "Numbers must be from 1 to 9\n";
for (size_t f = 1; f < 10; ++f)
std::cout << f << ' ' << freq[f] << '\n';
}