[ create a new paste ] login | about

Link: http://codepad.org/aq9itfqO    [ raw code | output | fork ]

C, pasted on Nov 10:
 #include <stdio.h>
 #include <stdlib.h>
 #define hour 60

struct flight 
{
    int flight_time;
    int flight_layover;
};

struct flight flights[5]=
{
        {4 * hour + 15, 1 * hour + 20},
        {3 * hour + 58, 0 * hour + 46},
        {3 * hour + 55, 11 * hour + 29},
        {2 * hour + 14, 0 * hour + 53},
        {3 * hour + 27, 0 * hour + 0}
};

int getNum(void);
int distance(int start, int end , struct flight flights[]);

int main(void)
{
    int user_start=-1;
    int user_end=-1;
    int travel_time=0;

    printf ("Hello sir. Please enter you starting city:\n");
    user_start=getNum();
    user_start--;

    printf ("Good. Now enter the city you would like to end it:\n");
    user_end=getNum();
    user_end--;

    travel_time = distance(user_start, user_end, flights);

    printf ("The total travel time from %d to %d is %d.",user_start, user_end, travel_time);

    return 0; 
}

int distance(int start, int end, struct flight flights[])
{
    int total_mins=0;
    int i=0;
    for (i = start; i <= end; i++)
    {
        total_mins=total_mins + flights[i].flight_time + flights[i].flight_layover;
    }

    return total_mins;
}

int getNum(void)
{
    char record[121] = {0}; 
    int number = 0;
    fgets(record,  121, stdin);
    if(sscanf(record, "%d", &number) != 1 )
    {
        number  = -1;
    }
    return  number;
}


Output:
1
2
3
Hello sir. Please enter you starting city:
Good. Now enter the city you would like to end it:
The total travel time from -2 to -2 is 0.


Create a new paste based on this one


Comments: