Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- int main()
- {
- std::cout<<"Welcome to Stats Array!\n"
- <<"These are your choices:\n"
- <<"N- Numbers\n"
- <<"S- Sum of all\n"
- <<"A- Average of all\n"
- <<"B- Biggest of all\n"
- <<"F- Most Frequent of all\n"
- <<"H- How many numbers\n"
- <<"M- Median of all\n"
- <<"Q- Quit\n";
- bool running = true;
- int frequency[100] = {0};
- // run until user specifies to stop
- while(running) {
- char choice;
- int numInput;
- std::cin>>choice;
- switch (choice) {
- // add input to frequency
- case 'N': {
- std::cin>>numInput;
- frequency[numInput] += 1;
- std::cout<<"OK"<<std::endl;
- break;
- }
- // sum of all numbers
- case 'S': {
- int sum = 0;
- for (int i = 0; i < 100; i++) {
- if (frequency[i] > 0) {
- sum += i * frequency[i];
- }
- }
- std::cout<<sum<<std::endl;
- break;
- }
- // average of all numbers
- case 'A': {
- int sum = 0;
- int num = 0;
- for (int i = 0; i < 100; i++) {
- if (frequency[i] > 0) {
- num += frequency[i];
- sum += i * frequency[i];
- }
- }
- if(num == 0) {
- std::cout<<"ERROR"<<std::endl;
- break;
- }
- double avg = sum/(double)num;
- std::cout<<avg<<std::endl;
- break;
- }
- // biggest of all numbers
- case 'B': {
- int biggest = 0;
- for (int i = 0; i < 100; i++) {
- if (frequency[i] > 0) {
- biggest = i;
- }
- }
- if (biggest == 0) {
- std::cout<<"ERROR"<<std::endl;
- break;
- }
- std::cout<<biggest<<std::endl;
- break;
- }
- // most frequent of all numbers
- case 'F': {
- int num = 0;
- for (int i = 0; i < 100; i++) {
- if (frequency[i] > frequency[num]) {
- num = i;
- }
- }
- if (frequency[num] == 0) {
- std::cout<<"ERROR"<<std::endl;
- break;
- }
- std::cout<<num<<std::endl;
- break;
- }
- // count of how many numbers
- case 'H': {
- int howMany = 0;
- for (int i = 0; i < 100; i++) {
- if (frequency[i] > 0) {
- howMany += frequency[i];
- }
- }
- std::cout<<howMany<<std::endl;
- break;
- }
- // median of all numbers
- case 'M': {
- int howMany = 0;
- for (int i = 0; i < 100; i++) {
- if (frequency[i] > 0) {
- howMany += frequency[i];
- }
- }
- if (howMany == 0) {
- std::cout<<"ERROR"<<std::endl;
- break;
- }
- int median = howMany / 2 + 1;
- std::cout<<median<<std::endl;
- break;
- }
- // quit the program
- case 'Q': {
- running = false;
- break;
- }
- // input error handling
- default: {
- std::cout<<"Bad input. Try capitalizing the letter."<<std::endl;
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement