In: Computer Science
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";
}
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: