Physics 3730/6720 Assignment 3

Reading and references:

For each of the following exercises create the specified files with your answer(s). Submit your homework using the course submit utility.


Exercise 1. Practice with for loops

Here are some algorithmic puzzles that arise very often in programming. Write C++ code practice.cc that does all of the following:

(a) using a for loop, reads a list of 10 floating point numbers from standard input (i.e. use cin), storing them in an array, adds up their squares using a "for" loop, and writes the sum of the squares.

(b) writes the list of numbers in reverse order (i.e. backwards from the order they were read) using a for loop.

(c) finds the highest number in the list and writes the result.

Be sure to create a Makefile for compiling your code. The make target (and name of the executable) should be practice so you compile with the command make practice and execute with the command practice. Hand in both your Makefile and code practice.cc. Note that Exercise 3 below also requires a Makefile. Hand in only one file that combines the rules for both exercises.

Hint: In part (a) define a variable, say sum, that holds the running total. Then inside a for loop that counts with i going from 0 to 9, add to the running total using the assignment sum = sum + . Where the ith squared value is the square of the ith value in your array.

Hint: In part (c) start by setting the tentative highest number to the first one in the list and then going through the rest of the list, comparing it with each value. When you find one that is higher, replace your tentative value with the higher value.


This page is maintained by:
Carleton DeTar Mail Form
Last modified 16 September 2009