943,907 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1083
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 24th, 2009
0

i need help pleaz >>

Expand Post »
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 8:02 pm.
Similar Threads
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
bahr_alhalak is offline Offline
53 posts
since Oct 2008
Jun 24th, 2009
0

Re: i need help pleaz >>

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 8:09 pm.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Jun 24th, 2009
0

Re: i need help pleaz >>

the problem that i need to use this program in another way
for example : for looping , do-while looping
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
bahr_alhalak is offline Offline
53 posts
since Oct 2008
Jun 25th, 2009
0

Re: i need help pleaz >>

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.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,729 posts
since May 2006
Jun 25th, 2009
0

Re: i need help pleaz >>

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 5:06 am.
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
bahr_alhalak is offline Offline
53 posts
since Oct 2008
Jun 25th, 2009
0

Re: i need help pleaz >>

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 6:15 am.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jun 25th, 2009
0

Re: i need help pleaz >>

i need another solution for for looping
Last edited by bahr_alhalak; Jun 25th, 2009 at 6:39 am.
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
bahr_alhalak is offline Offline
53 posts
since Oct 2008
Jun 25th, 2009
0

Re: i need help pleaz >>

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 7:13 am.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jun 25th, 2009
0

Re: i need help pleaz >>

>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 7:10 am.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jun 25th, 2009
0

Re: i need help pleaz >>

Click to Expand / Collapse  Quote originally posted by iamthwee ...
>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 7:16 am.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC