Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Flight Times. 2 Problem Specification 3 Additional Specifications You may assume that the input is a valid 24 hour time. Output the time entered by.

Similar presentations


Presentation on theme: "1 Flight Times. 2 Problem Specification 3 Additional Specifications You may assume that the input is a valid 24 hour time. Output the time entered by."— Presentation transcript:

1 1 Flight Times

2 2 Problem Specification

3 3 Additional Specifications You may assume that the input is a valid 24 hour time. Output the time entered by the user before outputting the result. See sample runs.

4 4 Test Plan The program should work with any set of departure and arrival times. Try to be sure we have tested all cases. Test Cases: Shortly before each departure time. Shortly after each departure time. Midway between successive departure times. Exactly equal to a departure time.

5 5 Test Plan The closest departure time might be the next day or the previous day. Be sure to have test cases covering these cases. Special time values that might cause trouble: Midnight Noon Between midnight and 1:00 AM Between noon and 1:00 PM

6 6 Test Cases

7 7 Implementation Hints Remember that the time specified by the user might be either before or after the closest departure time. You will need to use the integer absolute value function, abs(), from the C Standard Library, stdlib. (Google for details.) You should have the following line near the top of your file, along with the #include for : #include Note that the closest departure time might be the next day or the previous day.

8 8 Implementation Hints To read in a 24 hour time such as 13:15 use scanf to input two integer values with a colon between. Example: scanf("%d:%d", &hour, &minute);

9 9 Sample Runs (Windows)

10 10 Where to start? Sketch out a design What does the program have to do? What data structures should it use?

11 11 Sketch out a design Could do this with pencil and paper. Many programmers prefer to do it as the beginning of the actual program. Pseudocode. Comments say what the program will do.

12 12 Start with a Template /* This program determines that closest departure time in a * list of airline flights to a time value entered by the user. * * Written by Bill Gates for COP 3514 */ #include int main() { return 0; }

13 13 What the program has to do int main() { // Get 24 hour time from user. // Output time entered by the user. // Convert time to minutes since midnight. // Output time in minutes since midnight. (Debugging output.) // Look through the departure time array and find the value that // is closest to the time entered by the user. // Output departure time and arrival time of closest flight // as AM/PM time of day. return 0; }

14 14 Data Structures We have to know the departure times and arrival times. Will need to compare the time entered by the user to each departure time. It’s hard to compare times represented as hours and minutes. Let’s represent all times as minutes since midnight.

15 15 Departure and Arrival Times // Departure times in minutes after midnight int departure_times[] = {8*60, 9*60+43, 11*60+19, 12*60+47, 14*60, 15*60+45, 19*60, 21*60+45}; // Arrival times in minutes after midnight int arrival_times[] = { 10*60+16, 11*60+52, 13*60+31, 15*60, 16*60+8, 17*60+55, 21*60+20, 23*60+58};

16 16 Implementation We have a roadmap. Let’s start to implement the program. Remember: Implement in tiny steps. Always have a running program.

17 17 Implementation Start with the “Hello, World!” level. Just a greeting message and a termination message. Be sure it compiles and works.

18 18 Hello, World! // Arrival times in minutes after midnight int arrival_times[] = { 10*60+16, 11*60+52, 13*60+31, 15*60, 16*60+8, 17*60+55, 21*60+20, 23*60+58}; printf ("This program determines the flight with the closest\n"); printf (" departure time to a time that you enter.\n"); // Get 24 hour time from user.... // Output departure time and arrival time of closest flight // as 24 hour times. printf ("Normal termination\n"); return 0; Compile and run.

19 19 Program Running We have a working program!

20 20 Input and Output Before trying to implement the real work implement the input and output actions. Start with input: Prompt the user and read a time in 24 hour format. Tell the user what we heard. Be sure the input is correct!

21 21 Get Input // Arrival times in minutes after midnight int arrival_times[] = { 10*60+16, 11*60+52, 13*60+31, 15*60, 16*60+8, 17*60+55, 21*60+20, 23*60+58}; int hour = 0; int minute = 0; printf ("This program determines the flight with the closest\n"); printf (" departure time to a time that you enter.\n"); // Get 24 hour time from user. printf ("\nEnter a 24-hour time: "); scanf("%d:%d", &hour, &minute); // Output time entered by the user. printf ("You entered %2d:%02d\n", hour, minute);

22 22 Program Running

23 23 Implement Output We need to output the departure time and arrival time as normal 12 hour AM/PM time values. What we have in the arrays is minutes since midnight. Let’s write a function to convert minutes since midnight to AM/PM. Input: minutes since midnight as int. Output AM/PM time to the screen.

24 24 Function output_time_of_day() /* This function outputs time of day specified by the caller * in minutes since midnight as a normal AM/PM time. */ void output_time_of_day(int minutes_since_midnight) { }

25 25 Function output_time_of_day() Start with a stub. /* This function outputs time of day specified by the caller * in minutes since midnight as a normal AM/PM time. */ void output_time_of_day(int minutes_since_midnight) { printf ("11:45 a.m."); }

26 26 Add code to do the output with fake data // Look through the departure time array and find the value that // is closest to the time entered by the user. best_index = 1; // Output departure time and arrival time of closest flight // as 24 hour times. i = best_index; printf ("Closest departure time is "); output_time_of_day(departure_times[i]); printf ("arriving at "); output_time_of_day(arrival_times[i]); printf ("\n"); printf ("Normal termination\n"); Compile and run.

27 27 Compile Errors! Add declarations for best_index and i. Try again.

28 28 Program Running

29 29 Function output_time_of_day() Next let’s make the time of day output function real. We need to think carefully about how to do this. Again sketch out what to do in pseudocode.

30 30 Function output_time_of_day() /* This function outputs time of day specified by the caller * in minutes since midnight as a normal AM/PM time. */ void output_time_of_day(int minutes_since_midnight) { // Get hours since midnight and minutes within the hour. // Determine if AM or PM. // If PM, adjust hour. // If before 1:00 (AM or PM), change hour to 12. // Output the time of day. }

31 31 Fill in Real Code /* This function outputs time of day specified by the caller * in minutes since midnight as a normal AM/PM time. */ void output_time_of_day(int minutes_since_midnight) { char halfday; // Get hours since midnight and minutes within the hour. int hour = minutes_since_midnight / 60; int min = minutes_since_midnight % 60; // Determine if AM or PM. if (hour >= 12) { halfday = 'p'; } else { halfday = 'a'; }

32 32 Fill in Real Code // If PM, adjust hour. if (hour >= 12) { hour -= 12; } // If before 1:00 (AM or PM), change hour to 12. if (hour < 1) { hour = 12; } // Output the time of day. printf ("%d:%2d %c.m.", hour, min, halfday); }

33 33 Program Running

34 34 Function output_time_of_day() Function output_time_of_day() is pretty tricky. Let’s test it with all of our departure and arrival times before going on.

35 35 Test output_time_of_day() for (i = 0; i < 8; i++) { printf ("Closest departure time is "); output_time_of_day(departure_times[i]); printf ("arriving at "); output_time_of_day(arrival_times[i]); printf ("\n"); }

36 36 Program Running We do have a problem. Need leading 0 in minutes.

37 37 Output Leading 0 in Minutes // Output the time of day. printf ("%d:%02d %c.m.", hour, min, halfday);

38 38 Program Running

39 39 More real code. Next let’s add real code to compute and output user’s input as minutes since midnight. Remove output “for” loop. int time_in_minutes; // Get 24 hour time from user. printf ("\nEnter a 24-hour time: "); scanf("%d:%d", &hour, &minute); // Output time entered by the user. printf ("You entered %2d:%02d\n", hour, minute); // Convert time to minutes since midnight. time_in_minutes = hour*60 + minute; // Output time in minutes since midnight. (Debugging output.) printf ("This is %d minutes since midnight\n", time_in_minutes);

40 40 Program Running

41 41 Moving Ahead Looks like our input and output functionality is correct now. Let’s get on the real work. Determine the closest departure time.

42 42 Function best_departure_time() /* This function determines the closest departure time to * the time passed by the caller. It returns the array index * of the best departure time. All times are in * minutes since midnight. */ int best_departure_time(int departure_times[], int length, int target_time) { return 1; } Start with a stub.

43 43 Function best_departure_time() Add call to the function. // Look through the departure time array and find the value that // is closest to the time entered by the user. best_index = best_departure_time(departure_times, array_length, time_in_minutes); // Output departure time and arrival time of closest flight // as 24 hour times. i = best_index;

44 44 array_length // Departure times in minutes after midnight int departure_times[] = {8*60, 9*60+43, 11*60+19, 12*60+47, 14*60, 15*60+45, 19*60, 21*60+45}; int array_length = sizeof(departure_times) / sizeof(departure_times[0]);

45 45 Program Running

46 46 The Real Work Function Find the closest departure time Look through the array of departure times. For each departure time, compute the time difference between that time and the user’s time. Remember the minimum time difference seen and index of the array entry that had that difference.

47 47 Find the closest departure time Computing the time difference is easy since all times are in minutes since midnight. BUT, we have to consider that the closest departure time might be the previous day or the next day.

48 48 Previous Day Example: Departure times: 8:00 AM 11:55 PM Target time: 1:00 AM

49 49 Next Day Example: Departure times: 1:00 AM 11:55 AM Target time: 11:55 PM

50 50 Function best_departure_time() /* This function determines the closest departure time to * the time passed by the caller. It returns the array index * of the best departure time. All times are in * minutes since midnight. */ int best_departure_time(int departure_times[], int length, int target_time) { // Look through the departure times array and find the value that // is closest to the time entered by the user. // Remember the best difference seen so far and the index // of the corresponding array entry. // Difference between user's time and departure time same day. // Difference between user's time and departure time previous day. // Difference between user's time and departure time next day. }

51 51 Function best_departure_time() /* This function determines the closest departure time to * the time passed by the caller. It returns the array index * of the best departure time. All times are in * minutes since midnight. */ int best_departure_time(int departure_times[], int length, int target_time) { // Remember the best difference seen so far and the index // of the corresponding array entry. int min_difference = INT_MAX; int best_index = 0; int i;

52 Function best_departure_time() // Look through the departure times array and find the value that // is closest to the time entered by the user. for (i = 0; i < length; i++) { int diff; // Difference between user's time and departure time same day. diff = abs(target_time - departure_times[i]); if (diff < min_difference) { min_difference = diff; best_index = i; } // Difference between user's time and departure time previous day. diff = abs(target_time - departure_times[i] - 60*24); if (diff < min_difference) { min_difference = diff; best_index = i; } // Difference between user's time and departure time next day. diff = abs(target_time - departure_times[i] + 60*24); if (diff < min_difference) { min_difference = diff; best_index = i; } return best_index;

53 53 Program Running

54 54 Program Running

55 55 Program Running

56 56 Test Thoroughly Use the test plan. Check all test cases.


Download ppt "1 Flight Times. 2 Problem Specification 3 Additional Specifications You may assume that the input is a valid 24 hour time. Output the time entered by."

Similar presentations


Ads by Google