954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

i need help pleaz >>

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 .

#include<stdio.h>
 
void main()
{
 float total,average;
 int s;
 float a;
 
 while((scanf("%f",&a)>0))
 {
  total=total+a;
  s=s+1;
 }
 average =total/s;
 printf("The sum of numbers are %f and the average is %f",total,average);
}
bahr_alhalak
Junior Poster in Training
53 posts since Oct 2008
Reputation Points: 14
Solved Threads: 0
 

I don't get it, what's your problem?

Also, don't use void main, replace it with int main.

William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 

the problem that i need to use this program in another way
for example : for looping , do-while looping

bahr_alhalak
Junior Poster in Training
53 posts since Oct 2008
Reputation Points: 14
Solved Threads: 0
 
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.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

thaaaaaanx WaltP ;

i am still trying

#include<stdio.h>
#include<conio.h>
 
int main()
{
 float total,average;
 int s;
 float a;
 clrscr();
do
{
total=total+a;
  s=s+1;
}
 while((scanf("%f",&a)>0));
 average =total/s;
 printf("The sum of numbers are %f and the average is %f",total,average);
}
bahr_alhalak
Junior Poster in Training
53 posts since Oct 2008
Reputation Points: 14
Solved Threads: 0
 

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)

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

i need another solution for for looping

bahr_alhalak
Junior Poster in Training
53 posts since Oct 2008
Reputation Points: 14
Solved Threads: 0
 
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:

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

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/ :)

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

>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

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

>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:

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


C99:

for(int i = 0; i < 5; i++)
{
  /* The code in this code block will run five times */
}
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

If you want to write portable code (works on any system) it's best to use the c89 switch (and also turn up the warning level (-Wall).

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

there are a mistakes i dont know how to solve it .. i am trying :(

#include<stdio.h>
#include<conio.h>
 
void main()
{
 float total,average, s;
 int i;
 int a;
 clrscr();
  printf("Enter the numbers : ");
 scanf("%d",&a);
 for ( i=0;i<a ; i++)
 {
  total=total+a;
  s=s+1;
  }
   average =total/s;
 printf("The sum of numbers are %f and the average is %f",total,average);
}
bahr_alhalak
Junior Poster in Training
53 posts since Oct 2008
Reputation Points: 14
Solved Threads: 0
 

> void main()
Mentioned before, so I'll mention it again.
main returns an int, not void

> total=total+a;
Is the initial value of total zero?

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

there is more :(

bahr_alhalak
Junior Poster in Training
53 posts since Oct 2008
Reputation Points: 14
Solved Threads: 0
 

You mean "more" like the scanf which was inside the loop, and now isn't.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

bahr_alhalak,

This is your 43rd post and I think you have been quite familiar with
the standard c/c++. Its pity. You are not serious and ignoring the community. Senior members are always right with their valuable comments & suggestions. Please be gentle.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

i admire Senior members and their comments & suggestions.

i am just trying to discuse with them .
i want answer for my problem

bahr_alhalak
Junior Poster in Training
53 posts since Oct 2008
Reputation Points: 14
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You