CS 112b
Fall 2000
Ileana Streinu

Homework 1

Thursday September 7, 2000
Due Wednesday 9/13/2000 by midnight.


Outline

You will write six small programs, very similar with each other and in increasing order of complexity. They should be named EXACTLY as I indicate: prog1.cpp, prog2.cpp, prog3.cpp, prog4.cpp, prog5.cpp and prog6.cpp. You will submit them in Hw1, together with a typescript file showing the compilation and execution, one after another, of all these programs.

Programs

  1. Write a small C++ program prog1.cpp which asks the user to enter two integers, and then prints the sum, the average, the product, the smallest and the largest of these numbers. The screen dialogue should appear as follows:

    Input two integers: 12 2
    Sum is 24
    Average is 7
    Product is 24
    Smallest is 2
    Largest is 12


  2. Write a program prog2.cpp which does exactly what prog1.cpp did, but using the following functions:
  3. prog3.cpp will compute the same functions as above, but for a vector (array) of n elements. The user enters first the number of elements n, then the vector, then the program computes (by calling the appropriate functions) and prints. You should use function calls, where Sum, Average, Product, Max and Min work now on an array (vector) of n integers.
  4. prog4.cpp is just a bit more complex. It still computes sum, average etc. but on a n x n matrix (two-dimensional array). As before, it should use functions Sum, etc., which add all the elements of the array (resp., compute average, product, min and max).
  5. prog5.cpp works like prog4 with an two-dimensional array, but it computes vectors of the row sums, averages, products, minima and maxima.
  6. prog6.cpp combines all previous programs into one. It asks the user: What do you want to do? and offers her the options: 1 for two numbers, 2 for a vector, 3 for a matrix, 4 for row-wise operations on a matrix. Depending on the number chosen by the user, it invokes the corresponding functions designed at the previous stages, and prints the results.

Last modified September 6, 2000.

Ileana Streinu