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

Homework 2: Due Thursday, 2/6/97

Problem 1: [7 points]

 
	#include <iostream.h>
	int main()
	{
		int x, answer;
	 	cout >> "Please enter an integer: ";
		cin >> x;
		answer = x + 1;
		answer = x * answer + 1;
		answer = x * answer + 1;
		answer = x * answer + 1;
		cout << "The value of the expression is " << answer << endl;
		return 0;

}

Problem 2: [7 points]

 
        #include <iostream.h>
       	// This program reads the value of a key and a sequence of 100
	// integers and searches for key in the sequence.
        int main()
        {
                int number, key, counter, found;

                // Prompt the user
		cout << "Please type the key: ";
		cin >> key;
                cout << "Please type 100 integers" << endl;

               	// Read and process the rest of the integers
               	counter = 0;
		found = 0;
                while (counter < 100)
               	{
                       	cin >> number;
                       	if (key == number)
                       		found = 1;
                        counter = counter + 1;
                }

		if (found == 1)
			cout << "key is found in the sequence" << endl;
		else
			cout << "key is not found in the sequence" << endl;
                return 0;
        }

Problem 3: [6 points]

(a)
 
	#include <iostream.h>
	void dream() // 3, 10, 15
	{
		cout << "I have a dream today." << endl; // 4, 11, 16
		cout << "I have a dream that one day" << endl; // 5, 12, 17
	}

	void manyDreams() // 7
	{
		cout << "every valley will be exalted. " << endl; // 8
		dream(); // 9
		cout << "this nation will rise up and live out"
		<< "the true meaning of its creed." << endl; // 13
		dream(); // 14
	}

	int main() //1
	{
		dream(); //2
		manyDreams(); // 6
		cout << "this land will be transformed into "
		<< "an oasis of freedom and justice." << endl; // 18
		return 0; // 19
	}

(b)
I have a dream today.
I have a dream that one day
every valley will be exalted. 
I have a dream today.
I have a dream that one day
this nation will rise up and live out the true meaning of its creed.
I have a dream today.
I have a dream that one day
this land will be transformed into an oasis of freedom and justice.



Sriram Pemmaraju
Sun Feb 2 14:48:43 CST 1997