Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

Time on Your Hands Have you ever had time on your hands? Was it a mess? Let's start this chapter off by writing a program

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Time on Your Hands Have you ever had time on your hands? Was it a mess? Let's start this chapter off by writing a program that adds and subtracts time. It should ask the user for two input val- ues-a time (like 3:57) and duration (such as 1:05). Then, print the sum (here 5:02) and difference (2:52) This problem is a little more difficult than the one you solved for Chapter 0. Install the starter code by typing get-starters in the terminal, and we'll get going. Along the way, we'll learn quite a bit about the C++ Programming Language and dealing with integers. 1. Planning a Solution One of the reasons that this program is a little bit more difficult because we there are several different ways you can solve it. The first difficulty is figuring out how to read a value like 3:57 from the keyboard. The second question we have to answer is: "how do you want to represent the time elements"? You really have three choices: As a single real (or floating-point) number representing a fractional hour. For instance, you can store the time 3:57 as the floating-point number 3.95. As a single integer representing whole minutes. In this case, the time 3:57 would be stored as the integer 237. As a pair of integers, one representing the hours and one for the minutes. Each has its own benefits and drawbacks. The second is by far the easiest, and so we'll use that 2.Mockup the Interactions Here's what I'd like the interaction to look like: sgilbert-H01: Time on My Hands ----------------------------------------- Give me a time (such as 3:57) and a duration 1 (such as 1:05), and I'll tell you the sum (that is, the time that follows the given time by the given duration), and difference (the time that precedes the given time by hat duration). Time: 3:57 Duration: 1:05 2 3 1:05 hours after, and before, 3:57 is [5:02, 2:52] kancelciorelhos I've split the interaction into three sections: a title and introduction, an input section, and an output section. Your actual solution will also have a section for processing, so that the basic form for the run() function will look like this: int run() // 1. Title and introduction // - blank Line // 2. Input section: prompt and input on same line // 3. Processing section - compute the results // - blank Line // 4. Output section: test data inside brackets [ ] return 0; Your Turn You should have enough information now to add the comments to the starter code and to use the cout object to produce the output shown in the mockup at the top of this sec- tion. Use the variables STUDENT and ASSIGNMENT in the first line, and don't hard-code the name and assignment that I have printed In Hoe we entered the input and output val- 3. The Input Section Before you can do input, you'll need variables to store the data you receive from the user. Regardless of you choose to represent your data in your calculations, you'll need to get four values from the user: The hours and minutes for the current time. These both need to be integers. The hours and minutes for the duration. These are also integers. Changing Values through Input Variables can be given values through input. In C++ you use the cin (see-in) object to read from the standard input stream. The extraction, get-from, or input operator (>>reads a value from input and converts and stores the result in your variable. You want to read an input that looks like this. Time: 3:57 Duration: 1:05 Although this would be fairly difficult in Java, in C++ it is fairly simple. Here's a good first draft, with only one small problem: // Prompt and read the input cout > timeHours; cin >> timeMinutes; cout > durationHours; cin >> durationMinutes; What's the problem? You haven't handled the colon separating the hours from the minutes! You can fix that in two ways. Because the colon is not a number, but a single character, the type of the variable should be char. This allows you to make your code a little more compact as well. // Prompt and read the input char discard; // won't keep this cout > timeHours >> discard >> timeMinutes; cout > durationHours >> discard >> durationMinutes; Method 2 - Unformatted Character Input Using the extraction operator is not the only way to use the cin object. The C++ input stream objects also support unformatted input using the traditional method" syntax you should already be familiar with. Calling the member function (a method in Java ter- minology) cin.get() simply removes and returns one character from the input stream. Using this technique, you can rewrite your code as: // Prompt and read the input cout > timeHours; cin.get(); // discard the colon cin >> timeMinutes; cout > durationHours; cin.get(); // discard next cin >> durationMinutes; 4.Processing Some Time Your next step is processing-organizing, analyzing, modifying and manipulating the in- put to produce our desired output. The simplest kind of processing: using arithmetic ex- pressions to perform numerical calculations. Before you get started, though, remember to do some planning. You know from the problem statement that if the user enters a time of 3:57 and a duration of 1:05, then the sum should be 5:02 and the difference should be 2:52. HUMEWURKU TIME UN YUURHANUS Let's take a look at some other expected inputs and outputs. Here's an Excel spreadsheet with some inputs and expected outputs. Time 3:57 12:00 12:00 5:15 Duration 1:05 0:38 1:15 7:59 Sum 5:02 12:38 1:15 1:14 Difference 2:52 11:22 10:45 9:16 Planning the Processing Just as we had four variables for our input, we'll need four variables for output. Let's call them: sumHours, sumMinutes, diffHours and diffMinutes You can use initialization, assignment and simple integer arithmetic to add or subtract the appropriate time and duration values. As you do, it's important to mentally trace the results so you have rough idea whether you've done the calculations correctly. AM Here's what happens. Note that I've marked the cells that don't work in light blue, Time 3:57 12:00 12:00 5:15 Duration 1:05 0:38 1:15 Sum 5:02 12:38 1:15 1:14 Difference 2:52 11:22 10:45 9:16 Sum 4:62 12:38 13:15 12:74 Difference 2:52 12:-38 11:-15 -2:-44 7:59 It's obvious that the nave solution yields the wrong answer, at least for the addition in the given set of inputs. And, if the addition is wrong, you can be pretty sure that the sub- traction is only accidentally correct, just like a "stopped clock is correct twice a day". Duration Min Time-Min 237 Time 3:57 12:00 12.00 5:15 Duration 1:05 0:38 1.15 7:59 Sum 302 758 795 794 Difference 172 682 645 -164 After 5:02:00 AM 12:38:00 PM 1.15.00 PM 1:14:00 PM 2:52:00 AM 11:22 00 AM 10 45.00 AM UM! 720 315 479 This looks like it's correct except for one value (when the difference becomes negative). Let's handle that later. Start with Pseudocode Before you start writing code, you should plan your calculation using pseudocode. Here's my pseudocode for the problem up to this point. Input section Prompt and read the time (time-hours and time-minutes) Prompt and read the duration (dur-hours and dur-minutes) Processing section time stand- ard library header. The two manipulators you need are setw which changes the col- umn width and setfill which affects how empty values in a column are filled, like this: Time on Your Hands Have you ever had time on your hands? Was it a mess? Let's start this chapter off by writing a program that adds and subtracts time. It should ask the user for two input val- ues-a time (like 3:57) and duration (such as 1:05). Then, print the sum (here 5:02) and difference (2:52) This problem is a little more difficult than the one you solved for Chapter 0. Install the starter code by typing get-starters in the terminal, and we'll get going. Along the way, we'll learn quite a bit about the C++ Programming Language and dealing with integers. 1. Planning a Solution One of the reasons that this program is a little bit more difficult because we there are several different ways you can solve it. The first difficulty is figuring out how to read a value like 3:57 from the keyboard. The second question we have to answer is: "how do you want to represent the time elements"? You really have three choices: As a single real (or floating-point) number representing a fractional hour. For instance, you can store the time 3:57 as the floating-point number 3.95. As a single integer representing whole minutes. In this case, the time 3:57 would be stored as the integer 237. As a pair of integers, one representing the hours and one for the minutes. Each has its own benefits and drawbacks. The second is by far the easiest, and so we'll use that 2.Mockup the Interactions Here's what I'd like the interaction to look like: sgilbert-H01: Time on My Hands ----------------------------------------- Give me a time (such as 3:57) and a duration 1 (such as 1:05), and I'll tell you the sum (that is, the time that follows the given time by the given duration), and difference (the time that precedes the given time by hat duration). Time: 3:57 Duration: 1:05 2 3 1:05 hours after, and before, 3:57 is [5:02, 2:52] kancelciorelhos I've split the interaction into three sections: a title and introduction, an input section, and an output section. Your actual solution will also have a section for processing, so that the basic form for the run() function will look like this: int run() // 1. Title and introduction // - blank Line // 2. Input section: prompt and input on same line // 3. Processing section - compute the results // - blank Line // 4. Output section: test data inside brackets [ ] return 0; Your Turn You should have enough information now to add the comments to the starter code and to use the cout object to produce the output shown in the mockup at the top of this sec- tion. Use the variables STUDENT and ASSIGNMENT in the first line, and don't hard-code the name and assignment that I have printed In Hoe we entered the input and output val- 3. The Input Section Before you can do input, you'll need variables to store the data you receive from the user. Regardless of you choose to represent your data in your calculations, you'll need to get four values from the user: The hours and minutes for the current time. These both need to be integers. The hours and minutes for the duration. These are also integers. Changing Values through Input Variables can be given values through input. In C++ you use the cin (see-in) object to read from the standard input stream. The extraction, get-from, or input operator (>>reads a value from input and converts and stores the result in your variable. You want to read an input that looks like this. Time: 3:57 Duration: 1:05 Although this would be fairly difficult in Java, in C++ it is fairly simple. Here's a good first draft, with only one small problem: // Prompt and read the input cout > timeHours; cin >> timeMinutes; cout > durationHours; cin >> durationMinutes; What's the problem? You haven't handled the colon separating the hours from the minutes! You can fix that in two ways. Because the colon is not a number, but a single character, the type of the variable should be char. This allows you to make your code a little more compact as well. // Prompt and read the input char discard; // won't keep this cout > timeHours >> discard >> timeMinutes; cout > durationHours >> discard >> durationMinutes; Method 2 - Unformatted Character Input Using the extraction operator is not the only way to use the cin object. The C++ input stream objects also support unformatted input using the traditional method" syntax you should already be familiar with. Calling the member function (a method in Java ter- minology) cin.get() simply removes and returns one character from the input stream. Using this technique, you can rewrite your code as: // Prompt and read the input cout > timeHours; cin.get(); // discard the colon cin >> timeMinutes; cout > durationHours; cin.get(); // discard next cin >> durationMinutes; 4.Processing Some Time Your next step is processing-organizing, analyzing, modifying and manipulating the in- put to produce our desired output. The simplest kind of processing: using arithmetic ex- pressions to perform numerical calculations. Before you get started, though, remember to do some planning. You know from the problem statement that if the user enters a time of 3:57 and a duration of 1:05, then the sum should be 5:02 and the difference should be 2:52. HUMEWURKU TIME UN YUURHANUS Let's take a look at some other expected inputs and outputs. Here's an Excel spreadsheet with some inputs and expected outputs. Time 3:57 12:00 12:00 5:15 Duration 1:05 0:38 1:15 7:59 Sum 5:02 12:38 1:15 1:14 Difference 2:52 11:22 10:45 9:16 Planning the Processing Just as we had four variables for our input, we'll need four variables for output. Let's call them: sumHours, sumMinutes, diffHours and diffMinutes You can use initialization, assignment and simple integer arithmetic to add or subtract the appropriate time and duration values. As you do, it's important to mentally trace the results so you have rough idea whether you've done the calculations correctly. AM Here's what happens. Note that I've marked the cells that don't work in light blue, Time 3:57 12:00 12:00 5:15 Duration 1:05 0:38 1:15 Sum 5:02 12:38 1:15 1:14 Difference 2:52 11:22 10:45 9:16 Sum 4:62 12:38 13:15 12:74 Difference 2:52 12:-38 11:-15 -2:-44 7:59 It's obvious that the nave solution yields the wrong answer, at least for the addition in the given set of inputs. And, if the addition is wrong, you can be pretty sure that the sub- traction is only accidentally correct, just like a "stopped clock is correct twice a day". Duration Min Time-Min 237 Time 3:57 12:00 12.00 5:15 Duration 1:05 0:38 1.15 7:59 Sum 302 758 795 794 Difference 172 682 645 -164 After 5:02:00 AM 12:38:00 PM 1.15.00 PM 1:14:00 PM 2:52:00 AM 11:22 00 AM 10 45.00 AM UM! 720 315 479 This looks like it's correct except for one value (when the difference becomes negative). Let's handle that later. Start with Pseudocode Before you start writing code, you should plan your calculation using pseudocode. Here's my pseudocode for the problem up to this point. Input section Prompt and read the time (time-hours and time-minutes) Prompt and read the duration (dur-hours and dur-minutes) Processing section time stand- ard library header. The two manipulators you need are setw which changes the col- umn width and setfill which affects how empty values in a column are filled, like this

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Successful Keyword Searching Initiating Research On Popular Topics Using Electronic Databases

Authors: Randall MacDonald, Susan MacDonald

1st Edition

0313306761, 978-0313306761

More Books

Students explore these related Databases questions