Like Tree2Likes

Question on my Program for intro to C calculating kilomters per liter.

This is a discussion on Question on my Program for intro to C calculating kilomters per liter. within the C Programming forums, part of the General Programming Boards category; Hey guys. I am new to C and I am having a hard time with my assignment. I am suppose ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    20

    Question on my Program for intro to C calculating kilomters per liter.

    Hey guys. I am new to C and I am having a hard time with my assignment. I am suppose to make a program that calculates kilometers per liter for 4 tanks of gasoline that a user fills in their car. this is what i have so far, i know its not right yet but i was just looking for some guidance. I am not sure if i am incrementing the number of tanks right and iam unsure how to get the average for the final calculations(i know its the total kilometers divided by the total liters). ANy help would be appreciated thank you in advance. Also i am using C-free-4.0 if that matters.

    Code:
    #include<stdio.h>
      
    int main()
    { 
     int i;
     float res, km, lt, sum, avg;
     sum=0;
     avg= sum/4;
     
     printf ("Welcometo the Johns kilometers per liter calculator.\n\n");
     
     for(i=1;i<=4;i++)
     {
      printf ("Enter the number of Kilometers for tank %i: ",i);
      scanf ("%d", &km);
      printf ("Enter the number of liters used by tank %i: ",i);
      scanf ("%f", &lt);
      res = (float) km / lt;
      printf ("the kiomleters per liter for tank %i is %.1f", i, res);
      sum = sum + res;
     }
      printf ("Your overall average kilometers per liter for 4 tanks is %.1f", avg);
      printf("Thanks for using the Johns KPL calculator program.");
      getchar();
      }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    Your problem is line 8. sum is 0, so you are dividing 0 by 4, and your average is always 0. Move that line to after the for loop, so it calculates the average after sum has the right value.

    EDIT: I just re-read your post, and you may need to calculate the average a bit differently. You may need to have a total_km and total_lt variable. Set each to 0 before your loop. When the user enters a value for km and lt, add them to the respective totals (like you do with sum currently). Divide them to get the average kpl after the for loop.

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    Your indentation needs work - You can Google what that means if you are not sure.

    Code:
    avg= sum/4;
    "avg" does not remember the expression that you give it (sum/4) - Instead it calculates the expression at that point (0) and retains the value 0 throughout your program.

    Basically, you want to move this expression to just after your "for" loop (after "sum" has it's total in it.)
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    20
    I keep receiving a permision denied error? PLease excuse the indentations its my rough draft.

    Code:
    
    #include<stdio.h>
      
    int main()
    { 
     int km, i;
     float res, lt, sum, avg;
     sum=0;
     
      printf ("Welcometo the Johns kilmeters per liter calculator.\n\n");
     
      for(i=1;i<=4;i++)
      {
      printf ("\n\nEnter the number of milometers for tank d#: ",i);
      scanf ("%d", &km);
      printf ("Enter the number of liters used by tank %d#: ",i);
      scanf ("%f", &lt);
      res = (float)km/lt;
      printf ("*** the kiomleters per liter for tank %d# is %.1f", i, res);
      sum = sum + res;
      }
      avg = sum/4;
       printf ("Your overall average kilometers per liter for 4 tanks is %.1f", avg);
       printf ("\n\nThanks for using the Johns KPL calculator program.");
      
    }

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    Code:
    printf ("\n\nEnter the number of milometers for tank %d#: ",i);
    And add "return 0;" to the end of your main function
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    Quote Originally Posted by fenway81 View Post
    I keep receiving a permision denied error?
    Huh? When doing what? Compiling? Running the program? Tell us exactly what you do that produces that error, and copy-paste the error message exactly from wherever it happens.
    Quote Originally Posted by fenway81 View Post
    PLease excuse the indentations its my rough draft.
    Nope, not excusing it. As a matter of fact, rough draft is a horrible reason for allowing poor indentation. Good indentation actually helps you program better, without it your code is hard to read (for you and us). The harder it is to read your code, the easier it is to make a mistake, and the harder it is to find and fix it. Fix your indentation before you post your code again. Read this link: SourceForge.net: Indentation - cpwiki.
    Salem and AndiPersti like this.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    20
    When i hit compile it says no errors, then when i hit RUN i get this error;

    [Error] C:\PROGRA~2\C-FREE~1\mingw32\Bin\ld.exe: cannot open output file C:\Program Files (x86)\C-Free Standard\temp\Untitled8.exe: Permission denied
    Complete Make Untitled8: 1 error(s), 0 warning(s)

    Code:
    #include<stdio.h>
      
    int main()
    { 
     int km, i;
     float res, lt, sum, avg;
     sum=0;
     
      printf ("Welcometo the Johns kilometers per liter calculator.\n\n");
     
      for(i=1;i<=4;i++)
      {
      printf ("\n\nEnter the number of Kilometers for tank %d#: ",i);
      scanf ("%d", &km);
      printf ("Enter the number of liters used by tank %d#: ",i);
      scanf ("%f", &lt);
      res = (float)km/lt;
      printf ("*** the kiomleters per liter for tank %d# is %.1f", i, res);
      sum = sum + res;
      return 0;
      }
      avg = sum/4;
       printf ("Your overall average kilometers per liter for 4 tanks is %.1f", avg);
       printf ("\n\nThanks for using the Johns KPL calculator program.");
      
    }

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    After you tested and ran it the first time whilst debugging, did you close it?
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    Quote Originally Posted by anduril462 View Post
    Nope, not excusing it. As a matter of fact, rough draft is a horrible reason for allowing poor indentation. Good indentation actually helps you program better, without it your code is hard to read (for you and us). The harder it is to read your code, the easier it is to make a mistake, and the harder it is to find and fix it. Fix your indentation before you post your code again. Read this link: SourceForge.net: Indentation - cpwiki.
    Fix that, and I will help you with your problem.

  10. #10
    Registered User
    Join Date
    Oct 2012
    Posts
    20
    OK i closed it out and it ran. It didnt run four loops like i exected am i doing somehting wrong?

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    Yes, you are. Read your code carefully (which would be much easier with good indentation), and see if you can spot why your program terminates at the end of the first iteration of the loop.

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    It ran four loops when I tested it with the changes that I suggested before

    Can you post your new code - And for the sanity of anduril462 would you be able to indent your code properly? (I saw your compiler is mingw32, are you using codeblocks? If so, click "Plugins >> Source Code Formatter" - It will indent it properly for you)
    Fact - Beethoven wrote his first symphony in C

  13. #13
    Registered User
    Join Date
    Oct 2012
    Posts
    20
    I apologize i am still getting used to the indentations, I will work on them as well.


    Code:
    #include<stdio.h>
      
    int main()
    { 
     /*Variable Declarations*/
        /*---------------------*/
        
     int km, i;
     float res, lt, sum, avg;
     sum=0;
     
     /* Output Welcome Message. */
     /* --------------------- */
     
     printf ("Welcometo the Johns kilometers per liter calculator.\n\n");
     
     printf ("The program will calculate kilometers per liter for 4 tanks for you after \n");
        printf ( "you have entered the kilometers driven and liters used.\n");
     
     for(i=1;i<=4;i++)
     {
      
     printf ("\n\nEnter the number of Kilometers for tank %d#: ",i);
     scanf ("%d", &km);
     
     printf ("Enter the number of liters used by tank %d#: ",i);
     scanf ("%f", &lt);
     
     res = (float)km/lt;
     printf ("*** the kiomleters per liter for tank %d# is %.1f", i, res);
     
     sum = sum + res;
     return 0;
     
     }/* end for loop */
     
     
     avg = sum/4; /*Average for four tanks */
     
     /* Output results Message. */
     /* --------------------- */
     
     printf ("Your overall average kilometers per liter for 4 tanks is %.1f", avg);
     
     /* Output GoodBye Message. */
     /* --------------------- */
     printf ("Thanks for using the Johns KPL calculator program.\n\n");
      
    }

  14. #14
    Registered User
    Join Date
    Oct 2012
    Posts
    20
    NOt to sound dumb but where are the puggins. I see what your saying my code is indented i must have the code block on or something.

    Code:
    #include<stdio.h>
      
    int main()
    { 
     /*Variable Declarations*/
        /*---------------------*/
        
     int km, i;
     float res, lt, sum, avg;
     sum=0;
     
     /* Output Welcome Message. */
     /* --------------------- */
     
     printf ("Welcometo the Johns kilometers per liter calculator.\n\n");
     
     printf ("The program will calculate kilometers per liter for 4 tanks for you after \n");
        printf ( "you have entered the kilometers driven and liters used.\n");
     
     for(i=1;i<=4;i++)
     {
      
     printf ("\n\nEnter the number of Kilometers for tank %d#: ",i);
     scanf ("%d", &km);
     
     printf ("Enter the number of liters used by tank %d#: ",i);
     scanf ("%f", &lt);
     
     res = (float)km/lt;
     printf ("*** the kiomleters per liter for tank %d# is %.1f", i, res);
     
     sum = sum + res;
     return 0;
     
     }/* end for loop */
     
     
     avg = sum/4; /*Average for four tanks */
     
     /* Output results Message. */
     /* --------------------- */
     
     printf ("Your overall average kilometers per liter for 4 tanks is %.1f", avg);
     
     /* Output GoodBye Message. */
     /* --------------------- */
     printf ("Thanks for using the Johns KPL calculator program.\n\n");
      
    }

  15. #15
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    If your code looks properly indented in your editor, but not in the forum, then take the time to format it by hand. It makes our job much easier. I don't mean to be an a-hole, but seriously, properly indented code is one of the easiest things you can do to reduce silly errors. Many editors/IDEs have a auto-indent feature such as the one Click_here mentioned (I don't use C::B so I can't help you there). Here's your code with good indentation:
    Code:
    #include<stdio.h>
    
    int main()
    {
        /*Variable Declarations*/
        /*---------------------*/
    
        int km, i;
        float res, lt, sum, avg;
        sum=0;
    
        /* Output Welcome Message. */
        /* --------------------- */
    
        printf ("Welcometo the Johns kilometers per liter calculator.\n\n");
    
        printf ("The program will calculate kilometers per liter for 4 tanks for you after \n");
        printf ( "you have entered the kilometers driven and liters used.\n");
    
        for(i=1;i<=4;i++)
        {
    
            printf ("\n\nEnter the number of Kilometers for tank %d#: ",i);
            scanf ("%d", &km);
    
            printf ("Enter the number of liters used by tank %d#: ",i);
            scanf ("%f", &lt);
    
            res = (float)km/lt;
            printf ("*** the kiomleters per liter for tank %d# is %.1f", i, res);
    
            sum = sum + res;
            return 0;
    
        }/* end for loop */
    
    
        avg = sum/4; /*Average for four tanks */
    
        /* Output results Message. */
        /* --------------------- */
    
        printf ("Your overall average kilometers per liter for 4 tanks is %.1f", avg);
    
        /* Output GoodBye Message. */
        /* --------------------- */
        printf ("Thanks for using the Johns KPL calculator program.\n\n");
    
    }
    What's inside your loop that shouldn't be? Look near the bottom. Where should that line actually be?

    EDIT: Also, it may help if you set your editor to indent using spaces rather than tabs, that often causes it to look good in the editor but poor in the forum.
    Last edited by anduril462; 10-08-2012 at 05:47 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Grades calculating program help please!
    By hotshotennis in forum C Programming
    Replies: 93
    Last Post: 04-24-2012, 12:08 PM
  2. C Program Not Properly Calculating
    By andynov123 in forum C Programming
    Replies: 16
    Last Post: 09-18-2011, 03:27 PM
  3. Calculating FPS for my OpenGL program
    By C+noob in forum Game Programming
    Replies: 1
    Last Post: 10-09-2005, 01:22 PM
  4. Replies: 2
    Last Post: 11-10-2003, 08:12 PM
  5. Replies: 8
    Last Post: 02-23-2002, 08:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21