






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Dr. K. Narayanaswamy Midterm Examination
You have 75 minutes to complete this examination. OPEN BOOK AND OPEN NOTES: -- you can use any and all reference materials. (Laptops and cell phones are NOT allowed)
YOU MUST ANSWER ALL QUESTIONS. PART I (which starts on Page 2 and ends on Page 7) contains very brief questions, mostly of the multiple choice variety, and some that require brief responses within the provided spaces. In PART II , which starts on page 8, there are 5 questions in all. These questions require longer responses. If you need more space (which should not normally be the case), make a note that your answer is continued overleaf, and write on the back of the page.
NAME: ________________________________
STUDENT ID: _____________
Question 1 10 Question 2 10 Question 3 10 Question 4 10 Question 5 10 TOTAL (100)
otherwise, each question is worth 1 point.
Questions 1, 2, 3, 4, 5, 6, 7, 8 and 9 are based on code below: void myfun (int x, int & y) { int temp = x; x = y; y = temp; }
Consider the following code: (for questions 18, 19, 20, and 21)
const int size = 7; int scores[size][size];
18.The number of elements in the scores array is: a) 64 b) 49 c) 63 d) 48
0 0 0 0 0 0 0 0 0 0 0 0 0
bool read_scores (int scores[size][size], string inputfilename) {
}
bool initialize_student (string ssn, string name, string address, int age, Student x);
segments. Your answers should fit into the space provided. Otherwise, indicate that you are continuing on the backside of the page, and finish your answer there. There are 5 questions in this part. Each carries 10 points.
// returns true if “n” is a fibonnaci number; false otherwise bool fibonacci (int n) { … }
a) Fill in the body of the function is_prime below: (4 points)
bool is_prime (int n) // returns true if n is a prime number, false otherwise {
}
b) Fill in the body of the following function: (3 points) bool both_prime_and_fibonacci (int n) {
}
c) Fill in the body of the following function: (3 points) bool prime_but_not_fibonacci (int n) {
}
const int size = 10; int A [size];
a) Why is it better to define the array size using a constant? (1 point)
b) Write the body of the following function. “m” is the smaller index and “n” is the higher index. You should return a value of ERROR (assumed to be a global integer constant) if m or n is invalid in any way or if m is larger than n. (6 points). // returns the minimum of the numbers of the array “input” from index m to n (including n) int minimum (int input[size], int m, int n) {
}
c) What is the terminal output produced by the following code: (3 points)
for (int n= 0; n < size; n++) A[n] = size – n ; cout << “Minimum: “ << minimum(A, 4, 7) << endl;
Use the working area below IF YOU WISH to show the contents of Array A: INDEX VALUE
Consider the code segment below:
string temp; ifstream inputfile; ofstream outputfile; inputfile.open(“input.txt”); outputfile.open(“output.txt”); while ((inputfile >> temp) != NULL) { outputfile << temp; outputfile << endl; }
AND, given the contents of the file “input.txt” as below: There are 99 different emergency broadcast systems in the 90043 zip code!!!
a) Write down the exact contents of the file “output.txt”. AFTER executing the code above (4 points)
b) Write the body of the function called file_length below that accepts a filename as parameter, opens the file, and returns as result the length of the file in characters (i.e., every character read from the file, regardless of what the character is, increments the length by 1). If the file does not exist return -1. (6 points) int file_length (string inputfilename) {
}