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.

Slides:



Advertisements
Similar presentations
The Software Lifecycle. Example Problem: Update a Checkbook Write a program that allows the user to enter a starting balance, a transaction type, D or.
Advertisements

Week 5 Part I Kyle Dewey. Overview Exam will be back Thursday New office hour More on functions File I/O Project #2.
C Functions. What are they? In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive.
C Language.
ARDUINO CLUB Session 1: C & An Introduction to Linux.
Chapter Five Functions
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Modular Programming With Functions
Computer Programming w/ Eng. Applications
Computer Science 2212a/b - UWO1 Structural Testing Motivation The gcov Tool An example using gcov How does gcov do it gcov subtleties Further structural.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
MPI Program Structure Self Test with solution. Self Test 1.How would you modify "Hello World" so that only even-numbered processors print the greeting.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
Overview creating your own functions calling your own functions.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Computer Science 1620 Programming & Problem Solving.
CS 201 Functions Debzani Deb.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
1 Project 5: Median. 2 The median of a collection of numbers is the member for which there are an equal number less than or equal and greater than or.
General Programming Introduction to Computing Science and Programming I.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
CS 108 Computing Fundamentals Notes for Thursday, February 19, 2015.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Homework #2: Functions and Arrays By J. H. Wang Mar. 20, 2012.
Intermediate 2 Computing Unit 2 - Software Development.
Homework #2: Functions and Arrays By J. H. Wang Mar. 24, 2014.
1 ICS103 Programming in C Lecture 8: Functions I.
The Hashemite University Computer Engineering Department
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Computer Programming for Engineers
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Exception Handling How to handle the runtime errors.
1 Project 5: Leap Years. 222 Leap Years Write a program that reads an integer value from the user representing a year and determines if the year is a.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Intermediate 1Unit 2 Outcome 2 Speed Distance Time 8 Lessons S D T.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Basic concepts of C++ Presented by Prof. Satyajit De
User-Written Functions
Chapter 5 Functions for All Subtasks 1
Chapter 5 Functions for All Subtasks 1
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
A First Book of ANSI C Fourth Edition
Arrays in C.
Introduction to CS Your First C Programs
Functions, Part 1 of 3 Topics Using Predefined Functions
1) C program development 2) Selection structure
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions, Part 1 of 3 Topics Using Predefined Functions
CHAPTER 6 Testing and Debugging.
Presentation transcript:

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 the user before outputting the result. See sample runs.

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 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 Test Cases

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 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 Sample Runs (Windows)

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

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 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 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 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 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 Implementation We have a roadmap. Let’s start to implement the program. Remember: Implement in tiny steps. Always have a running program.

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

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 Program Running We have a working program!

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 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 Program Running

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 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 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 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 Compile Errors! Add declarations for best_index and i. Try again.

28 Program Running

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 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 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 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 Program Running

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 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 Program Running We do have a problem. Need leading 0 in minutes.

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

38 Program Running

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 Program Running

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 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 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 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 Program Running

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 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 Previous Day Example: Departure times: 8:00 AM 11:55 PM Target time: 1:00 AM

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

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 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;

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 Program Running

54 Program Running

55 Program Running

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