CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2006
    Posts
    5

    Talking How to Do ( average ) By using the While Loop ?? (( C Language ))

    Hello all,

    Please I have a Question !

    I have to do this Question in my college :

    I must do a program in C Language . They want me to add 5 numbers then cumpute the average by using while loop !

    How to solve it ?

    I know how to compute the average by another way , but in this Question they need to do it by while loop .

    Please help me.

    and thank you all.


    Best regards,
    Girl 24

  2. #2
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: How to Do ( average ) By using the While Loop ?? (( C Language ))

    We don't do homework here. You need to try it yourself and show us some code before we will help you.

    You said that the specification is 5 numbers. The structure of a while loop is that it ends when the condition is false, although it can also be terminated early. Thus:

    Code:
    while ( condition )
    {
      do_stuff
    }
    You should therefore choose your condition such that it will be false once 5 numbers have been entered. To do this you will need to modify something in the loop such that having entered 5 numbers the condition becomes false.

    This might be one way:
    Code:
    int remaining_numbers = 5;
    while ( remaining_numbers > 0 )
    {
      get_a_number_and_do_stuff();
      --remaining_numbers;
    }
    I will now leave it to you to write the proper code.

  3. #3
    Join Date
    Dec 2006
    Posts
    5

    Re: How to Do ( average ) By using the While Loop ?? (( C Language ))

    Quote Originally Posted by NMTop40
    We don't do homework here. You need to try it yourself and show us some code before we will help you.
    Aww . Do you think that Iam lazy and want some one to solve it ? . No Iam not. I solve all my homework questions, but this one I stopped and I tried to do it but I didn't reach the right way.

    Thank you for helping me . Yes you are right. I did what you say. here it is :

    #include <stdio.h>
    main ()
    {
    float num,avg,sum;
    int i;
    i = 5;
    sum = 0;
    while ( i > 0 )
    {
    printf("Enter a number");
    scanf("%f",&num);
    sum = sum + num;
    --i;
    }
    avg = sum / 5;
    printf("The average =%f",avg);
    }



    Thank you NMTop40

    Best regards,
    Girl 24

  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: How to Do ( average ) By using the While Loop ?? (( C Language ))

    Hey, Girl! Welcome to CG!
    Quote Originally Posted by Girl 24
    main ()
    It should be int main().
    Quote Originally Posted by Girl 24
    i = 5;
    You can remove this limitation by asking the user to enter the count of numbers he wants to calculate the average for. using scanf as you did for individual numbers.

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: How to Do ( average ) By using the While Loop ?? (( C Language ))

    Quote Originally Posted by Girl 24
    Aww . Do you think that Iam lazy and want some one to solve it ? . No Iam not. I solve all my homework questions, but this one I stopped and I tried to do it but I didn't reach the right way.

    Thank you for helping me . Yes you are right. I did what you say. here it is :
    Code:
    #include <stdio.h>
    main ()
    {
     float num,avg,sum;
     int i;
     i = 5;
     sum = 0;
     while ( i > 0 )
     {
      printf("Enter a number");
      scanf("%f",&num);
      sum = sum + num;
      --i;
      }
      avg = sum / 5;
      printf("The average =%f",avg);
      }
    Thank you NMTop40

    Best regards,
    Girl 24
    For someone learning C that is not bad. Just a couple of little things:

    - use int as the return type for main() although I think having no return type (explicit int) make be allowable in C (even though it isn't in C++).

    - You may initialise your variables on the lines on which they are declared. So

    Code:
    float sum=0.0;
    - Preferable to use variables that have a meaning, so nums_remaining (even though it is a bit more typing) instead of i.

    - You might want a few spaces and new lines in your output.

    Also when you post code on codeguru forums, enclose it in code tags, that is, begin with [ code ] and end with [ /code ] (but without the spaces).

  6. #6
    Join Date
    Dec 2006
    Posts
    5

    Re: How to Do ( average ) By using the While Loop ?? (( C Language ))

    WoOoOW . you are perfect !

    Thank you exterminator and NMTop40.

    Your notes are very perfect and Iam so happy. thanks alot alot alot.


    Best regards,
    Girl 24

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured