C Programming - Having difficulty with this program?

This is a discussion on C Programming - Having difficulty with this program? within the C Programming forums, part of the General Programming Boards category; As I am writing this code, I have it set up where I can input values, but I am having ...

  1. #1
    BLG
    BLG is offline
    Registered User
    Join Date
    Sep 2009
    Posts
    14

    C Programming - Having difficulty with this program?

    As I am writing this code, I have it set up where I can input values, but I am having difficulty with the math part. Do I have to put in "#define" and that formula before the "int main(void)" and insert the equation before the getch()? I know I have to tell the program that a is this, g is that, etc, but that is where I am stuck at. Here is the question and what I have so far is at the bottom:

    Question:

    Prompt the user to enter the following values:
    1. Time of Day (hour from 0-23, in military time, let this be t)
    2. Parking Spots Available (for the semester, let this be a)
    3. Parking Permits Given (for the semester, let this be g)

    After much observation, you have realized that the formula for determining the amount of time you will wait to get a parking spot is:
    Time (in minutes) = (12 | 12 t |)*g/a

    Input Specification
    1. The time of day will be a single integer in between 0 and 23, inclusive, representing the hour the user is attempting to park.
    2. This number of spots available will be a positive integer less than 50,000.
    3. The number of permits given will be a positive integer less than 60,000.

    Sample Run
    What hour are you looking for parking?
    10
    How many spots are available this semester?
    9000
    How many parking permits were given this semester?
    45000
    You will have to wait 50.00 minutes to find parking.
    __________________________________________________ _____________________

    (MY WORK SO FAR)

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int hour, spots, parking_permits; 
        float time;
        
        printf ("What hour are you looking for parking? \n");
        scanf ("%d", &hour);
        
        printf ("How many spots are available this semester? \n");
        scanf ("%d", &spots);
        
        printf ("How many parking permits were given this semester? \n");
        scanf ("%d", &parking_permits);    
        
        getch ();
        
        return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You just have to compute your answer, once you have the data. I fail to see why any #define's would be needed at all.

  3. #3
    BLG
    BLG is offline
    Registered User
    Join Date
    Sep 2009
    Posts
    14
    Quote Originally Posted by tabstop View Post
    You just have to compute your answer, once you have the data. I fail to see why any #define's would be needed at all.
    so all I have to do is put in that formula and that's it?
    Do I have to specify a, g, and t?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Don't use a macro (eg: define) - *always* use functions (execpt in very special circumstances). You must declare the function before main, but you can define it (the actual body of code) anywhere (outside of a function, of course). After you have gathered your input, invoke the function to calculate the formula. The function might look something like:

    Code:
    	
    int expected_wait(int time, int spots_available, int permits_issued)
    {
    	int result = 0;
    /*
    	Formula goes here, assign to 'result'
    */	
    	return result;
    }
    And invoking it like so:

    Code:
    	
    	int expected_wait_time = expected_wait(hour, spots, parking_permits);
    Last edited by Sebastiani; 09-06-2009 at 06:26 PM.

  5. #5
    BLG
    BLG is offline
    Registered User
    Join Date
    Sep 2009
    Posts
    14
    thank you. You guys are helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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