FPU-S18-COP2272C-Exam2Practice1AK.pdf

FPU-S18-COP2272C-Exam2Practice1AK.pdf - Use PDF Bookmark to...

Info icon This preview shows pages 1–6. Sign up to view the full content.

Use PDF Bookmark to ease navigation in this file FPU Spring 2018 Answer Keys for COP2272C Exam Two Practice I Short Answer Questions 1. Give the output of the following program segment. int f1(int& x, int y); int main() { int a = 3, b = 5; int c; cout << a << “ “ << b << endl ; c = f1 (a, b); cout << a << “ “ << b << “ “ << c << endl ; c = f1(a, b + f1(b, 4)); cout << a << “ “ << b << “ “ << c << endl ; return 0; } int f1(int& x, int y) { x = x * y x * x; y = x + 1; if (x > y) return x; else return y; } Answer: 3 5 6 5 7 -90 -5 -89 2. Write a void function that first check sizes of two double element arrays and swap the elements of these two arrays if their sizes are equal. Make sure the indexes of elements remain same after the swap. Answer: void swap(double x[], double y[]) { size_t sx, sy; sx = sizeof(x)/sizeof(x[0]); sy = sizeof(y)/sizeof(y[0]); if (sx != sy) { cout << "Error: array x and array y have different size." << endl; return; } for (int i = 0; i < sx; i++) { double tmp = x[i]; x[i] = y[i]; y[i] = tmp; } }
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

2/9 3. Give the output of the following program fragments. 1) int number[5]; int i; for (i=0; i<5; i++) { number[i] = 5 i; cout << number[i] << “ “; } Answer: 5 4 3 2 1 2) int number[5]; int i; for (i=0; i<5; i++) { number[i] = i*2 + 1; } for (i=4; i>0; i--) { number[i] = number[i-1]; cout << number[i] << “ “; } Answer: 7 5 3 1 4. Give the output of the following program segment. int f(int x, int& y); int main() { int a = 3, b = 5; f(a, b); cout << a << “ “ << b << endl ; a = f(f(f(2, a), b), a); cout << a << “ “ << b << endl ; return 0; } int f(int x, int& y) { int v = 2; x = (x v) * (x + v); if (x < 5) y = 1; else y = x - 4; return y; }
Image of page 2
3/9
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

4/9 5. Give the output of the following program fragments. int number[4]; for (int i=0; i<4; i++) number[i] = i*2; for (int j=2; j>=0; j--) { number[j+1] = number[j]; cout << number[j+1] << “ “; }
Image of page 4
5/9 Part II Long Answer Questions 1. Function Passing by Reference Write the following function that tests whether the array values has four consecutive numbers with the same value.
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern