i need help pleaz >>

Reply

Join Date: Oct 2008
Posts: 50
Reputation: bahr_alhalak has a little shameless behaviour in the past 
Solved Threads: 0
bahr_alhalak bahr_alhalak is offline Offline
Junior Poster in Training

i need help pleaz >>

 
0
  #1
Jun 24th, 2009
hello every body

i have a problem with a c program .
the question that ( write a c program that reads a real numbers untill error then calculate the sum and the average of all numbers )

i have solve it with a while looping . and i need it with adiffrent looping .

  1. #include<stdio.h>
  2.  
  3. void main()
  4. {
  5. float total,average;
  6. int s;
  7. float a;
  8.  
  9. while((scanf("%f",&a)>0))
  10. {
  11. total=total+a;
  12. s=s+1;
  13. }
  14. average =total/s;
  15. printf("The sum of numbers are %f and the average is %f",total,average);
  16. }
Last edited by bahr_alhalak; Jun 24th, 2009 at 7:02 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,522
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 127
William Hemsworth William Hemsworth is offline Offline
Posting Virtuoso

Re: i need help pleaz >>

 
0
  #2
Jun 24th, 2009
I don't get it, what's your problem?

Also, don't use void main, replace it with int main.
Last edited by William Hemsworth; Jun 24th, 2009 at 7:09 pm.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 50
Reputation: bahr_alhalak has a little shameless behaviour in the past 
Solved Threads: 0
bahr_alhalak bahr_alhalak is offline Offline
Junior Poster in Training

Re: i need help pleaz >>

 
0
  #3
Jun 24th, 2009
the problem that i need to use this program in another way
for example : for looping , do-while looping
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 4,222
Reputation: WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future 
Solved Threads: 400
Moderator
WaltP's Avatar
WaltP WaltP is online now Online
Industrious Poster

Re: i need help pleaz >>

 
0
  #4
Jun 25th, 2009
Originally Posted by bahr_alhalak View Post
the problem that i need to use this program in another way
for example : for looping , do-while looping
So what's the problem? Doesn't your book describe a for loop and a do-while loop?

You need to try it, we aren't supposed to do it for you.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 50
Reputation: bahr_alhalak has a little shameless behaviour in the past 
Solved Threads: 0
bahr_alhalak bahr_alhalak is offline Offline
Junior Poster in Training

Re: i need help pleaz >>

 
0
  #5
Jun 25th, 2009
thaaaaaanx WaltP ;

i am still trying
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. int main()
  5. {
  6. float total,average;
  7. int s;
  8. float a;
  9. clrscr();
  10. do
  11. {
  12. total=total+a;
  13. s=s+1;
  14. }
  15. while((scanf("%f",&a)>0));
  16. average =total/s;
  17. printf("The sum of numbers are %f and the average is %f",total,average);
  18. }
Last edited by bahr_alhalak; Jun 25th, 2009 at 4:06 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2,025
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 224
tux4life's Avatar
tux4life tux4life is offline Offline
Postaholic

Re: i need help pleaz >>

 
0
  #6
Jun 25th, 2009
Don't use conio ( #include<conio.h> ), it's unportable!
To the OP: Are you encountering any problems with your current code? (as you only posted your new code)
Last edited by tux4life; Jun 25th, 2009 at 5:15 am.
"You can't build a reputation on what you are going to do."
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 50
Reputation: bahr_alhalak has a little shameless behaviour in the past 
Solved Threads: 0
bahr_alhalak bahr_alhalak is offline Offline
Junior Poster in Training

Re: i need help pleaz >>

 
0
  #7
Jun 25th, 2009
i need another solution for for looping
Last edited by bahr_alhalak; Jun 25th, 2009 at 5:39 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2,025
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 224
tux4life's Avatar
tux4life tux4life is offline Offline
Postaholic

Re: i need help pleaz >>

 
0
  #8
Jun 25th, 2009
Originally Posted by bahr_alhalak View Post
i need another solution for for looping
Changing it to a for loop is not that difficult, look up how a for loop works, or take a look at this small example:
  1. int i; // declare this variable before any statement in your code
  2. for(i = 0; i < 5; i++)
  3. {
  4. /* The code in this code block will run five times */
  5. }
And because you're so kind I've also Googled up a tutorial about Control structures (if, while, do-while, for): http://www.cplusplus.com/doc/tutorial/control/
Last edited by tux4life; Jun 25th, 2009 at 6:13 am.
"You can't build a reputation on what you are going to do."
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,321
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 384
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: i need help pleaz >>

 
0
  #9
Jun 25th, 2009
>for(int i = 0; i < 5; i++)

That code where 'i' is declared within the parenthesis
...
'for' loop initial declaration used outside C99 mode JFYI
Last edited by iamthwee; Jun 25th, 2009 at 6:10 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2,025
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 224
tux4life's Avatar
tux4life tux4life is offline Offline
Postaholic

Re: i need help pleaz >>

 
0
  #10
Jun 25th, 2009
Originally Posted by iamthwee View Post
>for(int i = 0; i < 5; i++)

That code where 'i' is declared with the parenthesis
...
'for' loop initial declaration used outside C99 mode JFYI
I didn't notice I was in the C forum, but according to C99 this was valid, however I edited my post to conform to the C89 standard

C89:
  1. int i; // declare this variable before any statement in your code
  2. for(i = 0; i < 5; i++)
  3. {
  4. /* The code in this code block will run five times */
  5. }

C99:
  1. for(int i = 0; i < 5; i++)
  2. {
  3. /* The code in this code block will run five times */
  4. }
Last edited by tux4life; Jun 25th, 2009 at 6:16 am.
"You can't build a reputation on what you are going to do."
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 821 | Replies: 16
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC