CPE 101 Sample Lab
Quiz
Student Name:_______________________________
OVERVIEW
This is an individual activity. Do not share your work
with others, and do not look at the work others are doing.
You will be writing a C program to perform some simple arithmetic
computations. You must construct a working solution on a computer
in the lab.
1. The quiz is open-book, closed-note, and closed-web
2. You may NOT use a personal laptop computer.
3. Make sure your code compiles with no warnings on hornet.
4. You are welcome to use jGRASP, but it isn’t required – you may work
entirely on hornet.
5. Read through the entire assignment and ask for necessary
clarifications immediately.
SUBMISSION REQUIREMENTS
1. Submit ALL pages of this quiz handout with your name.
2. Make sure your program has the following comment block at the top of
the file.
/*
* Your Name Here
*
* Lab Quiz 1
*
* CPE101
*/
3. Turn in your source files electronically using the following command
on hornet:
handin graderjd quiz1 quiz1.c
* Be sure your source compiles and runs correctly on hornet using the
specified compiler options (-lm -Wall –pedantic –ansi).
* Double check you electronic submission using the following command:
handin graderjd quiz1
Problem Statement
At a recent tortoise racing competition, your pet
"Speedy" took first place. The judges at the race determined that
Speedy's average rate of travel is one foot every ten seconds.
Just for fun, you are curious how long it would take Speedy to travel
from Cal Poly to Morro Bay (14 miles), to DisneyLand (232 miles), and
around the world (24,901 miles). Write a program that allows the
user to enter these distances in miles and computes Speedy's travel
time for each one.
Assumptions
Speedy's rate of travel is a constant within the
program. The distances entered in miles will be positive whole
numbers. The output should be a decimal number in hours, with two
decimal places. The program should display an appropriate
prompting message, read the user's input from the keyboard, and display
the results on the screen with an explanatory remark. No checking
is performed to see if the computations overflow the machine's
allowable numeric range, and consequently invalid results may occur.
Required Input
1. Three distances to walk (in miles).
Required Output
1. Prompts for input:
"Please enter the first distance for Speedy to travel (in miles):"
"Please enter the second distance for Speedy to travel (in miles):"
"Please enter the third distance for Speedy to travel (in miles):"
2. An explanatory remark: "It would take Speedy
_____ hours to travel ______ miles." printed for each result.
3. The number of hours (decimal number with 2 decimal
places).
4. The travel distance (same value as input).
Needed Constants and Formulas
Compute travel time ( Distance divided by rate )
Given: SpeedyRate = 1 ft / 10 sec
1 hour
= 3600 sec
1 mile
= 5280 feet
and
user's input: Distance
formulas:
DistInFeet = Distance * 5280
RateInFeet = 3600 * SpeedyRate
TravelTime = DistInFeet /
RateInFeet
SPECIFICATIONS
1. Obtain from the ~cs101-1 account on hornet a
skeleton file named quiz1skeleton.c.
2. Rename the file to quiz1.c.
3. Add the necessary program statements to create a complete program
that solves the problem above:
a. Obtains the input data from the user and store it in a variable of
type int. You may assume that the user will only
enter whole numbers.
b. Write a function named FindTravelTime to perform all
arithmetic calculations.
c. Invoke the function as appropriate from the main program to find the
travel time for each distance entered.
d. Display the result computed for each input before asking for the
subsequent input (see example below).
4. Here is a sample run. The user’s inputs to the program are
shown in bold for clarity – your program doesn’t have to do bold
text. With the exception of the input data and results, the program
must match the prompts and output exactly as shown including, spelling
and spacing. (Note that “xxx.xx” below will be replaced
with the actual numeric results).
Please enter the first distance for Speedy to travel (in miles): 14
It would take Speedy xxx.xx hours to travel 14 miles.
Please enter the second distance for Speedy to travel (in miles): 232
It would take Speedy xxx.xx hours to travel 232 miles.
Please enter the third distance for Speedy to travel (in miles): 24901
It would take Speedy xxx.xx hours to travel 24901 miles.
(Please note that when your program is graded, different distances may
be provided as input than shown above.) Partial credit will be
awarded for compiling, partial functioning programs. You do
not need to provide comments in your code (except the header block).
TEST DATA
Show your manual calculations here for the expected result when the
distance is 10 miles. Circle your result.