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

Exam 1: Friday, 2/21/97

This exam is 50 minutes long and you are welcome to consult your books or notes during the exam. The exam is printed on 4 sides and you are expected to write your answers in the space provided.

What is your name?

What is your section number?

Problem 1: [50 points]

A sequence of integers is said to be Fibonacci-like if each integer (except the first two) is the sum of the previous two integers in the sequence. For example, the following sequence is a Fibonacci-like sequence:

10 14 24 38 62 100 162 262

Note that the first two integers in the above sequence are arbitrary. Each of the remaining integers is the sum of the two integers just before it in the sequence. For example, 24 = 10 + 14, 38 = 14 + 24, 62 = 24 + 38, and so on.

Write a program that reads a sequence of 100 integers and determines if the sequence is Fibonacci-like or not. In particular, the program should produce either the message

The sequence is Fibonacci-like

or the message

The sequence is not Fibonacci-like

As a model, you may want to use the program that we wrote for searching in a sequence of integers for a key (Problem 2 in Homework 2).

Problem 2: [50 points]

(a)
[40 points] In this problem you are given an incomplete C++ function; your task is to add the missing code. The function reads in an arbitrary number of letter grades and produces the corresponding numerical grade point average.

The function has the header

 
void gradePointAverage(string grade1, double value1,

string grade2, double value2,

string grade3, double value3,

string grade4, double value4)

This function starts by prompting the user for letter grades. The user enters letter grades separated by white spaces and then enters the letter Z to indicate that all letter grades have been entered. The 8 parameters of the function provide a ``table'' for converting letter grades into numeric values. In particular, the 4 string parameters of the function: grade1, grade2, grade3, and grade4 represent the 4 possible letter grades that can be entered by the user and the 4 double parameters value1, value2, value3, and value4 represent the numeric values respectively of these 4 letter grades. For example, it is possible (but not necessary that) grade1 equals "A", grade2 equals "B", grade3 equals "C", grade4 equals "D", value1 equals 4.0, value2 equals 3.0, value3 equals 2.0, and value4 equals 1.0. The function compares each entered letter grade with each of grade1, grade2, grade3, and grade4 and determines the corresponding numeric value for the letter grade. Once Z has been entered, the function calculates and prints the corresponding grade point average. You may assume that all courses for which the grades were entered have the same credit hours. This means that the grade point average is simply the sum of the numeric values of the grades entered divided by the number of grades entered. Note that the grade point average should be a real number ( double) and not an integer ( int or long int). It is possible that the user simply enters Z as the first and only input; In this case, the function should print an appropriate message, such as:

No grades have been entered.

instead of calculating the average. You may assume that the user will always provide input of the correct form.

The function gradePointAverage that is missing certain pieces of code is given on the next page. Your task is to fill in this code.

 
void gradePointAverage(string grade1, double value1,

string grade2, double value2,

string grade3, double value3,

string grade4, double value4)

{

//declare variables here

cout << "Enter a letter grade [enter Z to quit]: " << endl;

cin >> grade;

while(grade != "Z")

{

//convert the letter grades into corresponding numeric values

if(grade == grade1)

value = value1;

//accumulate the sum

//increment the number of grades entered

cout << "Enter a letter grade [enter Z to quit]: " << endl;

cin >> grade;

}

//calculate the average if grades other than Z

//were entered, and produce the appropriate message

}

(b)
[10 points] Write a program that reads letter grades typed by a user and computes the corresponding grade point average. The program assumes that the letter grades are separated by white spaces and are terminated by the character Z. The possible letter grades are: Excellent, Good, Fair and Poor with corresponding numeric values 10.0, 8.0, 6.0, and 4.0 respectively. Your program should call the function gradePointAverage written in part (a) with the appropriate arguments to perform its task.

Note: Almost everything this program needs to do is done in the function gradePointAverage, so this program contains a main function that simply makes an appropriate function call with the appropriate arguments.



Sriram Pemmaraju
Wed Jul 2 16:53:58 CDT 1997