4 Replies - 2083 Views - Last Post: 22 February 2011 - 07:00 PM Rate Topic: -----

#1 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 iqbalmmz  Icon User is offline

  • D.I.C Head

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

Re: nested for loop; average of odd number

Posted 22 February 2011 - 02:56 PM

View Postchinchang, 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?
Was This Post Helpful? 0
  • +
  • -

#4 Crutoy  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • 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;
}



Was This Post Helpful? 1
  • +
  • -

#5 iqbalmmz  Icon User is offline

  • D.I.C Head

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

Re: nested for loop; average of odd number

Posted 22 February 2011 - 07:00 PM

View PostCrutoy, 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 ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1