Thread: Help - AVG function in Loop

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    2

    Help - AVG function in Loop

    I can't get the average function to work after the loop. I don't know if my accumulate counter isn't working or I have something wrong with my average...But its not outputting the right file. Any suggestions?
    Attached Images Attached Images Help - AVG function in Loop-screen-shot-2016-09-27-20-40-06-png 

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Is there a reason you decided to paste on image instead of code? From a glance nothing looks suspicious except for fflush(stdin); - how exactly is the file wrong?

  3. #3
    Registered User
    Join Date
    Sep 2016
    Posts
    2

    loop

    Quote Originally Posted by whiteflags View Post
    Is there a reason you decided to paste on image instead of code? From a glance nothing looks suspicious except for fflush(stdin); - how exactly is the file wrong?
    Yeah, I'm not sure why I didnt do that. I don't know whats off. Im getting a value of .1 for the avg output after the loop

    Code:
     #include
    Code:
    <stdio.h>
    
    int main (void)
    {
    
    
    /*Variable Delclarations*/
    /*----------------------*/
    
        int   trip;
        float mpg;
        float miles_driven = 0.0;
        float avg = 0.0;
        float gallons;
        float total_gallons = 0.0;
        float miles;
    
    /*Display program info*/
    /*--------------------*/
    
    printf ("Welcome to the mileage calculator.\n\n");
    printf ("This program will calculate the miles per gallon for you for three tanks of gas after you have entered the gallons used and miles driven.\n\n");
    
    /*User Input*/
    /*----------*/
    
        for (trip = 1; trip <= 3; ++ trip)
        {
    printf ("\nEnter the number of gallons used for tank #%d:", trip);
            scanf  ("%f", &gallons);
            fflush (stdin);
    
            total_gallons = gallons + total_gallons;
    
    printf ("Enter the number of miles driven: ");
            scanf  ("%f", &miles);
            fflush (stdin);
    
            mpg = miles / gallons;
            miles_driven = miles + miles_driven;
            avg = total_gallons / miles_driven;
    
    printf ("***The miles per gallon used for this tank is %.1f \n",mpg);
            fflush (stdin);
    
        } // end of loop
    
    /*Display Results*/
    /*---------------*/
    
    printf ("\nYour overall average miles per gallon for three tanks is %.1f \n", avg);
        fflush (stdin);
    
    printf ("\nThanks for using the Mileage calculator program!\n");
    
        return 0;
    } /* End Main */
    

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Actually now that I look, you have
    avg = total_gallons / miles_driven;
    when
    avg = miles_driven / total_gallons;
    is the right ratio.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Also fflush(stdin) is wrong. fflush() is only valid for output streams and update streams in which the last operation was a write. Its behavior is undefined for input streams.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While loop in insertNode function is an infinite loop
    By blongnec in forum C Programming
    Replies: 8
    Last Post: 03-19-2016, 09:57 PM
  2. Function to end a loop ?
    By frosty16 in forum C Programming
    Replies: 2
    Last Post: 11-08-2013, 02:05 PM
  3. cannot get loop to function
    By randomuser in forum C Programming
    Replies: 8
    Last Post: 08-30-2012, 05:22 PM
  4. function with loop within a loop
    By rabert1 in forum C Programming
    Replies: 7
    Last Post: 04-17-2012, 01:30 AM
  5. function, function call, for loop
    By jackson6612 in forum C++ Programming
    Replies: 2
    Last Post: 05-03-2011, 04:03 PM

Tags for this Thread