Question

In: Computer Science

I have to create a program in C++ where a user can enter as many numbers...

I have to create a program in C++ where a user can enter as many numbers as they want (they predetermine the number of values to be inputted) and then the program can echo that input back to the user and then determine if the numbers were even, odd, or a zero and it outputs how many of each were found. This is to be down with four void functions and no arrays. The program initializes the variables zero, odds, and even to 0, then reads a number at a time. If the number is even, increments the even count, otherwise increments the odd count. Update zero if it is a zero. Below is the code the I have so far, however I think there are some errors with how the functions are laid out. Any help would be appreciated! Apologies if it posts in an odd format or with odd spacing.

#include<iostream>
using namespace std;
void initialize();
void get_number(int &x);
void classify_number(int& zero_count, int& even_count, int& odd_count);
void print_results();
int N; //The number of values that the user wants to enter
int main()
{
   cout << "Enter number of values to be read, then press return.\n";
   cin >> N;

   return 0;
}
void initialize()
{
  
   int even_num = 0;
   int odd_num = 0;
   int zero_count = 0;
  
}
void get_number(int &x)
{

   do {
       cout << "Enter a positive whole number, then press return.\n";
       cin >> x; //The number the user enters
       cout << x;

   } while (count < N);

}
void classify_number(int& zero_count, int& even_count, int& odd_count)
{
   get_number(int &x);
   if (x % 2 == 0)
       zero_count++;
  
       if (x % 2 > 0)
       even_count++;
  
   else
       odd_count++;
}
void print_results()
{
   classify_number(int& zero_count, int& even_count, int& odd_count);
   cout << "There are " << even_count << "number of evens.\n";
   cout << "There are " << zero_count << "number of zeros.\n";
   cout << "There are " << odd_count << "number of odds.\n";
}

Solutions

Expert Solution

Please find your solution below and if any doubt comment and do upvote.

CODE:

#include<iostream>
using namespace std;
void initialize();
void get_number(int x);
void classify_number(int, int& zero_count, int& even_count, int& odd_count);
void print_results();
int N; //The number of values that the user wants to enter
//global variable so the can be accessed by all function
int even_count ;
int odd_count ;
int zero_count;
int main()
{
   cout << "Enter number of values to be read, then press return.\n";
   cin >> N;
   get_number(N);
   return 0;
}
void initialize()
{
  
    even_count = 0;
    odd_count= 0;
    zero_count = 0;
  
}
void get_number(int N)
{
    
    int count=0;
    int x;
    //initialize global to 0
    initialize();
    do {
       cout << "Enter a positive whole number, then press return.\n";
       cin >> x; //The number the user enters
       cout << x<<endl;
       //call the funtion and increment their count
       classify_number(x,zero_count,even_count,odd_count);
       count++;

   } while (count < N);
   //then print the count
   print_results();

}
void classify_number(int x,int& zero_count, int& even_count, int& odd_count)
{
   
   if (x == 0)
       zero_count++;
  
   else if (x % 2 == 0)
       even_count++;
  
   else
       odd_count++;
}
void print_results()
{
   
   cout << "There are " << even_count << " number of evens.\n";
   cout << "There are " << zero_count << " number of zeros.\n";
   cout << "There are " << odd_count << " number of odds.\n";
}

OUTPUT:


Related Solutions

Write a C program that prompt the user to enter 10 numbers andstores the numbers...
Write a C program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
Write a C++ program that prompt the user to enter 10 numbers andstores the numbers...
Write a C++ program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
I need to ask a user what numbers they want to enter. They can enter as...
I need to ask a user what numbers they want to enter. They can enter as many as they like. Then inside I need to use conditionals to determine if the numbers are <=20, <=323 && > 30, >200. I can't figure out how to let the user enter as many inputs as they want. I know I need to use a loop to check each number entered and determine if it is small middle or greater but I can't...
Write a C++ program that asks the user to enter in three numbers and displays the...
Write a C++ program that asks the user to enter in three numbers and displays the numbers in ascending order. If the three numbers are all the same the program should tell the user that all the numbers are equal and exits the program. Be sure to think about all the possible cases of three numbers. Be sure to test all possible paths. Sample Runs: NOTE: not all possible runs are shown below. Sample Run 1 Welcome to the order...
USE PYTHON. Create a program that will ask a user to enter whole positive numbers. The...
USE PYTHON. Create a program that will ask a user to enter whole positive numbers. The program will use a while loop to let the program keep running. The user will be allowed to use the program as long as the quitVariable is equal to 'y' or 'Y'. When the user decides to quit playing, say "Thanks for playing!". The program will use a for loop to test if the number are even or odd. If even print "number is...
write a c++ program that prompts a user to enter 10 numbers. this program should read...
write a c++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the two numbers and the average of the 10 numbers PS use file I/o and input error checking methods
in C++, Write a program that asks the user to enter 6 numbers. Use an array...
in C++, Write a program that asks the user to enter 6 numbers. Use an array to store these numbers. Your program should then count the number of odd numbers, the number of even numbers, the negative, and positive numbers. At the end, your program should display all of these counts. Remember that 0 is neither negative or positive, so if a zero is entered it should not be counted as positive or negative. However, 0 is an even number....
Write a C++ program that asks the user to enter a series of single-digit numbers with...
Write a C++ program that asks the user to enter a series of single-digit numbers with nothing separating them. Read the input as a C-string or a string object. The program should display the sum of all the single-digit numbers in the string. For example, if the user enters 2514, the program should display 12, which is the sum of 2, 5, 1, and 4. The program should also display the highest and lowest digits in the string. It is...
Hi i need a c++ program that can convert charactor into numbers pseodocode user input their...
Hi i need a c++ program that can convert charactor into numbers pseodocode user input their name for example john every single charactor should have a value so it return correct result eg: j=2, o=1, h=7,n=2 and display these numbers if you may need any question to ask me, i will be very happy to answer them. Thanks! example project close but not accurate #include #include #include #include #include #include #include #include #define INPUT_SIZE 8 using namespace std; // Function...
Time Calculator Create a C++ program that lets the user enter a number of seconds and...
Time Calculator Create a C++ program that lets the user enter a number of seconds and produces output according to the following criteria: • There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. • There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT