Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

separate averages of odd and even inputted integer numbers series

Status
Not open for further replies.

talidyce

Technical User
Mar 16, 2001
10
AU
I am writing a program to take a series of positive integers as input, and separately calculate and print the averages of the odd an even numbers, using a negative trip-value as end-of-data flag.
I can't figure out how to get the loop to return to the even series after jumping to the odd.
Any help would be greatly appreciated.
 
I'm not sure I understand your problem fully. Why can't you do all the calculations on the fly?

Set up 2 variables, one for odd numbers, one for even

While user input
Get number
Figure out if it's odd or even
Add the number to the appropriate variable.

Calculate averages


Russ
bobbitts@hotmail.com
 
# include <stdio.h>
# include <conio.h>

void main()
{
int Num = 1,no_of_odd = 0,no_of_even = 0;
int OddsTotal,EvensTotal;
float avg_even,avg_odd;
printf(&quot;THIS PROGRAM CACULATES ODDS AVG AND EVENS AVG DIFFERENTLY&quot;);
while(Num > 0)
{
printf(&quot;Enter a negetive number to Exit :&quot;);
scanf(&quot;%d&quot;,&Num);
if(num <= 0) break;
if(num%2==0)
{
EvensTotal+=num;
no_of_even++;
}
if(num%2!=0)
{
OddsTotal+=num;
no_of_odd++;
}
}
avg_even = EvensTotal/no_of_even;
avg_odd = OddsTotal/no_of_odd;
printf(&quot;Average of Even Nos. Entered : %f&quot;,avg_even);
printf(&quot;Average of Odd Nos. Entered : %f&quot;,avg_odd);
}

So this is the code arq.


I hope this was not an assignment.
Happy Learning,
SwapSawe.
 
># include <stdio.h>
># include <conio.h>

Don't need this header

>
>void main()

main() returns int:

int main()

>{
>int Num = 1,no_of_odd = 0,no_of_even = 0;
>int OddsTotal,EvensTotal;

You have to initialize these to 0:

int OddsTotal=0,EvensTotal=0;

>float avg_even,avg_odd;
>printf(&quot;THIS PROGRAM CACULATES ODDS AVG AND EVENS AVG >DIFFERENTLY&quot;);

fflush(stdout);

Since output is usually line-buffered, this ensures the user will see the prompt before they can enter input.

>while(Num > 0)
> {
> printf(&quot;Enter a negetive number to Exit :&quot;);

fflush(stdout);

> scanf(&quot;%d&quot;,&Num);
> if(num <= 0) break;

0 isn't a legal value?

> if(num%2==0)
> {
> EvensTotal+=num;
> no_of_even++;
> }
> if(num%2!=0)
> {
> OddsTotal+=num;
> no_of_odd++;
> }
> }
>avg_even = EvensTotal/no_of_even;
>avg_odd = OddsTotal/no_of_odd;
>printf(&quot;Average of Even Nos. Entered : %f&quot;,avg_even);
>printf(&quot;Average of Odd Nos. Entered : %f&quot;,avg_odd);

printf(&quot;Average of Even Nos. Entered : %f\n&quot;,avg_even);
printf(&quot;Average of Odd Nos. Entered : %f\n&quot;,avg_odd);

>}
>
>So this is the code arq.
>
>
>I hope this was not an assignment.

It probably was, that's why I didn't post any code. I always make it a point not to do so unless the OP demonstrates that they've put some of their own effort into the problem.

Russ
bobbitts@hotmail.com
 
Thank you very much for the quick and informative answers rbobbitt and SwapSawe.
This fixed my problem. It was indeed part of an assignment and I had been trying to work out this particular one for 3 days and in frustration decided to call for help. We were going to review the questions with the teacher and finalise them before handing in the assignment, so I did not feel I was cheating by seeking third party help.
Thanks again for helping :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top