程序代写代做代考 c++ C compiler CSci 1113 Final

CSci 1113 Final
Name:
Student ID:
Instructions: Please pick and answer any 10 of the 12 problems for a total of 100 points. If you answer more than 10 problems, only the first 10 will be graded. The time limit is 120 minutes. Please write your answers in the space provided. The exam is open book and notes. You may use electronic devices to ONLY look at either an e-book version or electronic notes. You may not use the internet, compiler or any other outside resources. (If you are typing on your keyboard/input device for anything other than ctrl-F to find words in the e-book or notes, this is probably not acceptable.)
Problem (1) [10 points]
Write (in C++) a default constructor for a class called “IDsystem”, along with the dec- laration of the class. This class should contain a single integer called “id” that you want to start at zero.
1

Problem (2) [10 points]
(Similar to previous problem) Write (in C++) a function called “nextID()” for the class “IDsystem” that will do two things: (1) return the current value of the variable “id” (a member variable in the class) and (2) increase the “id” variable by one. You may assume “id” is set to zero before you run the function the first time. Declare and define the function “nextID()” to have this functionality, so the sample main() below should output “0 1 2 3 4 5 ”.
int main() {
IDsystem x;
cout << x.nextID() << " " << x.nextID() << " " << x.nextID() << " "; cout << x.nextID() << " " << x.nextID() << " " << x.nextID() << " "; } 2 Problem (3) [10 points] Write (in C++) the best parent class for the following “Cat” and “Dog” classes. class Dog : public Pet { private: string owner; public: double estimateLifespan() { return 10; } string getCollar() { return name + " owned by " + owner; } }; class Cat : public Pet { double estimateLifespan() { return 17; } }; 3 Problem (4) [10 points] What is the output of the following code? int ** a; int *b; int c = 3; b = &c; a = new int*[5]; for(int i=0; i < 5; i++) { a[i] = new int; *a[i]=*b+i; b=a[i]; } b = new int; a[1] = &c; a[2] = &c; for(int i=0; i < 5; i++) { cout << *a[i] << " "; } cout << endl << "b = " << *b << ", c = " << c << endl; 4 Problem (5) [10 points] (Similar to previous problem) Write C++ delete statements after the following lines of code to delete as much memory as possible. Clearly indicate any “new”s in the code that you cannot delete by only adding delete statements after (and not in the middle). int ** a; int *b; int c = 3; b = &c; a = new int*[5]; for(int i=0; i < 5; i++) { a[i] = new int; *a[i]=*b+i; b=a[i]; } b = new int; a[1] = &c; a[2] = &c; 5 Problem (6) [10 points] Write a destructor for the following class. class imaclass { private: string** a; string* b; string c; public: imaclass(); }; imaclass::imaclass() { c = "meow"; a = new string*; *a = &c; b = new string[20]; } 6 Problem (7) [10 points] Write an equals operator for the following class. class data { private: int* airy; int size; public: data(int s); }; data::data(int s) { size = s; airy = new int[size]; for(int i=0; i < size; i++) { airy[i] = 0; } } 7 Problem (8) [10 points] Find 3 errors in the code below. Assume that the code is completely shown except for #includes and “using namespace std”. For each error, identify whether it is a runtime error, syntax error or logic error. You must also precisely describe why you think the part of code you identify is an error. class parent { private: int x; }; class child { void foo(); }; void foo() { cout << x; } int main() { child c; c.foo(); } 8 Problem (9) [10 points] What is the output of this code. Show work for full (and partial) credit. void stoof(char ding[], int i) { if(ding[i] == ’\0’) { cout << "6"; return; } if(ding[i+1] == ’\0’) { cout << "a"; return; } stoof(ding, i+2); stoof(ding, i+1); } int main() { stoof("sumthin", 3); } 9 Problem (10) [10 points] Write a function (in C++) that takes two inputs: (1) an array of strings and (2) an integer for the size of the array. Return the number of strings in the array that contain either of the letters ’q’ or ’z’. 10 Problem (11) [10 points] Assume you have a function “lastDay(int month, int year)” that tells you how many days are in a month (just like lab). Write code (in C++) that cins two dates in the format “1/2/1999 11/23/1999” where the first date is the start date and the second is the end date. You should cout how many days are between these two dates (so 1/31/2000 2/1/2000 should say just “1” day between). You may assume that the year will always be the same in the start and end date. You may also assume that the months will be different between the start and end date. 11 Problem (12) [10 points] Assume variables “a”, “b” and “c” are already declared and initialized. Write (C++) code that finds the root of the polynomial: a · x2 + b · x + c using the quadratic equation: √ −b± b2−4·a·c. If no real roots exist, simply state this instead. 2·a 12