Introduction to Programming (22C:16, 22C:106)

Homework 1: Due Thursday, 1/30/97

Problem 1: [7 points]

Write an algorithm (in the form of a flowchart) to perform the following task:

Find the maximum integer in a given sequence of 100 integers.

Problem 2: [13 points]

Given below is a C++ program that performs the task mentioned in Problem 1.

 
	#include <iostream.h>
	// This program reads a sequence of 100 integers and produces as output
	// the value of the largest.
	int main()
	{
		int number, maximum, counter;

		// Prompt the user
		cout << "Please type 100 integers" << endl;

		// Read the first integer
		cin >> maximum;

		// Read and process the rest of the integers
		counter = 1;
		while (counter < 100)
		{
			cin >> number;
			if (number > maximum)
				maximum = number;
			counter = counter + 1;
		}
		cout << "The largest number is " << maximum << endl;
		return 0;
	}
  1. [6 points] Rewrite the above program so that it not only computes the maximum integer in the sequence, but also the minimum integer.

  2. [7 points] Rewrite the above program so that it not only computes the maximum integer in the sequence, but also the second largest integer. Note that all integers in the sequence may be identical, in which case there is no second largest. In this case, your program should produce an appropriate message.



Sriram Pemmaraju
Sat Jan 18 20:39:32 CST 1997