Thread: while loop

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    29

    while loop

    I am trying to design a program that ask the user to enter 5 integer, then the program will calculate the average

    Code:
    #include<stdio.h>
    
    
    int main()
    {
    	int num;
    	int counter, total, average;
    	
    	total=0;
    	counter =0;
    	while ( counter<5 ) {
    	printf("Enter number:"); 
    	scanf("%d" , &num );
    		total += num;  
    	} 
    
    
    	average= total/5;
    
    
    	printf("average is %d" , average);
    
    
    	return 0;
    }
    The error I got it is an infinite loop .
    what should i do about it ?

    thank you

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Within the body of the while loop, you might want to change the value of counter. Otherwise it will always have a value of zero, which is always less than five.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    @loongkee your counter is not incrementing hence the value of counter will always remain zero and hence infinite loop

    try the below.
    Code:
    #include<stdio.h>
    
    
    int main()
    {
        int num;
        int counter, total, average;
    
        total=0;
        counter =0;
        while ( counter<5 ) {
        printf("Enter number:"); 
        scanf("%d" , &num );
            total += num;  
          counter++;
        } 
    
    
        average= total/5;
    
    
        printf("average is %d" , average);
    
    
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    Quote Originally Posted by grumpy View Post
    Within the body of the while loop, you might want to change the value of counter. Otherwise it will always have a value of zero, which is always less than five.
    yes, you forget to set counter. when you enter one number, it must beincreased.

  5. #5
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    alternatively
    Code:
    #include<stdio.h>
    
    int main(void){
        int num;
        int counter, total;
    
        total = counter = 0;
    
        while (counter++ < 5) {
           printf("%d: Enter number:",counter);
           scanf("%d" , &num );
           total += num;
        }
    
        printf("average is %d" , (total/5) );
    
    
        return 0;
    }
    I cleaned it up a bit but the idea is that you can use the ++ operator to incrment your counter variable without a separate
    Code:
    counter++
    statement
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    29
    ok ..got it ..
    Thank you very much !!

    I have another problem

    I put float , but the answer give integer . May I know what's wrong ?




    Code:
    #include<stdio.h>
    
    
    void fnReadMarks(int* , int* );
    float fnCalcAvg( int , int );
    void fnPrint(float);
    
    
    int main()
    {
    	int a,b;
    	float avg;
    
    
    	fnReadMarks( &a , &b );
    
    
    	avg=fnCalcAvg(a , b);
    
    
    	fnPrint(avg);
    
    
    	return 0;
    }
    
    
    void fnReadMarks(int*x , int*y )
    {
    	printf("Enter 2 value:");
    	scanf("%d%d", x, y );
    }
    
    
    float fnCalcAvg( int x , int y )
    {
    	float average;
    	average= (x+y)/2;
    	return(average);
    }
    
    
    void fnPrint( float avg1)
    {
    	printf("The average is %.2f", avg1);
    }

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    Code:
    float fnCalcAvg( int x , int y )
    {
        float average;
        average= (x+y)/2;
        return(average);
    }
    x and y are ints, when you devide int by an int, you get an int, THEN it assigns it to average, a float (double)

    you either need to cast an int to a float (double) for the function, or change the ints to floats (double)

    normally this is one of the first things taught in classes.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by loongkee View Post
    Code:
    float fnCalcAvg( int x , int y )
    {
    	float average;
    	average= (x+y)/2;
    	return(average);
    }
    x+y is computed using integer arithmetic, and then divided by 2, again using integer arithmetic (which will truncate, and any fractional part is lost). Only then, is the resultant integer converted to float, in order to store the value in average. The fact that average is of type float does not make the addition or the division occur using floating point.

    If you want the calculation done using floating point, you have to force conversion to float before doing the division. Options might include
    Code:
        average = (x + y)/2.0f;      /*   2.0f is of type float, so (x + y) is computed as an integer and then converted to float, before dividing by 2.0f.   */
    or
    Code:
        average = (x + y)/((float)2);    /*   converts int 2 to float explicitly.  Since now dividing by float, the value of (x + y) is converted to float, before doing the division */
    or
    Code:
        average = ((float)x + y)/2;      /*  x is converted to float explicitly.   Because y is being added to a float, y is converted to float before doing the addition.   Then (since ((float)x + y) is of type float, 2 is also converted to float, before doing the addition */
    There are many other possibilities.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Dec 2012
    Posts
    29
    ok..thank you

    I am very weak in C ..

  10. #10
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by loongkee View Post
    ok..thank you

    I am very weak in C ..
    We all were at one point and if you're paying attention you realize you learn something new everyday. C is a succint language but has many subtleties that can lead to unintended consequences. The more you use it the more you learn. Keep it up.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  2. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  3. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  4. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  5. While loop ..crating an infanat loop
    By fmchrist in forum C Programming
    Replies: 7
    Last Post: 01-14-2006, 10:52 AM