View the step-by-step solution to:

Question

# CISC1600 Midterm Review1. Translate the following into C++

code, using predefined functions in the C++ library.

(Ex: ???????????????????????????????? = √(????2 − ????1)2 + (????2 − ????1)2  distance = sqrt(pow((x2-x1),2)+ pow((y2-y1),2))

a. ???? =

????(????−????)

????

b. ???? = ????????2

c. ???? =

−????−√????2−4????????

2????

2. Explain the difference between call-by-value and call-by reference, and how it affects the swap function

reproduced below. What would happen if one of the ampersands (&) were missing in the function parameters?

Both?

void swap(int &a, int &b)

{

int temp;

temp = a;

a = b;

b = temp;

}

3. Explain the difference between = and ==, and give an example of when each should be used.

Which one should you use in the underlined part of the following code to prevent an infinite loop?

int main()

{

int choice = ‘Y’;

while(choice __ ‘Y’)

{

cout << “knock knock ”;

cout << “Proceed? (Y/N)”;

cin << choice;

}

cout << “Who’s there? n”;

}

4. What do the following pieces of code output?

a. number = 3;

if(number < 5)

cout << “But soft, what light through yonder window breaks? n”;

if(number < 4)

cout << “‘Tis the east, and Juliet is the sun. n”;

if(number == 3)

cout << “Arise, fair sun, and kill the envious moon, n”;

if (number > 2)

cout << “Who is already sick and pale with grief n”;

else

cout << “That thou, her maid, art far more fair than she. n”;

b. choice = ‘c’;

if (choice == ‘a’)

cout << “O Romeo, Romeo, wherefore art thou Romeo? n”;

else if (choice == ‘b’)

cout << “Deny thy father and refuse thy name; n”;

else if (choice == ‘d’)

cout << “Or if thou wilt not, be but sworn my love n”;

else

cout << “And I’ll no longer be a Capulet. n”;

c. number = 5;

if (number = 8)

cout << “We know what we are, but know not what we may be n”

else

cout << “Better a witty fool than a foolish wit n”;

d. number = 1;

if (number > 0)

if (number > 3)

cout << “O thou invisible spirt of wine, ”

<< “if thou hast no name to be known by, ”

<< “let us call thee devil. n”;

else

cout << “The lady doth protest too much, methinks. n”;

5. What do the following loops output?

a. i = 0;

while (i < 10){

cout << i;

i++;

}

b. i = 5;

while (i >= 0){

i--;

cout << i;

}

6. What is the output of the following program?

#include <iostream>

using namespace std;

void sum(int a, int b, int &c);

int main()

{

int num1, num2, num3;

num1 = 2;

num2 = 2;

num3 = 0;

sum(num1, num2, num3);

cout << num1 << ‘+’ << num2 << ‘=’ << num3;

return 0;

}

void sum(int a, int b, int &c)

{

a = 4;

b = 3;

c = a + b;

}

CISC1600 Midterm Review 1. Translate the following into C++ code, using predefined functions in the C++ library. (Ex: ?°?±²³´? = √(µ 2 − µ 1 ) 2 + (¶ 2 − ¶ 1 ) 2 distance = sqrt(pow((x2-x1),2)+ pow((y2-y1),2)) a. ² = ·(¸¹?) ? b. º = »´ 2 c. µ = ¹·¹¼· ½ ¹4¾¸ 2. Explain the difference between call-by-value and call-by reference, and how it affects the swap function reproduced below. What would happen if one of the ampersands (&) were missing in the function parameters? Both? void swap(int &a, int &b) { int temp; temp = a; a = b; b = temp; } 3. Explain the difference between = and ==, and give an example of when each should be used. Which one should you use in the underlined part of the following code to prevent an infinite loop? int main() { int choice = Y ; while(choice __ Y ) { cout << knock knock ; cout << Proceed? (Y/N) ; cin << choice; } cout << Who s there? \n ; }
4. What do the following pieces of code output? a. number = 3; if(number < 5) cout << But soft, what light through yonder window breaks? \n ; if(number < 4) cout << “‘ Tis the east, and Juliet is the sun. \n ; if(number == 3) cout << Arise, fair sun, and kill the envious moon, \n ; if (number > 2) cout << Who is already sick and pale with grief \n ; else cout << That thou, her maid, art far more fair than she. \n ; b. choice = c ; if (choice == a ) cout << O Romeo, Romeo, wherefore art thou Romeo? \n ; else if (choice == b ) cout << Deny thy father and refuse thy name; \n ; else if (choice == d ) cout << Or if thou wilt not, be but sworn my love \n ; else cout << And I ll no longer be a Capulet. \n ; c. number = 5; if (number = 8) cout << We know what we are, but know not what we may be \n else cout << Better a witty fool than a foolish wit \n ; d. number = 1; if (number > 0) if (number > 3) cout << O thou invisible spirt of wine, << if thou hast no name to be known by, << let us call thee devil. \n ; else cout << The lady doth protest too much, methinks. \n ; 5. What do the following loops output? a. i = 0; while (i < 10){ cout << i; i++; } b. i = 5; while (i >= 0){ i--; cout << i; }
Show entire document

View the full answer

1. 2.In call by value a copy of the actual arguments are passed to formal arguments of the called function
and any change made to formal arguments in the called function will not affect the...

### Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

### -

Educational Resources
• ### -

Study Documents

Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

Browse Documents