Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 2nd, 2008, 2:23 AM   #1
LAYAN-2008
Newbie
 
Join Date: Apr 2008
Posts: 3
Rep Power: 0 LAYAN-2008 is on a distinguished road
find out the average of odd numbers -do-while statement.

hi , I want to know if my answer is true??and I want your opinion in my answer..
• Write a program fragment to find out the average of odd numbers between two numbers given by the user using a do-while statement?


int sum=0;
int X=50;
int Y = 70;
int count=0;
do{
X++;
if(X%2!=0)
{
sum +=X;
count++;
}
}
while(X<Y);
int average = sum/count;
System.out.println("average=" + average );
LAYAN-2008 is offline   Reply With Quote
Old May 2nd, 2008, 2:41 AM   #2
Freaky Chris
Hobbyist Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 169
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: find out the average of odd numbers -do-while statement.

Looks like it would give the correct results, without testing i will say that you may have to change < to <= and possible change the point at which you increment X.

P.S Use [code] tags

Chris
__________________
Who said i couldn't program
sarcasm = raw_input('Type in a sarcastic remark: ')
print sarcasm
Freaky Chris is offline   Reply With Quote
Old May 2nd, 2008, 3:40 AM   #3
JONY SMITH
Newbie
 
Join Date: May 2008
Posts: 2
Rep Power: 0 JONY SMITH is on a distinguished road
Re: find out the average of odd numbers -do-while statement.

Quote:
Originally Posted by Freaky Chris View Post
Looks like it would give the correct results, without testing i will say that you may have to change < to <= and possible change the point at which you increment X.

P.S Use [code] tags

Chris
yes it will give you right answer possibly. Try with <= and note out different hope one will be the right answer.
__________________
Mr.Smith
Offshore Software Development
JONY SMITH is offline   Reply With Quote
Old May 2nd, 2008, 7:45 AM   #4
tomisinbamgbelu
Programmer
 
Join Date: May 2008
Posts: 5
Rep Power: 0 tomisinbamgbelu is on a distinguished road
Send a message via Yahoo to tomisinbamgbelu
Re: find out the average of odd numbers -do-while statement.

your code is quite right but the question says given by the user. ending with an odd no is not provided for, so the while statement should read x<=y; so as 2 test d last number, in case its odd.
tomisinbamgbelu is offline   Reply With Quote
Old May 6th, 2008, 3:24 PM   #5
misho
Newbie
 
Join Date: Apr 2008
Posts: 10
Rep Power: 0 misho is on a distinguished road
Re: find out the average of odd numbers -do-while statement.

The code as you have written it should work; however, that's based on X and Y being even. If X were odd, the "X++;" line would be bad. If Y were odd, the "X<Y" condition would be bad.
For arbitrary X and Y, you'd either need to modify the rest of the code, or add something like the following near the beginning:

c++ Syntax (Toggle Plain Text)
  1. if(X%2 != 0) X--;
  2. if(Y%2 != 0) Y++;

I'd also add that since consecutive odd numbers are 2 apart, incrementing by 1 then checking if your number is odd or even is kind of wasteful. Something like the following would be better:

c++ Syntax (Toggle Plain Text)
  1. //assuming X and Y have been declared and are even by this point
  2. //and that Y>X
  3. int count = 0, sum = 0;
  4. X++;
  5. do {
  6. sum += X;
  7. count++;
  8. X += 2; //or X++;X++; , not really sure which is faster
  9. } while (X < Y);
  10. int average = sum / count;

I was going to point out that average may not be an int, but you can prove that the average is (X+Y)/2 (again, X and Y are both even, or both odd, I guess), so it has to be.
misho is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
code to add numbers in an array truBlu1 Java 9 Oct 12th, 2007 10:08 PM
random Numbers in openGL csrocker101 C++ 5 Apr 24th, 2007 8:02 PM
Vector problem - find max, min and positions codylee270 C++ 12 Mar 20th, 2006 12:57 AM
Random Number & Average Problem Hadrurus Java 6 Aug 15th, 2005 1:08 PM
Program that can find a patern Anyways C++ 2 Mar 22nd, 2005 2:27 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:38 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC