here is the exact assignment:
Quote
* Write a program that will prompt the user to enter 10 integers and print the average
of all the odd numbers entered.
* if a negative number is entered, print the average of the odd numbers entered up to
that point and exit the program.
* Use only variables of type int. The average should include the fractional part.
of all the odd numbers entered.
* if a negative number is entered, print the average of the odd numbers entered up to
that point and exit the program.
* Use only variables of type int. The average should include the fractional part.
This is what I have:
#include <stdio.h>
#define N 10
main ()
{
unsigned long int num;
unsigned long int sum;
unsigned long int odd;
unsigned long int average;
unsigned long int i;
printf("\nThis program will prompt user to enter ten integers and I will print the average\n");
printf("of all the odd numbers entered. If a negative number is entered, I will print the\n");
printf("average of the odd numbers entered up to that point and then I will exit.\n");
odd = ((2 * num) + 1);
for (i = 1; i <= N; ++i) {
for (odd = i; odd <= num; odd++) {
printf("\nEnter integer %lu: ", i);
scanf("%lu", &num);
sum = sum + num;
}
average = odd / N;
}
printf("\nYour average for the odd numbers entered is %.2f\n", (float)average);
return 0;
}
I noticed the problem I am having with the average is that I am assuming all the numbers entered will be odd, so I divide by N (or 10). What if out of the ten numbers entered, only three are odd, how would I correctly divide by only 3 to compute the average?
This assignment is not worth any point, its just meant for use to get used to thinking in computational terms. So please if you can do it, post code so I can better understand.

New Topic/Question
Reply




MultiQuote





|