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

Homework 1: Due Thursday, 1/30/97

Problem 1: [7 points]

					START
					  |
					  |
					  V
			   Output "Please type 100 integers"
					  |
					  |
					  V
				     Input maximum 
					  |
					  |
					  V
		                     counter <- 1
					  |
					  |
					  V	     NO
		---------------> Is counter < 100? ------> Output maximum ---> STOP
		|			  |
		|			  | YES
		|			  V
		|		     Input number
		|			  |
		|			  |
		|			  V		NO
		|	         Is number > maximum? ------
		|			  |		    |
		|			  | YES		    |
		|			  V		    |
		|		   number <- maximum	    |
		|			  |		    |
		|			  |		    |
		|			  V		    |
		|		counter <- counter + 1 <----
		|			  |
	        |                         |
		 -------------------------

Problem 2: [13 points]

  1.  
    	#include <iostream.h>
           	// This program reads a sequence of 100 integers and produces as output
           	// the value of the largest and the smallest.
           	int main()
           	{
           		int number, minimum, maximum, counter;
           		// Prompt the user
           		cout << "Please type 100 integers" << endl;
           		// Read the first integer
           		cin >> maximum;
           		minimum = maximum;
           		// Read and process the rest of the integers
           		counter = 1;
           		while (counter < 100)
           		{
           			cin >> number;
           			if (number > maximum)
           				maximum = number;
           			if (number < minimum)
           				minimum = number;
           			counter = counter + 1;
           		}
           		cout << "The largest number is " << maximum
    << endl;
           		cout << "The smallest number is " << minimum
    << endl;
           		return 0;
           	}
    
  2.  
           	#include <iostream.h>
           	// This program reads a sequence of 100 integers and produces as output
           	// the value of the largest and the second largest
           	int main()
           	{
           		int number, maximum, secondMax, counter;
           		// Prompt the user
           		cout << "Please type 100 integers" << endl;
           		// Read the first integer
                   	cin >> maximum;
                   	secondMax = maximum;
                   	// Read and process the rest of the integers
                   	counter = 1;
                   	while (counter < 100)
                   	{
                   		cin >> number;
    			if ((number < maximum) && (secondMax == maximum))
    				secondMax = number;
    
                   		if (number > maximum)
    			{
    				secondMax = maximum;
                           		maximum = number;
    			}
    
                           	if ((number > secondMax) && (number < maximum))
    				secondMax = number;
    
                           	counter = counter + 1;
                    }
                   	cout << "The largest number is " <<
    maximum << endl;
    		if (secondMax == maximum)
    			 cout << "All numbers are identical" << endl;
    		else
    			 cout << "The second largest number is " <<
    secondMax << endl;
                    return 0;
            }
    



Sriram Pemmaraju
Sun Jan 26 17:06:27 CST 1997