Odds and evens

I want to tally the number of odd and even numbers that i have in a file into a prototype. I already have the file open in a prototype call readData. so far i have this

void oddTally(int num)
{
	ifstream inputFile;
	int tally = 0;

	readData(num);
	
	if ((num) % 2 != 0);
	tally++;

}


from this point im kind of lost, someone help?
Last edited on
That tally is just counting the numbers that are not divisible by 2, that is the odd numbers, and you're ignoring the even numbers.

You can keep a tally, but that's redundant as you can always add the odd and even numbers together to get tally,

That leaves you with:
1
2
3
4
5
6
7
8
9
10
11
12
void getCounts(unsigned& odd, unsigned& even)
{
	ifstream inputFile;

	int num;
	readData(num);
	
	if ((num % 2) != 0)
		++odd;
	else
		++even;
}


All you have to do is read each number from the file and do that calculation until the file is empty.
Last edited on
Its that I need only the odd numbers because in other prototype I'm going to tally the even numbers and in a different prototype i'm going to find the average of odds and evens individually
Last edited on
Its that I need only the odd numbers because in other prototype I'm going to tally the even numbers and in a different prototype i'm going to find the average of odds and evens individually


It's good to think about efficiency in your code. The algorithm you've described requires reading the file 4 times. It would be much faster if you read it only once:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void getStats(ifstream &is, unsigned &sumOfOdds, unsigned &countOfOdds,
                      unsigned &sumOfEvens, unsigned &countOfEvens)
{
    int num;
    sumOfOdds = countOfOdds = sumOfEvens = countOfEvens = 0;
    while (is >> num) {
        if (num%2) {
            sumOfOdds += num;
            ++countOfOdds;
        } else {
            sumOfEvens += num;
            ++countOfEvens;
        }
    }
}


Last edited on
I know it would be faster, but the teacher is asking to do it that way
This is your code:
1
2
3
4
5
6
7
8
void oddTally(int num)
{
	ifstream inputFile;
	int tally = 0;
	readData( num );
	if ( (num) % 2 != 0 ) ;
	tally++;
}

This is exactly the same code, with only style changes:
1
2
3
4
5
6
7
8
9
10
void oddTally(int num)
{
	ifstream inputFile;
	int tally = 0;
	readData( num );
	if ( 0 != (num) % 2 ) {
	}

	++tally;
}

In other words you increment the tally whether you have odd or even num.

Now, there are questions:
* What does the readData() do, exactly?
* What is the point of completely unused variable 'inputFile'?
* You do get some 'tally', but nobody will ever know it. Do you guess why?
readData opens a file of 20 numbers. It's just an assignment, im not really sure why the inputFile is there actually, I thought that it was to bring readData into oddTally
You say that "readData opens a file". How does that affect the integer variable 'num'?
well the file has this numbers
25
41
58
96
42
52
63
47
85
96
100
58
99
31
84
94
20
49
87
67
I just want them to be separated by odd in one prototype (oddTally) and even in other (evenTally) then find oddSum and evenSum. Also, oddAverage and evenAverage, but it's not working my oddTally
Topic archived. No new replies allowed.