CIS210 Fall 2016 Homework Points 20 Due Date 10282016 beginn

CIS-210 (Fall 2016) Homework Points: 20 Due Date: 10/28/2016 (beginning of class) 1. Problems from textbook (Chapter 6 Exercises) Problems: 8, 16, 20, 28 .

Solution

Answers for each of the question with expalanation.

8.
a. cout<<find(15, 25)<<endl;


Executing the program we get,
   first = 15 * 25 = 375
   second = 375 - 15 + 25 = 385
   if(375 > 50) -> executes to true
       num1 = 375/10 = 37
   else -> skipped
   return 37 + 25 = 62

   OUTPUT: 62

b. cout<<discover(3, 9)<<endl;

Executing the program we get,
   secret = 1;
   for i = 3, secret = 1 * 3 = 3; execution stops when i == 9, i++
   for i = 4, secret = 3 * 4 = 12; execution stops when i == 9, i++
   for i = 5, secret = 12 * 5 = 60; execution stops when i == 9, i++
   for i = 6, secret = 60 * 6 = 360; execution stops when i == 9, i++
   for i = 7, secret = 360 * 7 = 2520; execution stops when i == 9, i++
   for i = 8, secret = 2520 * 8 = 20160; execution stops when i == 9. i++
   for i = 9 , execution stops.

   return 20160

   OUTPUT: 20160

c. cout<<find(10, 10) <<\" \"<<discover(10, find(10,10))<<endl;

In the same way mentioned in 8 a) the find function find(10,10) is executed.
The result would be,

   first = 100
   second = 100
   num1 = 10
   return 20

Execution of discover function will be similar to 8 b) but the second oarameter would be the result of find(10,10) which would be 20(as per above execution).

discover(10,10) =>

secret = 1
for i = 10, i<10 ..... here i is not less than 10.
Hence condition fails.
for loop will not be exceuted.

return 1 (which is the initial value of secret)

OUTPUT: 20 1

d. x = 20; y = 10;
cout<<disocver(x, y)<<endl;

Execution of discover function starts..

secret = 1;
for i = 20, i<10 ... i is not less than 10..
for loop will not be executed.

return 1;

OUTPUT: 1

16. cout<<mystery(7,8,3)<<endl;

Executing the function,

if(7 <= 8 && 7 <= 3) == > fails because 7 is not less than or equal to 3, however 7 <= 8. But && means both the expressions must be true
else if( 8 <= 3 && 8 <= 7) ==> fails because 8 is not less than 3 or 7
else
return (7+8-3)

cout<<mystery(10, 5, 30)<<endl;

Executing the function,

if(10 <= 5 && 10 <= 30) == > fails because 10 is not less than 5 but is less than 30. But still for && both expressions should be true.
else if( 5 <= 30 && 5 <= 10)
   return (30+10-5)

cout<<mystery(9,12,11)<<endl;

Executing the function,

if(9 <= 12 && 9 <= 11)
   return (12+11-9)

cout<<mystery(5,5,8)<<endl;

Executing the function,

if(5 <= 5 && 5 <= 8)
   return (5+8-5)

cout<<mystery(10,10,10)<<endl;

Executing the function,

if(10 <= 10 && 10 <= 10)
   return (10+10-10)

OUTPUT:
12
35
14
8
10

20.
a. function prototype - Line 5 => void func(int, double&, char);
function heading - Line 17 => provides the heading of the function
function body - Line 18 to Line 26 => the part between the curly brackets
function definition - Line 17 to Line 26 => the function heading and the body together is called the function definition.

b. function call statements - Line 11 and Line 13 where func() is called.
formal parameters - parameters used within a function are called formal parameters. formal Parameters of func() are first, second and ch
actual parameters - the values that are passed to the function are the actual parameters.
Actual parameters to func() at Line 11 are num, one and ch which are not initialized.
At Line 13 => 16, 0ne and %

c. The variables defclared outsid ethe function ar ethe global variables.
Here NUM and temp are global variables.
Local variables in main() are num, one, ch
Local variables in func() are num, y, u, first, second, ch

d. named constants.
Line 3 has the const varaible NUM which is of double type and holds 3.5

28. Executing the program.

intNum1 = 2
intNum2 = 5
x = 6

summer(&a, b) //reference is passed for a. it is poiting ot the memeory location of intNum1
{
   intNum1 = 5 + 12 = 17
   a = 2*5+5 = 15;
   b = 17 + 4 = 21

}

Here in main() intNum1 value is changed because the reference was passed to summer() whereas intNum2 remains the same.

in main() , intNum1 = 17 and intNum2 = 5

fall(u, &v) //reference is passed for second parameter
{
   intNum2 = 6;
   v = 6 * 4 = 24
   x = 17 - 24 = -7
}

OUTPUT:
17 5 6
17 24 -7

 CIS-210 (Fall 2016) Homework Points: 20 Due Date: 10/28/2016 (beginning of class) 1. Problems from textbook (Chapter 6 Exercises) Problems: 8, 16, 20, 28 . Sol
 CIS-210 (Fall 2016) Homework Points: 20 Due Date: 10/28/2016 (beginning of class) 1. Problems from textbook (Chapter 6 Exercises) Problems: 8, 16, 20, 28 . Sol
 CIS-210 (Fall 2016) Homework Points: 20 Due Date: 10/28/2016 (beginning of class) 1. Problems from textbook (Chapter 6 Exercises) Problems: 8, 16, 20, 28 . Sol

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site