9 Replies - 4390 Views - Last Post: 04 March 2009 - 01:06 PM Rate Topic: -----

#1 lmarcil1982  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-February 09

C++ Function that takes a parameter of an integer & returns the #

Post icon  Posted 03 March 2009 - 04:39 PM

Any help would be great. I am a new programmer and I was very excited that I was starting to understand C++, but this code has been stressing me out. I can not figure out what I am doing wrong! I have spent many hours and DAYS on this and I just can't seem to get it right.

Here is what I am trying to do:
"Write a function that takes as a parameter an integer and returns the number of odd, even, and zero digits. Also write a program to test your function." I also have to have a do loop in there that ask the user if they would like to run the program again or not.

Here is the code I have, but it's only outputting the first part. =(

// Author:  Laura

//Program:  Classify Numbers that are odd, even or zero.
//This program reads the integer that the user inputs and 
//then outputs the number of zeros, odd, and even numbers.

//Algorithm:  Initializes the variables, zeros, odds, and
//evens to 0.  It then reads the number.
//If the number is even, invrement the even count, and if the 
//number is also zero, increment the zero count; else increment
//the odd count.
//Program will repeat if user wants it to.  

#include "stdafx.h"
#include "iostream"
#include "iomanip"


using namespace std;


//====================
//Function prototypes
//====================
void initialize(int& zeroCount, int& oddCount, int& evenCount);
void getNumber(int&num);
void classifyNumber(int num, int& zeroCount, int& oddCount, 
					int& evenCount);
void printResults(int zeroCount, int oddCount, int evenCount);

	
int main()
{

//   ====================
//   Variable declaration
//   ====================
	 int counter; 
	 int number;
	 int zeros;
	 int odds;
	 int evens;

	 char userAnswer;
	 bool finished = false;
//   ==========

//   =========
//   User loop
//   =========
	 do {

//   ============
//   Input values
//   ================================
	 initialize (zeros, odds, evens);

	 cout << "Please enter an integer value." << endl;
	 cin >> number;
	 cout << endl;
	
	 
	 for (counter = 1; counter <= number; counter++)
	 {
		 getNumber(number);
		 cout << number << " ";
		 classifyNumber(number, zeros, odds, evens);
	 }// end for loop

	 cout << endl; 

	
//	=============================
//	User loop - obtained by user
//	=============================
	  cout << "Would you like to go again? (y/n)" << endl;
	  cin >> userAnswer;
	  if (userAnswer == 'n')
		  finished = true;
//	====================

}	 while (!finished);
//	======================

	 return 0;

}// Function main
//  =============
   void initialize(int& zeroCount, int& oddCount, int& evenCount)
	 {
		 zeroCount = 0;
		 oddCount = 0;
		 evenCount = 0;
	 }

	 void getNumber(int& num)
	 {
		 cin >> num;
	 }

	 void classifyNumber(int num, int& zeroCount, int& oddCount,
						 int& evenCount)
	 {
		 switch (num % 2)
		 {
		 case 0:
			 evenCount++;
			   if (num == 0)
				   zeroCount++;
			   break;
		 case 1:
		 case -1:
			 oddCount++;
		 }
	 }

	 void printResults(int zeroCount, int oddCount, int evenCount)
	 
	 {
		 cout << "There are " << evenCount << " evens, "
			  << "which includes " << zeroCount << " zeros"
			  << endl; 
		 
		 cout << "The number of odd numbers is: " << oddCount << endl;

	 }
//   =======================================================================


Is This A Good Question/Topic? 0
  • +

Replies To: C++ Function that takes a parameter of an integer & returns the #

#2 krum110487  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 291
  • Joined: 07-February 09

Re: C++ Function that takes a parameter of an integer & returns the #

Posted 03 March 2009 - 05:06 PM

whoops, I can't read, I have to leave work, if no one has answered, then I will...

This post has been edited by krum110487: 03 March 2009 - 05:08 PM

Was This Post Helpful? 0
  • +
  • -

#3 lmarcil1982  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-February 09

Re: C++ Function that takes a parameter of an integer & returns the #

