# nested for loop; average of odd number

Page 1 of 1

## 4 Replies - 2083 Views - Last Post: 22 February 2011 - 07:00 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=217969&amp;s=99f505d5675f63547e0b5dbab5af810b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 iqbalmmz

Reputation: 0
• Posts: 92
• Joined: 15-February 11

# nested for loop; average of odd number

Posted 22 February 2011 - 01:18 PM

i was wondering if anyone here can help me with this program. either give me pointers or help me write some of the code. i'm trying to write a program that prompts the user to enter 10 arbitrary numbers, of those 10 arbitrary numbers entered, I want the program to compute the average of only the odd numbers.

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.

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.

Is This A Good Question/Topic? 0

## Replies To: nested for loop; average of odd number

### #2 chinchang

• Indie Game Developer

Reputation: 192
• Posts: 725
• Joined: 22-December 08

## Re: nested for loop; average of odd number

Posted 22 February 2011 - 01:26 PM

Here is what you need to do :

1. Initialize 2 variables sum & counter to zero.
2. Make a loop that runs N times (later convert to end with a -ve integer).
3. In a loop iteration do the following :
- get a number as input from user.
- Check if its odd or not.
- If number is odd, add it to sum and increment the variable counter.
4. When you get out of the loop divide sum by counter to get the average.

### #3 iqbalmmz

Reputation: 0
• Posts: 92
• Joined: 15-February 11

## Re: nested for loop; average of odd number

Posted 22 February 2011 - 02:56 PM

chinchang, on 22 February 2011 - 01:26 PM, said:

Here is what you need to do :

1. Initialize 2 variables sum & counter to zero.
2. Make a loop that runs N times (later convert to end with a -ve integer).
3. In a loop iteration do the following :
- get a number as input from user.
- Check if its odd or not.
- If number is odd, add it to sum and increment the variable counter.
4. When you get out of the loop divide sum by counter to get the average.

okay so I did what you said but the output i'm getting doesn't seem to be correct. here is what I have now

```#include <stdio.h>
#define N 10
main ()
{
int num;
int sum = 0;
int counter = 0;
int i;

for (i = 1; i <= N; i++) {
printf("\nEnter number: ");
scanf("%d", &num);
} if (num = 2*num + 1) {
sum += num;
counter++;
}

printf("Your average is %.2f\n", (float)sum / counter);
return 0;
}

```

am I suppose to incorporate the if into the for loop?

### #4 Crutoy

Reputation: 13
• Posts: 70
• Joined: 10-January 11

## Re: nested for loop; average of odd number

Posted 22 February 2011 - 03:41 PM

you pretty much almost have it, except that youre if statement is wrong, the way you have it it will always be true, you want to check if number is odd then add it to sum and increment a counter.
if (num = 2*num + 1)

```#include "stdafx.h"
#include <iostream>

using std::cout;
using std::cin;

int main()
{
int number,sum = 0,odd = 0;

for(int i = 0; i < 10; i++)
{
cin >> number;

if((number % 2) != 0)
{
sum+= number;
odd++;
}

}
cout << sum/ odd;
cin.get();
cin.get();
return 0;
}

```

### #5 iqbalmmz

Reputation: 0
• Posts: 92
• Joined: 15-February 11

## Re: nested for loop; average of odd number

Posted 22 February 2011 - 07:00 PM

Crutoy, on 22 February 2011 - 03:41 PM, said:

you pretty much almost have it, except that youre if statement is wrong, the way you have it it will always be true, you want to check if number is odd then add it to sum and increment a counter.
if (num = 2*num + 1)

```#include "stdafx.h"
#include <iostream>

using std::cout;
using std::cin;

int main()
{
int number,sum = 0,odd = 0;

for(int i = 0; i < 10; i++)
{
cin >> number;

if((number % 2) != 0)
{
sum+= number;
odd++;
}

}
cout << sum/ odd;
cin.get();
cin.get();
return 0;
}

```

Thanks chinchang and crutoy, I got it working. I noticed I should have incorporated the if statement into the for loop. Here is what I got at the end:

```#include <stdio.h>
#define N 10
main ()
{
int num;
int sum = 0;
int counter = 0;
int i;

for (i = 1; i <= N; i++) {
printf("\nEnter number: ");
scanf("%d", &num);
if (num%2!=0) {
sum += num;
counter++;
}

}

printf("Your average is %.2f\n", (float)sum / counter);
return 0;
}

```

I must say, I am getting the hang of this stuff