CS 112b
Spring 2001
Ileana Streinu
Homework 1
Thursday February 1, 2001
Due Wednesday 2/7/2001 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.
Your grade on this homework is based on how many of these tasks
you have been able to complete. A program is considered complete
if it compiles, executes and this is documented with a
readable typescript. You have to submit all the files, by the deadline
(sharp!).
Programs
- 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
- Write a program prog2.cpp which does exactly what prog1.cpp
did, but using the following functions:
- Sum, summing the two integers.
- Average, computing the average of the two numbers.
- Product, computing the product.
- Min, computing the minimum element.
- Max, for the maximum.
- Input, for entering the data.
- Output, for printing the result.
- 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.
- 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).
- prog5.cpp works like prog4 with an two-dimensional array, but
it computes vectors of the row sums, averages, products, minima
and maxima.
- 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.
Ileana Streinu