Posted 03 March 2009 - 08:42 PM

If anybody could help me on this, that would be great.

I have looked this code over sooo much and I just don't get what I am doing wrong.
Was This Post Helpful? 0
  • +
  • -

#4 learncplusplus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-February 09

Re: C++ Function that takes a parameter of an integer & returns the #

Posted 04 March 2009 - 08:56 AM

[color=#FF0000]This could be done in a simple way, However, there r very minor changes to be done in ur code
below code snippet shows the changes

#include "stdafx.h" 
#include "iostream"
#include "iomanip"


using namespace std;


//====================
//Function prototypes
//====================
void initialize(int& zeroCount, int& oddCount, int& evenCount);
void getNumber(int&num);
void classifyNumber(int num, int& zeroCount, int& oddCount,
					int& evenCount);
void printResults(int zeroCount, int oddCount, int evenCount);

int main()
{

//   ====================
//   Variable declaration
//   ====================
	 int counter;
	 int number, nums;	[color] [color=#FF0000]// Added nums variable  [/color]
	 int zeros;
	 int odds;
	 int evens;

	 char userAnswer;
	 bool finished = false;
//   ==========

//   =========
//   User loop
//   =========
	 do {

//   ============
//   Input values
//   ================================
	 initialize (zeros, odds, evens);

	 cout << "Please enter an Total # of integer values." << endl;
	 cin >> number;
	 cout << endl;


	 for (counter = 1; counter <= number; ++counter)
	 {

		 getNumber(nums);   [color] [color=#FF0000] // changed here [/color]
//		 cout << number << " ";
		 classifyNumber(nums, zeros, odds, evens);

	 }// end for loop

	 cout << endl;


//	=============================
//	User loop - obtained by user
//	=============================
	  cout << "Would you like to go again? (y/n)" << endl;
	  cin >> userAnswer;
	  if (userAnswer == 'n')
		  finished = true;
//	====================

}	 while (!finished);
//	======================
		printResults(zeros, odds, evens);  [color] [color=#FF0000] // calling print funtion [/color][code]

	 return 0;

}// Function main


//  =============
void initialize(int& zeroCount, int& oddCount, int& evenCount)
{
		zeroCount = 0;
		oddCount = 0;
		evenCount = 0;
}

void getNumber(int& num)
{
		cin >> num;
}

void classifyNumber(int num, int& zeroCount, int& oddCount, int& evenCount)
{
		 switch (num % 2)
		 {
				 case 0:
					 evenCount++;
						if (num == 0)
								++zeroCount;
						 break;
				 case 1:
				case -1:
					 oddCount++;
		 }
} 

Was This Post Helpful? 0
  • +
  • -

#5 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: C++ Function that takes a parameter of an integer & returns the #

Posted 04 March 2009 - 10:57 AM

If I was assigned this project, the first thing I would do is define two functions: one which determines if a number is even, and one which determines if a number is odd, e.g.,

bool isEven(int);
bool isOdd(int);

bool isEven(int number)
{
	if(number == 0) /* Zero isn't even or odd */
		return false;
	if(number % 2 == 0) /* If a number is divisible by two, it is even */
		return true;

	return false;
}

bool isOdd(int number)
{
	if(number == 0)
		return false;
	if(number % 2 != 0) /* If a number isn't divisible by two, it is odd */
		return true;

	return false;
}
.

Next, because I will be needing to check each digit of the number, I would define a function to return the last digit of a number:

int getDigit(int);

int getDigit(int number)
{
	if(number > 0) /* We can't divide by zero */
		return number % 10;

	return 0; /* There are no more digits */
}
.

getDigit returns the last digit of a number if the number is greater than zero. If a number is less than or equal to zero, getDigit returns 0, meaning there are no more digits. Because, getDigit ignores number less than zero, this function will not work with negative integers.

Now that I have a function that returns the digits of a numbers, and two functions that classify a number as even or odd, I can write a function that counts the number of even, odd, and zero digits of a number:

void countDigits(int, int &, int &, int &);

void countDigits(int number, int &evens, int &odds, int &zeros)
{
	int digit;

	evens = 0;
	odds  = 0;
	zeros = 0;

	digit = getDigit(number); /* Get the last digit of the number */

	while(number > 0) {
		if(isEven(digit)) /* Is the digit even? */
			evens ++;
		else if(isOdd(digit)) /* Is the digit odd? */
			odds ++;
		else if(digit == 0) /* Is the digit a zero? */
			zeros ++;

		/* Get the next digit of the number */
		number /= 10;
		digit = getDigit(number);
	}
}


countDigits counts the number of even, odds, and zero digits in the specified number. The function takes the number as the first argument and
reference variables to store the number of even, odd, and zero digits as the second, third, and fourth argument, respectively.

Using the countDigits function, I can write a test program that prompts the user to enter a positive integer, and then prints the number of even, odd, and zero digits in the number.

In main:

int number;
int evens;
int odds;
int zeros;

do {
	cout << "Enter a positive integer: ";
	cin >> number;
}while(number < 1); /* Make sure the number is positive */

countDigits(number, evens, odds, zeros);

cout << "There are " << evens << " even, " << odds << " odd, and " << zeros << " zero digits in the number " << number << "." << endl;
.

Here is the code for the entire program:

/*
 * Program: Digit Counter
 *
 * File: digit.cpp
 *
 * Purpose: Counts the number of even, odd, and zero digits of a
 *		  number entered by the user.
 *
 * Author: Brenton Saunders
 *
 * Date: 04 Mar 2009
 *
 */
#include <iostream>

using namespace std;

bool isEven(int);
bool isOdd(int);
int  getDigit(int);
void countDigits(int, int &, int &, int &);

int main()
{
	int number;
	int evens;
	int odds;
	int zeros;

	do {
		cout << "Enter a positive integer: ";
		cin >> number;
	}while(number < 1); /* Make sure the number is positive */

	countDigits(number, evens, odds, zeros);

	cout << "There are " << evens << " even, " << odds << " odd, and " << zeros << " zero digits in the number " << number << "." << endl;

	return 0;
}

/*
 * bool isEven(int number)
 *
 * Returns true if the specified number is even.
 *
 */
bool isEven(int number)
{
	if(number == 0) /* Zero isn't even or odd */
		return false;
	if(number % 2 == 0) /* If a number is divisible by two, it is even */
		return true;

	return false;
}

/*
 * bool isOdd(int number)
 *
 * Returns true if the specified number is odd.
 *
 */
bool isOdd(int number)
{
	if(number == 0)
		return false;
	if(number % 2 != 0) /* If a number isn't divisible by two, it is odd */
		return true;

	return false;
}

/*
 * int getDigit(int number)
 *
 * Returns the last digit of the specified number. If the number is less than
 * or equal to zero, getDigit returns 0.
 *
 */
int getDigit(int number)
{
	if(number > 0) /* We can't divide by zero */
		return number % 10;

	return 0; /* There are no more digits */
}

/*
 * void countDigits(int number, int &evens, int &odds, int &zeros)
 *
 * Counts the number of even, odd, and zero digits in the specified number.
 *
 */
void countDigits(int number, int &evens, int &odds, int &zeros)
{
	int digit;

	evens = 0;
	odds  = 0;
	zeros = 0;

	digit = getDigit(number); /* Get the last digit of the number */

	while(number > 0) {
		if(isEven(digit)) /* Is the digit even? */
			evens ++;
		else if(isOdd(digit)) /* Is the digit odd? */
			odds ++;
		else if(digit == 0) /* Is the digit a zero? */
			zeros ++;

		/* Get the next digit of the number */
		number /= 10;
		digit = getDigit(number);
	}
}
.

This post has been edited by bsaunders: 04 March 2009 - 11:23 AM

Was This Post Helpful? 0
  • +
  • -

#6 lmarcil1982  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-February 09

Re: C++ Function that takes a parameter of an integer & returns the #

Posted 04 March 2009 - 11:31 AM

bsaunders - Thanks for your help. I just have a couple more questions. First, why did you not like my functions? Does it make more sense as a programmer to do the functions that you did? I want to understand all this so I just wanted to get your input on it.

Is this the way you suggest me doing my code?

#include "stdafx.h" 
#include "iostream"
#include "iomanip"


using namespace std;


//====================
//Function prototypes
//====================

bool isEven(int);
bool isOdd(int);
int getDigit(int);
int countDigits(int, int &, int &, int &);
// ========================================


int main()
{

//   ====================
//   Variable declaration
//   ====================

int number;
int evens;
int odds;
int zeros;


//   =========
//   User loop
//   =========
do {
// ===


	cout << "Enter a positive integer: ";
	cin >> number;
}while(number < 1); /* Make sure the number is positive */

countDigits(number, evens, odds, zeros);

cout << "There are " << evens << " even, " << odds << " odd, and " << zeros << " zero digits in the number " << number << "." << endl;

   }// end for loop

	 cout << endl;

//	=============================
//	User loop - obtained by user
//	=============================
	  cout << "Would you like to go again? (y/n)" << endl;
	  cin >> userAnswer;
	  if (userAnswer == 'n')
		  finished = true;
//	====================

}	 while (!finished);
//	======================
	 return 0;

}// Function main

// ===========================
// Functions 
// =========================
   bool isEven(int number)
{
	if(number == 0) /* Zero isn't even or odd */
		return false;
	if(number % 2 == 0) /* If a number is divisible by two, it is even */
		return true;

	return false;
}

bool isOdd(int number)
{
	if(number == 0)
		return false;
	if(number % 2 != 0) /* If a number isn't divisible by two, it is odd */
		return true;

	return false;
}

int getDigit(int number)
{
	if(number > 0) /* We can't divide by zero */
		return number % 10;

	return 0; /* There are no more digits */
}

void countDigits(int number, int &evens, int &odds, int &zeros)
{
	int digit;

	evens = 0;
	odds  = 0;
	zeros = 0;

	digit = getDigit(number); /* Get the last digit of the number */

	while(number > 0) {
		if(isEven(digit)) /* Is the digit even? */
			evens ++;
		else if(isOdd(digit)) /* Is the digit odd? */
			odds ++;
		else if(digit == 0) /* Is the digit a zero? */
			zeros ++;

		/* Get the next digit of the number */
		number /= 10;
		digit = getDigit(number);
	}
}

Was This Post Helpful? 0
  • +
  • -

#7 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: C++ Function that takes a parameter of an integer & returns the #

Posted 04 March 2009 - 11:36 AM

It is not at all the case that I didn't like your functions. I wrote the program to illustrate the steps taken to solve the problem.
Was This Post Helpful? 0
  • +
  • -

#8 lmarcil1982  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-February 09

Re: C++ Function that takes a parameter of an integer & returns the #

Posted 04 March 2009 - 11:41 AM

Oh okay. I just wanted to make sure that I was not totally wrong in my thinking of using the functions I did.

I'm still working on the new code to make it a little prettier and I am also getting a couple of errors that I am trying to solve.
Was This Post Helpful? 0
  • +
  • -

#9 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: C++ Function that takes a parameter of an integer & returns the #

Posted 04 March 2009 - 11:53 AM

What kind of errors are you receiving?
Was This Post Helpful? 0
  • +
  • -

#10 lmarcil1982  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-February 09

Re: C++ Function that takes a parameter of an integer & returns the #

Posted 04 March 2009 - 01:06 PM

Oh YAY! Nevermind, I figured out my errors. I added a userloop to my program so that the user can run it over and over again, but I forgot to declare my
char userAnswer;
bool finished = false;


and then I had a function within a function, which we all know that does not work very well. I thank you SO much for your help. You have no idea how long I have been staring at my program trying to figure out what I have done wrong. I made myself go through every peice of my first code vs. the code that you helped me restructure and it makes so much more sense to me! I'm really starting to like C++, but I get so frusterated with myself when I forget one step that I know I know! =) You made my day!
I never knew I could get this excited over programming! I'm actually an accounting student and the 4 year that I am transfering to requires C++ and VB6 (which is next quarter!), so this is the first time I have ever even touched any kind of programming.
Hahaha, sorry about my book!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1