Anda di halaman 1dari 3

GENE 121 Assignment 4 Winter 2013

------------------------------------------------------------------------------------------------------------------
This assignment will be completed in pairs. You have been automatically matched with a partner.

In this assignment you will be using loops for repetition and performing file I/O.

Question 1

Download points.dat, which contains a list of x and y coordinates (first column is x, second is
y). Write a program that:
• Outputs to a file:
• The first ten x and y values
• The average, maximum, and minimum of all the x coordinates and
• The average, maximum and minimum of all the y coordinates

Your program should handle cases where points.dat:


• Doesn’t exist
• Contains only spaces
• Contains only negative numbers

Use different versions of the file to test and debug your program. Submit your program
with output from the file points.dat given on Learn.

Question 2

Write a program that prompts the user for a base length, and then uses nested loops to draw a pyramid
with asterisks (*). It should be a symmetrical pyramid about the vertical centre. Each line should have
two more asterisks than the last, and the base length entered should be the number of asterisks in the
final line. The base length must be an odd integer >=1 and <=43, use a do-while loop to enforce that
this is true.

Example output:

Enter a base length


13
*
***
*****
*******
*********
***********
*************
Press any key to continue . . .
Question 3

A water taxi / delivery service company uses a boat on Georgian Bay to transport people and goods
from the main dock to various islands and ports on the mainland around the bay. The service charge for
a trip is $15.00 for each stop and $2.10 per kilometre of travel. If the taxi is not able to pick up a new
customer, you must pay for the return trip to the origin. A GPS records the (x, y) positions for each
stop. The position of the main dock is (0,0).

The file called taxi.txt contains the data representing whether the customer had to pay for the
return trip (1 or 0), the number of stops, and the positions of the stops, for all the trips made in April.
For example:
0 1 0 10.2
1 2 0 -3 4 -3
0 4 0 -3 4 -3 -7.5 2.6 5.1 6
: :
Explanation for the first line of data:
• Return trip is false
• Taxi made 1 stop
• Taxi drove 10.2 km north from (0,0) to (0,10.2)

Second line:
• Return trip is true
• Taxi made 2 stops
• Taxi drove 3 km south from (0,0) to (0,-3)
• Taxi drove 4 km east from (0,-3) to (4,-3)
• Taxi returned home from (4,-3) to (0,0)

Assume all trips are made in a straight line. Use appropriate data types (int, double, bool).

Your program should:


• Check that the file has opened correctly
• Output to a file:
o The distance and cost for the current trip, and the current totals for distance and cost
for the first four trips and every tenth thereafter (ie. the 14th, 24th, and so on)
o Total distance driven by the water taxi and the total amount of money collected by the
operator for all of the trips

Format your output using setw() and setprecision() in a table similar to the one shown below:

RetT # Stop Distance Cost Total Dist Total Cost


0 1 10.20 36.42 10.20 36.42
1 2 12.00 55.20 22.20 91.62
0 4 32.84 128.97 55.04 220.59
: :

Hint: The total distance for April is > 1800 km. Submit your code with the output described above.
Question 4

If you have not done so already, you should start using your reference sheet while programming for
the assignments and while answering practice problems (from the textbook, or old exams).

With this week’s lab partner, compare your reference sheet items for the previous weeks. Based on
your comparison, the TA feedback, and your increased programming experience, list two changes that
you would make to items from assignment 2 and 3.

List two additional items based on this week’s course material that you would add to your reference
sheet.

Anda mungkin juga menyukai