Problem 1: [7 points]
This problem is partly a puzzle and partly an exercise in writing a simple C++ program. Write a C++ program that reads in an integer x and produces as output the value of the expression:
So the main task that your program needs to perform is computing the value of the above expression. This is fairly straightforward given what you have already learned. But, I want your program to satisfy some additional requirements: your program should use no more than 4 addition operations (in all) and no more than 3 multiplication operations (in all). It may be possible to do it using fewer arithmetic operations. This additional requirement does not affect the correctness of the program you write, but it does affect the efficiency (that is the amount of time taken to run) of your program.
Hint: In my solution the only addition operations used are increment operations, that is, operations that increase a value by 1.
Problem 2: [7 points]
Write a C++ program to solve the following problem:
Given an integer called key and a sequence of 100 integers determine whether key is an element in the given sequence.Your program should start by prompting the user for key and should then prompt the user for the sequence of 100 integers. The main task that your program needs to perform is searching for key in the given sequence. After searching for key, your program should produce a message indicating to the user whether the key was found or not. You should start with the program that computed the maximum of 100 integers (refer to Problem 2 in Homework 1) and then modify this program to one that solves the above problem. The modifications you make should be fairly small.
Problem 3: [6 points]
Consider the following program:
void manyDreams()
{
cout << "every valley will be exalted. " << endl;
dream();
cout << "this nation will rise up and live out"
<< "the true meaning of its creed." << endl;
dream();
}
int main()
{
dream();
manyDreams();
cout << "this land will be transformed into " <<
<< "an oasis of freedom and justice." << endl;
return 0;
}
#include <iostream.h>
void dream()
{
cout << "I have a dream today." << endl;
cout << "I have a dream that one day" << endl;
}
Acknowledgement: The influence on this problem of the 1963 speech in Washington D.C. by Dr. Martin Luther King, Jr. is acknowledged.