Some help in C Programming, not C++ please.

Techie333

Platinum Member
Jan 20, 2001
2,368
0
0
Hi guys, need some help in C Programming HW. Here are the instructions:

Write a program that reads integers until 0 is entered. After input terminates, the program should report the total number of even integers (excluding the 0) entered, the average value of the even integers, the total numbers of odd integers entered, and the average value of the odd integers. Do this using the switch statement.

Here is what I've got so far. Numbers only apply for 1-9 so I tried doing this, but doesn't work:

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

int main(void)
{
int evencount=0, oddcount=0;
float eventot=0, oddtot=0;
char ch;
printf("Enter some numbers and enter 0 at end to terminate.\n");
while ((ch = getchar ()) != '0')
{
switch(ch)
{
case '1' : oddcount++;
oddtot+=1;
case '2' : evencount++;
eventot+=2;
case '3' : oddcount++;
oddtot+=3;
case '4' : evencount++;
eventot+=4;
case '5' : oddcount++;
oddtot+=5;
case '6' : evencount++;
oddtot+=6;
case '7' : oddcount++;
oddtot+=7;
case '8' : evencount++;
eventot+=8;
case '9' : oddcount++;
oddtot+=9;
}
}

printf("\n\nThe total number of even integers is %d and the average of"
"\nthem is %d", evencount, (eventot/evencount));
printf("\n\nThe total number of odd integers is %d and the average of"
"\nthem is %d", oddcount, (oddtot/oddcount));
printf("\n\n");
system("PAUSE");
return 0;
}

Here's what the output looks like and I don't know why:
Enter some numbers and enter 0 at end to terminate.
2461350


The total number of even integers is 18 and the average of
them is -1908874354

The total number of odd integers is 21 and the average of
them is -204522252

Press any key to continue . . .
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
You need to have a `break` for each case, not having the break after each case will cause execution to 'fall' thru every case block below the triggered case until it sees break or exits the switch .
if ch was ''1' then it would execute the code for case '1', case '2',case '3'....,case '9', as there is no break tellling it to exit the switch after case '1'.
The reason it doesn't automatically break is to allow for this sort of a switch:
switch(ch)
{
case '1':
case '3':
case '5':
printf("1,3 or 5 press");
break;

}
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
How about explaining why it does not work?

Also, get rid of the case statement and use a modulo %2. simpler and actually performs according to the requirements.

Also, because this is homework, do not expect a lot of assistance other than guidance and you will get some flaming for posting HW here.
 

Techie333

Platinum Member
Jan 20, 2001
2,368
0
0
Thanks! The break worked and I modified for it to work with modulous, but the averages won't work! I just keep getting 0. Here is the new one:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

int main(void)
{
int evencount=0, oddcount=0;
float eventot=0, oddtot=0;
char ch, dh;
printf("Enter some numbers and enter 0 at end to terminate.\n");
while ((ch = getchar ()) != '0') \\getting input till 0
{
dh=ch-48; \\converts from character to decimal
switch(dh%2)
{
case 0 : evencount++;
eventot+=dh;
break;
default : oddcount ++;
oddtot+=dh;
break;
}
}
printf("\n\nThe total number of even integers is %d and the average of"
"\nthem is %d", evencount, (eventot/evencount));
printf("\n\nThe total number of odd integers is %d and the average of"
"\nthem is %d", oddcount, (oddtot/oddcount));
printf("\n\n");
system("PAUSE");
return 0;
}
Here is what the output looks like now:
Enter some numbers and enter 0 at end to terminate.
2465140


The total number of even integers is 4 and the average of
them is 0

The total number of odd integers is 2 and the average of
them is 0

Press any key to continue . . .
 

itachi

Senior member
Aug 17, 2004
390
0
0
an average of 0 is impossible.. your code doesn't handle casting.

this code should work.
 

ys

Senior member
Oct 10, 1999
757
0
0
You can put printfs in each case or at the end of the loop to see what's being assigned to the variables.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
for full credit you might want to use an "if" for each of the averages, to test for evencount==0 or oddcount==0
 

Techie333

Platinum Member
Jan 20, 2001
2,368
0
0
Originally posted by: itachi
an average of 0 is impossible.. your code doesn't handle casting.

this code should work.
I really appreciate your help but I don't really understand that code. Some of that stuff is probably beyond what I have done, so I don't think I will be using that plus the professor might think it isn't my work. Anybody else can modifiy my code to work?
 

Techie333

Platinum Member
Jan 20, 2001
2,368
0
0
Originally posted by: ys
You can put printfs in each case or at the end of the loop to see what's being assigned to the variables.
For some reason it puts 0 in for eventot and oddtot, probably typecasting as somebody mentioned before, any ideas on how to fix?
 

Techie333

Platinum Member
Jan 20, 2001
2,368
0
0
nevermind, I figured it out! Thanks for your help guys. I just typecasted into ints and floats.
 

jalaram

Lifer
Aug 14, 2000
12,920
2
81
First, you're dividing an int with a float. Second, look at your printf statements. Notice how the first variable prints out fine and the second doesn't.

Edit: N/m. Just saw that you figured it out.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
typecast the count to a float when you are doing the calculation for the averages.
Then take the calculation and cast if back to an integer for the final output.

printf("\n\nThe total number of even integers is %d and the average of"
"\nthem is %d", evencount, (int)(eventot/(float)evencount));
printf("\n\nThe total number of odd integers is %d and the average of"
"\nthem is %d", oddcount, (int)(oddtot/(float)oddcount));