CS126L Practical Examination #2
#1. A mathematical generating a sequence of numbers from any positive integer n (n >
0) as follows:
If n is 1, stop.
If n is even, the next number is n/2.
If n is odd, the next number is 3*n + 1.
Continue with this process until reaching 1.
Here are some examples for the first few integers.
2 -> 1
3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
4 -> 2 -> 1
5 -> 16 -> 8 -> 4 -> 2 -> 1
6 -> 3 -> etc as for 3 above.
7 -> 22 -> 11 -> 34 -> 17 -> 52 -> 26 -> 13 -> 40 -> 20 -> 10 -> 5 -> see
5 above.
Do you want to try again? (y/n):
Write a program that reads a number and prints the mathematical sequence for it.
#2. Write a program that reads daily temperatures, as floats. The user key in the
number of data sets to be process. Read in a loop until all data have been read. After
the input has been read, print the maximum and minimum values. You can assume that
there is at least one input data value.
Enter number of input values: 7
Sample input values
10.0 11.3 4.5 -2.0 3.6 -3.3 0.0
The output should look something like the following. Formatting may vary.
Maximum = 11.3
Minimum = -3.3
Do you want to try again? (y/n):
#4. Write a program which reads a sequence of integers and counts how many times
the number 55 occurs. Print the count. For example, If the user input the number of
integers to be process as 8 and the integer you are to search from, with input values
55 25 1004 4 -6 55 0 55, the output should be 3 because there were 3 55's in the input
stream. Your program should work for any input, not just this example.
#5 Write a program that will accept an n integer from the user via the keyboard, The
dialog for this program should look as follows:
Enter number of integers to process: 3
Enter the integers: 7 20 17
Largest = 20
Smallest = 7
Median = 17
#6 Write a program should read in values until a negative value is read in. It should
determine the largest value that was read in and display that value once the loop stops.
A sample dialog for this program looks as follows:
Enter an integer: 5
Enter an integer: 1
Enter an integer: 15
Enter an integer: 9
Enter an integer: -1
Largest = 15
Do you want to try again? (y/n):
#7 Write a program should read in values until a negative value is read in. It should
determine the smallest value that was read in and display that value once the loop
stops. A sample dialog for this program looks as follows:
Enter an integer: 5
Enter an integer: 1
Enter an integer: 15
Enter an integer: 9
Enter an integer: -1
Smallest = 1
Do you want to try again? (y/n):
#8 Write a program that should first read in two values from the keyboard, a starting
value and an ending value. It should sum all the even numbers between those two
values including the endpoints and display the sum. If the ending point is less than the
starting point, the sum should be 0. A sample dialog for this program looks as follows:
Enter starting and ending: 12 15
Sum of evens = 26
Do you want to try again? (y/n):
#9 Write a program that should first read in two values from the keyboard, a starting
value and an ending value. It should sum all the odd numbers between those two
values including the endpoints and display the sum. If the ending point is less than the
starting point, the sum should be 0. A sample dialog for this program looks as follows:
Enter starting and ending: 10 20
Sum of odds = 75
Do you want to try again? (y/n):
#10 Write a program that computes the sum of all the integers in a range specified by
the user. The program would accepts two parameters that specify a lower and upper
bound. It should compute the sum of all the integers between those bounds, inclusive,
and display that sum. A sample dialog for this program looks as follows:
Enter a lower and upper bound: 2 4
Sum = 9
Enter 0 to quit, 1 to continue: 0
#11. Write a program that sums the values that are input that are within the specified
bounds. The program should prompt the user for the lower and upper bounds and then
successively prompt the user for values until a negative value is input. The program
should also determine whether the value is within bounds. The program should accept
the value and the bounds as parameters and return a boolean value indicating whether
the value is with the bounds inclusive of the end points. A sample dialog for this
program looks as follows:
Enter bounds: 10 20
Enter value: 15
Enter value: 5
Enter value: 10
Enter value: -1
Sum = 25
Do you want to try again? (y/n):
Do not reverse the lower and upper bounds supplied by the user if the lower bound if
greater than the uppper. If the lower bound was a 10 and the upper bound a 5, for
example, the sum should be zero no matter what values are input.
#12 Write a program that reads in integers as long as they continue to be input in
increasing order, that is each successive number must be greater than or equal to the
previous. As soon as the sequence is no longer increasing the program should stop and
output the number of values in increasing order. A sample dialog for this program looks
as follows:
Enter a number: 2
Enter a number: 5
Enter a number: 7
Enter a number: 0
Increasing count = 3
Do you want to try again? (y/n):
#13 Write a program that prompts the user for a number and computes the nth term of
the following sequence of numbers referred to as the sequence of pentagonal numbers:
A sample dialog for this program looks as follows:
Enter a number: 5
Sum = 35
Do you want to try again? (y/n):
#14 Write a program that prompts the user for two integers, reads them in and then
allows the user two choices: to compute the minimum of those two values or the
maximum. If the user inputs an invalid choice, set the result to 0. A sample dialog for
this program looks as follows:
Enter two integers: 8 35
Enter 1 for min, 2 for max: 1
Result = 8
Do you want to try again? (y/n):
#15 Write a program that reads in integers until 10 have been read in or until a
negative number has been input. As the numbers are read in they should be summed
and the sum should be displayed once all the integers have been input. The negative
integer, should be one of the inputs and 10 are not to be included in the sum. A sample
dialog for this program looks as follows:
Enter an integer: 8
Enter an integer: 2
Enter an integer: -1
Sum = 10
Do you want to try again? (y/n):
#16 Write a program that requests the user to input an integer n. It should compute
the sum of the first n integers that are multiples of 3, the sum 3 + 6 + ... + 3n, using
the following formula:
A sample dialog for the program is shown below:
Enter an Integer: 3
Sum = 18
Do you want to try again? (y/n):
#17 Write a C program that reads the lengths of three sides of a triangle and prints out
the classification of the triangle as follows:
invalid input: all sides must have lengths that are strictly positive
not a triangle: the sum of any two sides must be greater than the length of the
third
scalene triangle: no two sides have equal length
isosceles triangle: two sides (but not the third) are equal
equilateral triangle: all three sides are equal
Provide validation all data entries and ask if the user wants to try running again the
program.
Enter the three sides of a triangle: 2.5 2.5 2.5
IT’S AN EQUILATERAL TRIANGLE.
Do you want to try again? (y/n):
#18 Write a C++ program that asks the user to enter his or her weight and the name
of a planet. The program was then to output how much the user would weigh on that
planet. . The following table gives the factor by which the weight must be multiplied for
each planet. The program should output an error message if the user doesn't type a
correct planet name. The prompt and the error message should make it clear to the
user how a planet name must be entered. Be sure to use proper formatting and
appropriate comments in your code. The output should be clearly labeled and neatly
formatted.
Mercury 0.4155
Venus 0.8975
Earth 1.0
Moon 0.166
Mars 0.3507
Jupiter 2.5374
Saturn 1.0677
Uranus 0.8947
Neptune 1.1794
Pluto 0.0899
#19 Write a C++ program that reads a time in numeric form and prints it in English.
The time is input as hours and minutes, separated by a space. Hours are specified in
24-hour time (15 is 3 p.m.), but the output should be in 12-hour a.m./p.m. form. Note
that noon and midnight are special cases. Here are some examples:
Enter time: 12 00
Noon
Enter time: 0 00
Midnight
Enter time: 6 44
Six forty four AM
Enter time: 18 11
Six eleven PM
#20 Write a program that asks the user to type a positive integer. When the user types
a negative value the program writes ERROR and asks for another value. When the user
types 0, that means that the last value has been typed and the program must write the
average of the positive integers. If the number of typed values is zero the program
writes 'NO AVERAGE'. Provide data validation for all data entries. Ask the user if he/she
wants to try again. If the user answers Y, then the program restarts. Otherwise, the
program exits.
#21 Write a program that asks the user to type an integer N and compute u(N) defined
with :
u(0)=3
u(1)=2
u(n)=n*u(n-1)+(n+1)*u(n-2)+n
Provide data validation for all data entries. Ask the user if he/she wants to try again. If
the user answers Y, then the program restarts. Otherwise, the program exits.