You are on page 1of 1

Programming Exercises 25

Engineering R1.21 Suppose you know how long it takes a car to accelerate from 0 to 60 miles per hour.
Develop an algorithm for computing the time required to travel a given distance (for
example 5 miles), assuming that the car is initially at rest, accelerates to a given speed
(for example 25 miles per hour), and drives at that speed until the distance is covered.
Hint: An object that starts at rest and accelerates at a constant rate a for t seconds
1
travels a distance of s = at 2 .
2

PROGRAMMING EXERCISES

P1.1 Write a program that prints a greeting of your choice, perhaps in another language.
P1.2 Write a program that prints the message, “Hello, my name is Hal!” Then, on a new
line, the program should print the message “What would you like me to do?” Then
it’s the user’s turn to type in an input. You haven’t yet learned how to do it—just use
the following lines of code:
string user_input;
getline(cin, user_input);
Finally, the program should ignore the user input and print the message “I am sorry,
I cannot do that.”
This program uses the string data type. To access this feature, you must place the line
#include <string>
before the main function.
Here is a typical program run. The user input is printed in color.
Hello, my name is Hal!
What would you like me to do?
Clean up my room
I am sorry, I cannot do that.
When running the program, remember to press the Enter key after typing the last
word of the input line.
P1.3 Write a program that prints out a message “Hello, my name is Hal!” Then, on a new
line, the program should print the message “What is your name?” As in Exercise
P1.2, just use the following lines of code:
string user_name;
getline(cin, user_name);
Finally, the program should print the message “Hello, user name. I am glad to meet
you!” To print the user name, simply use
cout << user_name;
As in Exercise P1.2, you must place the line
#include <string>
before the main function.
Here is a typical program run. The user input is printed in color.
Hello, my name is Hal!
What is your name?
Dave
Hello, Dave. I am glad to meet you!

cfe2_c 01_p1_28.indd 25 10/25/10 1:03 PM

You might also like