The program reads a given set of integers, then prints and determines the number whether it is an odd, even, or zero.
However, I am unable to solve the issue of printing the correct results. My question, where is the issue? And what am I doing wrong? Am I missing something?
what is getNumber() supposed to do? Currently it does like nothing.
On line 25: storeNum is just a clone of number which is unitialized and hence random.
number and checkNum in printResults() are uninitialized as well
I guess that line 21 and 22 are supposed to be inside getNumber()? If so getNumber() shouldn't have parameters
analyzeNumber() doesn't return anything (hence the return value is undefined/random). I guess you want to return max;. Do int max = 0; at the beginning and within the if: max++; instead of count++;. Further more it requires a second parameter that tells what type of number should be analyzed.
You have max multiple time (all uninitialized). You need to decide: whether you use the local or the global one, but not both (like in analyzeNumber())
This program reads a given set of integers and then prints the total number of odd numbers, the number of evens, and the number of zeroes.
This program reads a certain number of integers.
Prompt the user to specify how many integers are to be read so that you can set up a loop to process the integers that are to be entered by the user.
After reading an integer, you need to check whether it is even, odd, or a zero.
For this program you need to:
1. Get a number from the user 2. Analyze the number 3. Increment the appropriate counters 4. Repeat steps 1 - 3, to get and analyze all numbers entered by user 5. After all numbers have been analyzed (processed), print results
For this program, you need to create the following functions:
The code is self-explanatory; however, my question is can you help me? I tried several times to get the code to work. I read chapter 6 a few times and revisited a few sections such as: Local and Global Variables, Static Local Variables, Using Reference Variables as Parameters in Starting Out with C++ Early Objects. Yet, I seem to find myself struggling with the use of function due to the confusion on this particular chapter by changing variables on multiple accounts.
#include <iostream>
usingnamespace std;
int getNumber(int);
int analyzeNumber(int);
void printResults();
int main()
{
int max, number;
cout << "How many intergers do you want to classify? ";
cin >> max;
for (int repeat = 0; repeat < max; repeat++)
{
cout << "\nEnter an interger number: ";
cin >> number;
//Grabs and stores the number in getNumber
getNumber(________);
}
printResults();
system("pause");
return 0;
}
int getNumber(int _______)
{
//Stores the number and before it is overwritten, the analyzeNumber
//checks it out.
analyzeNumber(________);
}
int analyzeNumber(int ________)
{
//Checks if the number is a zero, odd, or even by using a COUNT to track the
//total of each integer inputted.
int count = 0;
if (_______ == 0)
{
count = 0;
count++;
}
elseif (_________ % 2 > 0)
{
count = 0;
count++;
}
else
{
count = 0;
count++;
}
}
}
void printResults()
{
//Displays the total numbers based on what the user inputted.
cout << "The numbers you entered are: \n" << getNumber(__________) << "\n";
//Displays the total or count of each individual interger.
cout << "The total number of zeroes: " << __________ << "\n\n";
cout << "The total number of even numbers is: " << __________ << "\n\n";
cout << "The total number of odd numbers is: " << __________ << "\n\n";
}
I ain't sure if double posting is allowed; however, here's my fourth attempt at my chapter 6 exam.
The question is, why can't I have the numbers be analyzed to check and see if the number is a zero, odd, or even; then print the total in printResults()?
Are the functions currently doing nothing? Are the variables out of place? Is the issue due to the variables being set to 0? Or are the variables both in the call and function not working properly?
#include <iostream>
usingnamespace std;
int getNumber (int &num);
int analyzeNumber (int);
void printResults();
int main ()
{
int max, number = 0;
cout << "How many intergers do you want to classify? ";
cin >> max;
for (int repeat = 0; repeat < max; repeat++)
{
cout << "\nEnter an interger number: ";
cin >> number;
}
cout << "\n\n";
//Grabs and stores the number in getNumber
getNumber(number);
//Checks if the number is a zero, odd, or even by using the TOTAL variable
//to track the total of each integer inputted.
analyzeNumber(number);
system ("pause");
return 0;
}
int getNumber(int &num)
{
return num;
}
int analyzeNumber (int num)
{
int total = 0, total2 = 0, total3 = 0, value;
if (value == 0)
{
total++;
}
elseif (value % 2 > 0)
{
total2++;
}
else
{
total3++;
}
void printResults();
}
void printResults()
{
int num, total, total2, total3;
//Displays the total numbers based on what the user inputted.
cout << "The numbers you entered are: \n" << getNumber(num) << "\n";
//Displays the total or count of each individual interger.
cout << "The total number of zeroes: " << total << "\n\n";
cout << "The total number of even numbers is: " << total2 << "\n\n";
cout << "The total number of odd numbers is: " << total3 << "\n\n";
}