Exam 2 - Answers to Problems 5 - 8
5: Suppose A is a two-dimensional array of integers with N rows and N columns. Then
a) retrieve the value of A that is stored in row 3, column 4, and print it to the screen.
b) store the value -10 in the entry in row 4, column 3
c) find the sum of the numbers in column 0.
d) find the sum of all numbers in the array
e) find the sum of the numbers along the main diagonal
6. Questions dealing with file input and output.
a) If "test.dat" contains double numbers, one per file, find the largest and smallest number and write them to an output file.
f >> number;
double max = number, min = number;
if (number > max)
max = number;
if (number < min)
min = number;
f >> number;
f << min << endl;
f << max << endl;
b) Write a program to count the number of lines in a text file.
7. Suppose the start of a program looks as follows:
a) create a function to display an array
b) create a function to add two arrays A and B into a suitable output array C
c) a function that swaps two numbers in an array A
8. Define a type 'Boolean', write a function IsNegative (to return true if an input integer is negative), and a function showBoolean (to display a boolean on the screen). Then write a complete program to test everything.