COEN 243: Programming Methods I

Midterm Exam 1 (section U)

Time Allowed: 60 Minutes

Total Marks: 15

Name (first, LAST) :______, ______

Student No.:……….…...………………….

Signature : ………………………………

1.  Determine the values stored in the variables after the execution of the following programs or program segments.

a). With keyboard input: -3.2e+4e+4

// Begin

int m;

float x;

char letter;

// executable statements

cout < ”Please type your input”;

cin > m>x>letter;

// End

m=______, x=______, letter=______. (2 marks)

a). // Begin

using namespace std;

int fun1(int);

int x=1;

int main()

{ int m=2, n=5;

m=fun1( fun1(m%n)+1);

cout<‘’x=‘’<x<‘’,’’‘’m=‘’<m<endl;

return 1;}

int fun1(int m)

{ m=m*m; x=x*m; return x/m;}

// End

would print: x=______, m=______.

(3 marks)

2.  Write a program that reads a vehicle’s value (in thousands) and other insurance options and print the estimated total insurance cost. The insurance cost of a vehicle consists of there parts: Basic, Theft, and Collision. Among them, Theft and Collision are optional. However, if both Theft and Collision are elected, a discount of 100 dollars is applied. The three parts are calculated against the vehicle value as below:

Value x (in thousands) Basic Collision Theft

0<x<=10 500 200 0

10<x<=30 500 200+(x-10)*30 200

x>30 500 800+(x-30)*40 200+(x-30)*10

(5 marks)

3.  Answer one of the two questions below: (5 marks)

a). Write a function that takes a letter grade (A. B. C. D. F) as input and returns the corresponding number of credits. A student gets 4 credits if he has a grade A in a course. Similarly, he gets 3 credits for B, 2 for C, 1 for D, and 0 for F. Also use the function to write a program to calculate the average number of credits of a student among the courses he or she has taken. The program reads in letter grades (A. B. C. D. F) of the courses taken by the student till a char ‘E’ being typed in. Any other input is regarded as a mistake and will be ignored.

b). Write a program that reads two integers and print their least common multiplier. The LCM of a pair of integer numbers is the least number that is a multiple of both of the two integers. For instance, the LCM of 10 and 11 is 110 and the LCM of 10 and 12 is 60. (Hint: To find the LCM of two integers, start with the greater integer, which is a multiple of the greater integer, and check if it is a multiple of the smaller one. If it is, the number is the LCM; otherwise, proceed to check the next immediate multiple of the greater integer.