CSCI 101L - Data Structures Practice problems for Final Exam Instructor: Prof Tejada 1
Problem 1. Debug this code Given the following code to increase the value of a variable: void Increment(int x) { x ++ ; int main() { int num1 = 5; cout << Before = << num1 << endl; Increment(num1); cout << After = << num1 << endl; The Increment function needs to increase the value of num1 in main, but currently the code prints out the same value for num1 after the call to Increment(num1). (a) Show how you would modify the code using pass by reference so that the value of num1 is increased after the call to Increment(num1) (you don't have to rewrite all the code, just rewrite the lines that need to change or the lines you want to add). (b) Show how you would modify the code using pass by pointer so that the value of num1 is changed after the call to Increment(num1) (you don't have to rewrite all the code, just rewrite the lines that need to change or the lines you want to add). 2
Given this function for calculating the nth Fibonacci number using iteration. Rewrite this function using recursion: int Fibonacci(int nthfibonacci) { int previous1=1; int previous2=1; int current; if (nthfibonacci == 0 nthfibonacci == 1 ) current = nthfibonacci; else { for (int counter = 2; counter <= nthfibonacci; counter++) { current = previous2 + previous1; previous1 = previous2; previous2 = current; counter++; //end for //end else return current; Rewrite this function using recursion: int min_value(int arr[], int size) { int min = arr[0]; for(int i = 1; i < size; i++) { if (arr[i] < min) { min = arr[i]; return min; 3
Problem 2 Short Answers What does the keyword virtual mean in C++? Why use recursion? Why iteration? What is Big O? Why is Big O important? What is the difference between composition and inheritance? Why use the reserved words new or delete? Why use dynamic memory allocation? When would you use pointers over reference variables? Why? What is the difference between public, protected, and private? Define the Composition relationship between classes. Give an example of this type of relationship and then draw a diagram: Define the Inheritance relationship between classes. Give an example of this type of relationship and then draw a diagram: 4
Problem 3. What does this code do? 1) What does this code fragment print out? int x = 10, Sum = 0 ; while ( x!= 0 ){ Sum += x--; cout << --x << Sum; 2) What does this code fragment print out? vector<int> my_vec(5); for(int i=0; i < 5; i++) { my_vec[i] = i+5; my_vec.push_back(9); my_vec.push_back(8); my_vec[0] = 3; int i; for(i=0; i < my_vec.size(); i++){ cout << my_vec[i] << ; 5
3) For the code fragment below assume the user input is: 99 34 56 60 77 What is the contents of output.txt file? int x; double average=0; ofstream ofile; ofile.open( output.txt, ios::out); for (int i=0; i<5; i++) { cin >> x; average +=x; ofile << Student << i << : score << x << endl; cout << Average: << average/5 << endl; ofile.close(); What is the output of the following C++ code? int x; int y; int *p = &x; int *q = &y; *p = 35; *q = 98; *p = *q; cout<<x<<" "<<y<<endl; cout<<*p<<" "<<*q<<endl; 6
What is the output of the following C++ code? int x; int y; int *p = &x; int *q = &y; x = 35; y = 46; p = q; *p = 78; cout<<x<<" "<<y<<endl; cout<<*p<<" "<<*q<<endl; 7
Problem 4 Classes & Dynamic memory allocation Given this class definition: class Student { char name[26]; double gpa; int sid; char grade; ; Write a function called Enrollment that asks the user for the number of students enrolled in a course, then creates an array of Student using dynamic memory allocation. The function asks the user to enter the information for each Student object in the array. The function returns the array, as well as the average gpa of all the students and the maximum grade. 8
Given the following class definition: class circletype { public: void setradius(double r); //Function to set the radius. //Postcondition: if (r >= 0) radius = r; // otherwise radius = 0; double getradius(); //Function to return the radius. //Postcondition: The value of radius is returned. double area(); //Function to return the area of a circle. //Postcondition: Area is calculated and returned. double circumference(); //Function to return the circumference of a circle. //Postcondition: Circumference is calculated and returned. circletype(double r = 0); //Constructor with a default parameter. //Radius is set according to the parameter. //The default value of the radius is 0.0; //Postcondition: radius = r; private: double radius; ; Write all of the member functions for the circletype class: 9
What does this print out? circletype circle1(8); circletype circle2; cout << "radius: " << circle1.getradius() << ", area: " << circle1.area() << ", circumference: " << circle1.circumference() << endl; circle2.setradius(4); cout << "radius: " << circle2.getradius() << ", area: " << circle2.area() << ", circumference: " << circle2.circumference() << endl; 10
You are given the following class definitions: class Shape { protected: int x; int y; public: Shape(int a, int b) { x=a; y=b; int getx() const { return x; int gety() const { return y; void setx(int a) { x=a; void sety(int b){ y=b; void print() const { cout << I am an instance of Shape! << endl; ; class Square : public Shape { protected: int side; public: Square(int a, int b, int s); double getside() const { return side; void setside(int s) { side = s; void print(); ; Write the implementation of the provided constructor for the Square class. Write the implementation of the provided void print function for the Square class, so that the function will print out I am an instance of Square!. 11
What will the following code print? Shape s(100,100); s.print(); Square sq(20,40,5); sq.print(); Shape q = sq; q.print(); 12