CMPT 120: Lab 4


Write a program that uses nested loops to print out what I'll call a 'powers' table. This is like a multiplication table, but instead of multiplying the row by the collumn, take the row to the column power. Read in the number of rows and columns from the user, don't worry about checking for bad input. Here's an example run.
How many rows would you like in the powers table? 4
How many columns would you like in the powers table? 3

1     1     1
2     4     8
3     9     27
4     16    64
Use the tab character '\t' to line up the columns. For example the statement print "1\t1\t1" would print out the first line of the table from above.


Write a function named square that takes one number as input and returns its square, square(5) returns 25 and so forth. Now write a function named difference that takes two numbers as inputs and returns the difference between the two as a non-negative number. For example difference(2,4) and difference(4,2) should both return the value 2.

Once you've written these functions write a program that reads two numbers from the user. Then it will print out the difference between the numbers, the squares of the numbers, and the difference between the squares of the numbers. Use the functions you wrote, there should be no math done in your main program. Here's an example run.
Enter the first number: 5
Enter the second number: 8
The difference between these numbers is 3.
The squares of these numbers are 25 and 64.
The difference between the squares is 39.



For the last part of this lab you will be taking previously written code and changing it into a function. I would reccomend using the posted solutions instead of writing the code from scratch.

From Lab 2, create a function that takes a student's percentage grade as input, and returns a string containing the student's letter grade. Write a short main program to demonstrate the function is working.

From Lab 3, create a function that takes as input a string and prints out the positions of all the spaces in the string or a message if there are no spaces. The function will not return a value. Write a short main program to demonstrate that the function is working.


Chris Schmidt, last updated May 29, 2007