COMP 11 - Introduction to Computer Science - Fall 2002

Sample Questions for Exam 3

1.
Study the following program carefully, assuming that the necessary files have been #included. Then indicate what the values of the variables s, nums[0], nums[1], nums[2], nums[3], days[0], days[1], days[2] would be when the end of the main function is reached, i.e. right after the comment "The end!". If a variable has not been assigned a value write no value, or if it is undefined in the current scope please write undefined.

void doI(int *p);
void doS(int *p, string *s);

int main()
{
int nums[5] = {4,3,2,1,2};
string days[] = {"Sun", "Mon", "Tue",
"Wed", "Thu", "Fri", "Sat"};
int *pi1, *pi2;
string s, *ps1;

for (int j=0; j<5; j++)
cin >> nums[j];

pi1 = nums;
pi2=nums+2;
ps1 = days;

*pi1 +=2;
*(pi1+2) = *(pi1+1);
if (pi1 < pi2)
s = "Tufts";

doI(pi1);
doS(pi1+3,days);
doS(pi1+3,ps1+2);
// The end!
}

void doI(int *p) {
p+=1;
*p = *p + 1 ;
}

void doS(int *p, string *s) {
string s2;
s2 = s[*p];
*s = s2;
}
2.

Recall that the algorithms Selection-Sort and Bubble-Sort sort array by going through several iterations. This question is concerned with sorting in ascending order. For example, the array initialized to will be sorted into . The question asks you about the first iteration of the sorting algorithms. A. Show the contents of the array A from above after one iteration of Bubble-Sort. B. Will the algorithm perform a second iteration? (Answer Yes/No and justify the answer in 1-2 sentences) C. Show the contents of the array A from above after one iteration of Selection-Sort. D. Will the algorithm perform a second iteration? (Answer Yes/No and justify the answer in 1-2 sentences)

3.
Given the following class:

class Student {
public:
string name;
double gpa;
};
Write a function called lowest - its header is below - that takes an array of Student instances (called s), the size of s (called size), and a minimum gpa (called min), and returns the name of the student with the lowest gpa that is also >= min. If several students have the same gpa that is also the lowest gpa >= min, then return any of their names. If no student in s has a gpa of at least min, then return the string "none". For example, assume we have the following declaration in main:

Student c[4] = { { "Jane", 3.5 }, { "Joe", 3.0 }, { "John", 2.5 }, { "Jill", 3.0 } };
If you are not familiar with this notation, it initializes c such that c[0].name is "Jane", c[0].gpa is 3.5, c[1].name is "Joe", etc. Then lowest(c, 4, 0.0) should return "John", lowest(c, 4, 2.8) should return either "Joe" or "Jill", lowest(c, 4, 3.3) should return "Jane", and lowest(c, 4, 3.8) should return "none".

string lowest (Student s[], int size, double min)
4.
Write a function named InPrint that takes has 2 parameters: a one dimensional array of integers named A, and an integer N that holds the number of elements in the array to be used. InPrint prints the values of all elements of A but in the following order: A[0], A[N-1], A[1], A[N-2], ... For example, if N was 5 and A had the elements { 7, 3, -1, 4, 1 }, then InPrint should print the numbers: 7 1 3 4 -1. To get up to full credit, write InPrint using recursion and no loops. If you wish, you may change the arguments to InPrint. To get partial credit, use loops.
5.
Study the code for the functions msort and main

void msort(int A[][5],int rows) {
for (int i=0;i<rows;i++)
sort(&A[i][0],0,4);
}

int main()
{
int A[3][5] = {{1,2,5,4,3},{2,1,5,1,7},{9,8,7,6,5}};
// before the call
msort(A,3);
// the end!
return 0;
}
The function sort(int[],int startPos,int endPos), called by mosrt, is a standard sorting procedure which sorts a segment of an array between startPos and endPos in ascending order. For example, if the array X has the values
 index 0 1 2 3 4 5 value 5 -7 2 6 1 3
then the call to sort(X,2,4) affects only positions 2,3 and 4 and modifies the array to be
 index 0 1 2 3 4 5 value 5 -7 1 2 6 3
Your Task: Fill in the values of the array A when the main program reaches the points marked by ``before the call'' and ``the end!''.

before the call:
 row/column 0 1 2 3 4 0 1 2

the end:
 row/column 0 1 2 3 4 0 1 2

6.
Write a program that prompts the user for a file name. The format of data in this file is given below. The program reads the name field from each line in the file and prints to the screen the number of characters in the longest name in the file. The program should include a main function and at least one other function that reads a line from the file and returns the length of the name on that line. Each line in the file has the format: id name age. For example the file may include:

11111 Joachim 25
22222 Joel 22
33333 Javed 29
If this file is used then the program should print 7 to the screen (Joachim is the longest name and it has 7 characters). You may find it useful to recall that is s is a string in your program then s.length() returns the length of the string.