Thread: i need a suggestion of a way to make this code look better

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    48

    i need a suggestion of a way to make this code look better

    Code:
    #include <stdio.h>
    
    //  Function Declarations
    void getData(int* days, double* airfare, double* ground, double* rate, double* food, double* enter);
    void calctotal(int days, double airfare, double ground, double rate, double food, double enter, double* totalF, double* totalR, double* total);
    void calcsubtotal(int days, double rate, double food, double* totalFood, double* totalRate);
    void output(int days, double airfare, double ground, double rate, double food, double enter, double totalF, double totalR, double total);
    
    int main(void)
    {
        //Local Declarations
        int days;
        double airfare;
        double ground;
        double rate;
        double food;
        double enter;
        double totalF;
        double totalR;
        double total;
        double subtotal;
    
        // Statements
        getData(&days, &airfare, &ground, &rate, &food, &enter);
        calctotal(days, airfare, ground, rate, food, enter, &totalF, &totalR, &total);
        output(days, airfare, ground, rate, food, enter, totalF, totalR, total);
    
        return 0;
    }// main
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    void getData(int* days,double* airfare, double* ground, double* rate, double* food, double* enter)
    {
        printf("how many days: ");
        scanf("%d", days);
    
        printf("How much is the airfare: ");
        scanf("%lf", airfare);
    
        printf("How much is the ground fare: ");
        scanf("%lf", ground);
    
        printf("How much is the hotel rate: ");
        scanf("%lf", rate);
    
        printf("How much is the food price: ");
        scanf("%lf", food);
    
        printf("How much is the cost of entertainment: ");
        scanf("%lf", enter);
    
        return;
    
    }// getData
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    void calctotal(int days, double airfare, double ground, double rate, double food, double enter, double* totalF, double* totalR, double* total)
    {
        double totalFood;
        double totalRate;
    
        calcsubtotal(days, rate, food, &totalFood, &totalRate);
    
        *total = totalFood + totalRate + airfare + ground + enter;
        *totalF = totalFood;
        *totalR = totalRate;
    
        return;
    
    }//calctotal
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    void calcsubtotal(int days, double rate, double food, double* totalFood, double* totalRate)
    {
        *totalFood = days * food;
        *totalRate = (days - 1) * rate;
    
        return;
    
    }//calcsubtotal
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    void output(int days, double airfare, double ground, double rate, double food, double enter, double totalF, double totalR, double total)
    {
        printf("\nDays: %d\n", days);
        printf("Airfare: %.2lf\n", airfare);
        printf("Ground Transportations: %.2lf\n", ground);
        printf("Hotel: %.2lf\n", totalR);
        printf("Food: %.2lf\n", totalF);
        printf("Entertainment: %.2lf\n", enter);
        printf("\n");
        printf("Total: %.2lf\n\n", total);
    
       
    
        return;
    }//output
    this is what i was being asked to do

    Write a program to calculate the total cost of your Spring break trip.
    Enter the number of days, airfare, amount needed for ground transportation,
    nightly hotel rate (includes taxes), allotment per day for food, and cost of entertainment.

    Use a function for input, a function to calculate totals which calls a function to calculate subtotals.
    This function will be invoked once per item: hotel, food.

    i finished the code but for some reason the caltotal and subtotal function doesn't seem right, also im a bit confuse of a way to call function subtotal from function caltotal

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    - While it's functionally the same, you can change:

    Code:
    void calcsubtotal(int days, double rate, double food, double* totalFood, double* totalRate);
    to
    Code:
    void calcsubtotal(int, double, double, double*, double*);
    In the function prototype ​(not the declaration), it shortens the code without affecting performance.

    - Clean up all the unused variables.
    - You could try also putting all of the values into a struct and passing it, instead of a bunch of doubles.
    - Make absolutely sure that you need to use doubles (as opposed to floats or even ints/shorts), especially because nothing you're calculating requires precision past the hundredths and you're just wasting memory.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    What memcpy has just said about leaving out parameter names is actually largely discouraged.
    Regardless, it doesn't address the real issue here, and that is that there is no good reason to have so many parameters to the functions.

    I suggest making a struct of the things that you read in via the getData function. Then just pass this to the other functions as required.

    You can also remove the superflous returns at the end of each function.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    #include <stdio.h>
    
    
    void calcsubtotal(int days, double rate, double food, double* totalFood, double* totalRate)
    {  *totalFood = days * food;
        *totalRate = (days - 1) * rate; }
    
    
    void calctotal(int days, double airfare, double ground, double rate, double food, double enter, double* totalF, double* totalR, double* total)
    {  double totalFood;
        double totalRate;
    
        calcsubtotal(days, rate, food, &totalFood, &totalRate);
    
        *total = totalFood + totalRate + airfare + ground + enter;
        *totalF = totalFood;
        *totalR = totalRate;  }
    
    
    void output(int days, double airfare, double ground, double rate, double food, double enter, double totalF, double totalR, double total)
    {  printf("\nDays: %d\n", days);
        printf("Airfare: %.2lf\n", airfare);
        printf("Ground Transportations: %.2lf\n", ground);
        printf("Hotel: %.2lf\n", totalR);
        printf("Food: %.2lf\n", totalF);
        printf("Entertainment: %.2lf\n", enter);
        printf("\n");
        printf("Total: %.2lf\n\n", total); }
    
    
    void getData(int* days,double* airfare, double* ground, double* rate, double* food, double* enter)
    {   printf("how many days: ");
        scanf("%d", days);
    
        printf("How much is the airfare: ");
        scanf("%lf", airfare);
    
        printf("How much is the ground fare: ");
        scanf("%lf", ground);
    
        printf("How much is the hotel rate: ");
        scanf("%lf", rate);
    
        printf("How much is the food price: ");
        scanf("%lf", food);
    
        printf("How much is the cost of entertainment: ");
        scanf("%lf", enter);  }
    
    
    int main(void)
    {   int days;
        double airfare;
        double ground;
        double rate;
        double food;
        double enter;
        double totalF;
        double totalR;
        double total;
        double subtotal;
    
        getData(&days, &airfare, &ground, &rate, &food, &enter);
        calctotal(days, airfare, ground, rate, food, enter, &totalF, &totalR, &total);
        output(days, airfare, ground, rate, food, enter, totalF, totalR, total);
    
        return 0; }

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    As a beginners effort, your code is not exactly ugly to start with.

    Some of my comments are VERY picky ... don't take that as an insult, as I get pickier with people who have obviously made a good effort (in other words, for people who try, I keep lifting the bar higher).

    I agree with memcpy's advice about using structs, rather than lots of single variables and long argument lists.

    The local variables like totalFood and totalRate in calctotal() are unnecessary. Why not pass totalF and totalR directly to calcsubtotal() ?

    You could also eliminate repeating code that does essentially the same thing. For example, you have multiple instances of "write a prompt and read a value" in your code. A helper function or two to simplify that would make the code clearer.

    You might want to consider how you name your functions. main() is a given (we have no choice there) but a human reader tends to look for separate words, so has trouble interpreting a string like calcsubtotal in a meaningful way. Can you come up with a better name? If you must have a function name with more than one word in it, can you use camel-case (for example CalculateSubtotal) or underscores for punctuation (for example Calculate_Subtotal) to make things easier for a mere human to understand?

    If you want to go a little more advanced, currencies are not usually worked with using floating point (except in classroom settings ). Most currencies have a major unit (eg dollars, pound) and a smaller unit (eg cents, pence) so values are best represented using a struct that contains the two. Or maybe some type that supports fixed decimal precision (which floating point does not) or represents rates and currency values as a rational number (ratio of two integers).

    I disagree with memcpy's advice to take argument names out of function prototypes (on the grounds that all code, or part thereof, needs to be reasonably self-documenting and I consider that more important than brevity). However, that is a matter of style: when it comes to "style", people will always disagree about what is better.

    [Incidentally, to pick nits with memcpy's comments, he is incorrect in distinguishing a function prototype from a function declaration. A function prototype is formally (according to the C standard) a function declaration and the implementation of a function is a definition (and a definition is a particular type of declaration).]

    I would also suggest looking at your comments and deciding if they are needed. Can you name your variables or functions better so, just by looking at THEM, a person can understand the code? If so, you can eliminate the need for some comments. I personally see little point in comments of the form //>>>>>> I would also suggest it is unnecessary to have comments identifying things like function declarations .... When programming in C, it is not unreasonable to expect that the reader will know a little C, so will recognise a function declaration on sight. Again, however, the balance of comments to code is a matter of style. The important thing is that the code is understandable. Comments are only one way of achieving that, and it is debatable whether they are always the right way.

    void functions don't tend to need return statements, as long as it is obvious the end of the function has been reached (preferably it would be obvious from code structure, but use comments if you really must).
    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.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    void calctotal(
        int days, 
        double airfare, 
        double ground, 
        double rate, 
        double food, 
        double enter, 
        double* totalF, 
        double* totalR, 
        double* total
    ) {
    Some people always do this with the definition (and the prototype); I only do it for long parameter lists like that one. It definitely makes the parameters much easier to identify when you are reading the code. You can do the same for function calls, altho IMO the ones in your code are short enough to stay on one line.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    i did that also, but some how when i posted on here it turned out to be different

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 08-21-2011, 08:10 PM
  2. Is there a way to make the following code portable?
    By manasij7479 in forum Linux Programming
    Replies: 3
    Last Post: 07-03-2011, 02:08 AM
  3. How do I make an EXE out of a C code?
    By NA84 in forum C Programming
    Replies: 4
    Last Post: 11-07-2010, 12:40 AM
  4. A little suggestion before positing your code
    By audinue in forum C Programming
    Replies: 11
    Last Post: 07-09-2008, 09:57 AM
  5. Suggestion - code forum.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-19-2003, 07:47 PM