I am trying to write a program that stores the seperately the tota of 10 inputted numbers depending on whether they are odd or even, and also add counters for how many odd or even numbers there are. For example, if the inputted numbers are 1.2.3.4.5.6.7.8.9.10 tit shoulde be 5 odd and 5 even numbers, the total of odds should be 25 and the total of evens should be 30.
The problem is, I can't understand how to seperate my odds and evens to different counters with my booleans. It always counts everything just as odds, or as even numbers.
#include <iostream>
#define N 10
using namespace std;
void outputResults(int numOfOdd, int oddTotal, int numOfEven, int evenTotal) ;
for (int i = 0; i < N; i++) {
numIsOdd(myNum[i]);
isEven(myNum[i]);
}
outputResults(numOfOdd, oddTotal, numOfEven, evenTotal);
cin.get();
return 0;
}
bool isOdd(int myNum){
return myNum % 2 == 0;
}
bool isEven(int myNum) {
return myNum % 1 == 0;
}
void outputResults(int numofOdd, int oddTotal, int numOfEven, int evenTotal) {
for (int i = 0; i < N; i++) {
if (isOdd) {
myNumOdd = myNum[N];
for (int i = 0; i < N; i++) {
for (int n = 0; n < i; n++) {
oddTotal = (myNum[n] + myNumOdd);
}
}
}
}
for (int i = 0; i < N; i++) {
if (isEven) {
myNumEven = myNum[N];
for (int i = 0; i < N; i++) {
for (int t = 0; t < i; t++) {
evenTotal = (myNum[t] + myNumEven);
}
}
}
}
cout << " Number of Odds is " << numofOdd << " numer of even is " << numOfEven << endl;
cout << " Even Total is :" << evenTotal << " Odd total is " << oddTotal << endl;
In isEven(), doing a modulo with 1 will ALWAYS leave a result of zero with positive integers. So every number, odd or even, is treated as an even number.
In isOdd(), doing a modulo with 2 with a zero result is the proper way to check if a number is even, not odd.
int
main()
{
cout << " Please input 10 numbers " << endl;
for (int i = 0; i < N; i++) {
std::cin >> myNum;
// If the number is even then update evenTotal and numOfEven
// else update oddTotal and numOfOdd
}
// Print out the results
outputResults(numOfOdd, oddTotal, numOfEven, evenTotal);
cin.get();
return 0;
}
bool
isEven(int myNum)
{
return myNum % 2 == 0;
}
void
outputResults(int numofOdd, int oddTotal, int numOfEven, int evenTotal)
{
cout << " Number of Odds is " << numofOdd << " numer of even is " << numOfEven <<
endl;
cout << " Even Total is :" << evenTotal << " Odd total is " << oddTotal << endl;
}
number_of_odds = 10 - no_of_evens ( or N - no_of_evens )
OP's effort, as strenuous as it appears, would have benefited from some sort of pseudocode plan before punching out code.
setup odds and evens piles and other counters, blah blah
:loop 10 times
get a number
if number is odd
put number in the 'next available spot' on the odds pile
keep count of odds
otherwise put number -ditto- on the evens pile
loop back:
display results
EDIT: On reviewing OP numbers don't have to be put into separate 'piles' (read containers of some sort, only the odd/even total's etc, and count of at least one type)