Thread: C program formula code problem

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    3

    Question C program formula code problem

    I am having a lot of trouble with this. I am making a C program that takes a users input for price of a meal at Restaurant 1 and price at Restaurant 2, then it asks for how many meals you will eat during the semester, and finally how much money your parents have put on your meal card.

    The input specifications are as follows. ALL input numbers are integers. Rest 2 is always more expensive that Rest 1. The amount of money put on the meal card is somewhere between the cost of eating every meal at Rest1 and every meal at Rest 2.

    The output should look as follows:

    You will eat X meals at rest1 and Y meals at rest2.
    You will have $Z left on your card.
    ______________________________________…
    I was given the following sample run of the program to help:

    How much do you spend on a meal at Rest 1?
    4
    How much do you spend on a meal at Rest 2?
    6
    How many meals will you eat this semester?
    150
    How much money did your parents put on your meal card?
    749
    You will eat 76 meals at Rest 1 and 74 meals at Rest 2.
    You will have $1 left on your card.

    I know how to implement all the coding for this program but what I can't seem to figure out is the mathematical formula to give me the desired outputs. I have tried several methods. Any tips would be appreciated. Remember, all numbers are integers.

    Thanks so much!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Total spent = (4 * NumberOfTimesYouEat@Rest 1) + (6 * NumberOfTimesYouEat@Rest 2)

    In a do while loop, start with the NOTYE@Rest 2 set equal to the total number of meals. (something that will exceed your meal card amount.)

    With each loop, decrease the NOTYE@Rest 2, and increase the NOTYE@Rest 1, by one.

    Break out of the loop when your total amount spent <= your meal card amount.

    Make sense?

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    Yeah, that gives me a clearer picture. Thanks! What do you think would be the best way to code that?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by parachutes89 View Post
    Yeah, that gives me a clearer picture. Thanks! What do you think would be the best way to code that?
    In a do while loop, as I mentioned. Read a lot, do you?

    Set your values of the meal card up, and the total spent (which can be any fictional amount that is greater than the meal card amount), before the do while loop ("prime the pump"), and then have it break out of the loop when the amount spent is <= the amount on the meal card.

    Post up your code to try it, and let's see where you're stuck.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    3

    Question

    This is what I completed. It does the job. Do you think it could be simplified in any way??


    Code:
    #include <stdio.h>
    
    int main(void)  {
    
        // Variables are declared
       int subway_cost, lazy_cost, difference;
       int card_balance, final_balance, total_cost;
       int subway_meals, lazy_meals, total_meals;
     
       // User inputs the values
       printf("How much does a meal at Subway cost?\n");
       scanf("%d", &subway_cost);
       printf("How much does a meal at Lazy Moon cost?\n");
       scanf("%d", &lazy_cost);
       printf("How many meals will you eat this semester?\n");
       scanf("%d", &total_meals);
       printf("How much money did your parents put on your meal card?\n");
       scanf("%d", &card_balance);
       
       // Price difference of the meals
       difference = abs(lazy_cost-subway_cost);
    
       // Total meals are divided between both restaurants as integers
       subway_meals = total_meals/2;
       lazy_meals = total_meals-subway_meals;
    
       // Total cost of the divided number of meals is calculated
       total_cost = (subway_meals*subway_cost)+(lazy_meals*lazy_cost);
       
       // Loop adjusts number of meals until total cost is <= card balance
             while (total_cost > card_balance)  {
                 subway_meals++;
                 lazy_meals--;
                 total_cost = (subway_meals*subway_cost)+(lazy_meals*lazy_cost);  }
       
       // Final balance calculated to determine if meal upgrade is available    
       final_balance = card_balance-total_cost;
    
       // Loop adjusts number of meals if upgrade is available
             while (final_balance >= difference)  {
                 subway_meals--;
                 lazy_meals++;
                 final_balance = card_balance-((subway_meals*subway_cost)+(lazy_meals*lazy_cost));  }
    
       // Number of meals to be eaten at each place is displayed
       printf("You will eat %d meals at Subway and %d meals at Lazy Moon.\n", subway_meals, lazy_meals);
    
       // If statement chooses output based on final balance value
             if (final_balance > 0)
                printf("You will have $%d left on your card.\n", final_balance);
             else
                printf("You will have no balance left on your card.\n");
    
    
       system("pause");
       return 0;
    
    }

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe so. The variable "difference" is not used in your original posts required output, for instance.

    Note that abs is not part of stdio.h. Your compiler may be smart enough to find it and include it, but I wouldn't depend on that. I don't believe you need abs, of anything, anyway.

    You've set up your variables before the while loop in a manner that may cause the program to never enter the while loop, at all. IMO, they should be set to the most expensive option (all meals at Lazy Moon), and only then enter the while loop, to find the ideal numbers. You don't want to set them to the mid-point before the loop. If the parents are generous, the 50-50 set points you have, may be too low.

    Your "upgrade" while loop is just a repeat of the earlier while loop, and is unnecessary. If your first while loop is correct (and it should be <= not just < imo), then this upgrade loop is redundant, and will give you no further data of use. The meal mix is already optimum.

    Pretty fair, otherwise. As a style note, I'd like to see the final brace of a compound block of code, fall directly below the first letter of the block:

    Code:
    while(int1 < int2) {
       int3 = int1 + int2;
       int4 = int 3 + int1;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my program i cant figure out...
    By youareafever in forum C Programming
    Replies: 7
    Last Post: 11-01-2008, 11:56 PM
  2. Replies: 12
    Last Post: 06-08-2005, 11:23 AM
  3. Odd problem in main(), code won't proceed
    By aciarlillo in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2005, 11:00 PM
  4. Problem using java programs within C code
    By lemania in forum Linux Programming
    Replies: 1
    Last Post: 05-08-2005, 02:02 AM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM