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 a single Python code that does all of the following:

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

(b) working with the same array as in (a), writes the list of numbers in reverse order (i.e. backwards from the order they were read) using a for loop.

(c) again, working with the same array, finds the lowest number in the list and writes the result.

Hand in both your code

Hint: In part (a) define a variable, say sqsum, 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 sqsum = sqsum + ith squared value. Where the ith squared value is the square of the ith value in your array.

Hint: In part (c) start by setting the tentative lowest value xlow 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 lower, replace your tentative value with the higher value.

This page is maintained by:
Carleton DeTar
Last modified 11 September 2017