Classify Numbers

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <iomanip>
using namespace std;

int getNumber(int storeNum);
int analyzeNumber(int checkNum);
void printResults();

int max;

int main()
{
    
int max, wholeNum, number, storeNum, results;

cout << "How many intergers do you want to classify? ";
cin >> max;

for (int num = 0; num < max; num++)
    {
        cout << "\nEnter an interger number: ";
        cin >> wholeNum;
    
        storeNum = getNumber(number);
        analyzeNumber(storeNum);
    }
    
    printResults();

    system("pause");
    return 0;
}
int getNumber(int storeNum)
{
    return storeNum;
}
int analyzeNumber(int checkNum)
{
    int max;
    
    for (int count = 0; count < max; count++)
        {
             if (checkNum == 0)
                {
                     count++;
                }
             else if (checkNum % 2 == 0)
                {
                     count++;
                }
             else
                {
                     count++;
                }
        }
}
void printResults()
{
     int number, checkNum;
     
     getNumber(number);
     
     if (number == 0)
        {
             cout << "The total number of zeroes: " << analyzeNumber(checkNum);
             
             cout << "\n\n";
        }
     else if (number % 2 == 0)
        {
             cout << "The total number of even numbers is: " << analyzeNumber(checkNum);
             
             cout << "\n\n";
        }
     else
        {
             cout << "The total number of odd numbers is: " << analyzeNumber(checkNum);
             
             cout << "\n\n";
        }
}
Last edited on
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())
I'll just input what I was given:

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:

1. getNum

2. analyzeNum

3. printResults

Sample Run:

How many integers do you want to classify? 20

(User enters 20 numbers)

Output:

The numbers that you entered are:

0, 12, 29, 45, 17, 2, 8, 0, 3, 9, 4, 0, 1, 0, 7, 23, 24, 0, 0, 28

The total number of zeroes: 6
The total number of evens: 6
The total number of odds: 8
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
using namespace 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++;
                }
             else if (_________ % 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";
}
Last edited on
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>

using namespace 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++;
          }
       else if (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";
}
Last edited on
I don't know up to which chapter have you learn about c++, i avoid using array or vector. Here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>

int getNumber();
void analyzeNumber(int num, int &evenSize, int &oddSize, int &zeroSize);
void printResults(int evenSize, int oddSize, int zeroSize);
int main(int argc, char** argv)
{
	int num_size;
	int numOfEven = 0;
	int numOfOdd = 0;
	int numOfZero = 0;

	std::cout << "How many intergers do you want to classify? ";
	std::cin >> num_size;

	for (int repeat = 0; repeat < num_size; repeat++)
	{
		analyzeNumber(getNumber(), numOfEven, numOfOdd, numOfZero);
	}

	printResults(numOfEven, numOfOdd, numOfZero);

	return 0;
}

int getNumber()
{
	int number;

	std::cout << "\nEnter an interger number: ";

	std::cin >> number;
	return number;
}


void analyzeNumber(int num, int &evenSize, int &oddSize, int &zeroSize)
{
	if(num!=0)
	{
		if((num%2) ==0)
			evenSize++;
		else
			oddSize++;
	}
	else
	{
		zeroSize++;
	}
}

void printResults(int evenSize, int oddSize, int zeroSize)
{
	std::cout << "Number of even: " << evenSize << std::endl;

	std::cout << "Number of odd: " << oddSize << std::endl;

	std::cout << "Number of zero: " << zeroSize << std::endl;
}

Last edited on
You pretty much solved it. Thanks.
Topic archived. No new replies allowed.