Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I would really appreciate if some help me out with this C++ assignment. OBJECTIV

ID: 3912250 • Letter: I

Question

I would really appreciate if some help me out with this C++ assignment.

OBJECTIVES:

-Review user-defined function: how to create the function, where to place the function, how to create a prototype of a function, where to place the user-defined function

-Learn how to use the reference parameters and value-returning functions

-Review function with default parameters. Understand how default parameter works.

Question1:

What is the output of the following program?

#include <istream>

using namespace std;

//prototype of function find

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

int main ()

{ int a, b, c;

a = 2;

b = 1;

c = 3;

//call function find

find (a, b, c);

cout << "first call: " << a << ", " << b << ", " << c << endl;

//call function find

find (a, b, c);

cout << "second call: " << a << ", " << b << ", " << c << endl;

//call function find

find (a, b, c);

cout << "third call: " << a << ", " << b << ", " << c << endl;

system("pass");

return 0; }

//User defined function with reference parameters

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

{

a = b + c;

b = a + 1;

c = b + 2;

}

Question2:

Consider the following function::

//function with default parameter

#include <iostream>

#include <sring>

using namespace std;

//function with default parameter

void functionDefaultParameter(char c = '#', double x = 3.5, int y = 1 )

{

cout << "c = " << c << " x = " << x << " y = " << y << endl;

}

a. Which of the following function calls is correct?

b. If it is correct, what is the output?

int main()

{

//1.

functionDefaultParameter();

2//.

functionDefaultParameter('A', 8.2);

3//.

functionDefaultParameter('B', 6.5, 13);

4//.

functionDefaultParameter('C');

5//.

functionDefaultParameter("H");

system("pause");

return 0;

}

Explanation / Answer

In C++ the execution of the program begins from the main function and we need to declare any other funcion before it used other wise the compiler may raise a error regarding the same.

A function in C++ can be created by ths

syntax: Function defenition

return_type function_name(return_type parameter1,return_type parameter2,return_type parameter3,...)

{

//Funtion body

}

eg)

int add(int a,int b)

{

return (a+b);

}

Function Declaration

return_type function_name(return_type parameter1,return_type parameter2,return_type parameter3,...);

int fun(int a,int b);

In function declaration we don't give the function defention we just give the prototype of fuction which contain function retun type , function name, function parameter list.

You can split up the function defention and function declaration but the function declaration need to done before invoking the function anywhere in program.And the function defenition can be done in anywhere of the program if you declare the function before invoking the function.

eg)

void fun(); //fun() declaration

int main()

{

fun();

}

void fun() //fun() defenition

{

cout<<"hai";

}

You can also skip the declaration procedure by only defininig the function before it invoked in the program.

eg)

void fun() // fun() declartion

{

cout<<"hai";

}

int main()

{

fun();

}

Question1

#include <iostream> // the istream chaneged to iostream because you using cin

using namespace std;

//prototype of function find

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

int main ()

{

int a, b, c;

a = 2;

b = 1;

c = 3;

//call function find

find (a, b, c);

cout << "first call: " << a << ", " << b << ", " << c << endl;

//call function find

find (a, b, c);

cout << "second call: " << a << ", " << b << ", " << c << endl;

//call function find

find (a, b, c);

cout << "third call: " << a << ", " << b << ", " << c << endl;

system("pass");

return 0; }

//User defined function with reference parameters

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

{

a = b + c;

b = a + 1;

c = b + 2;

}

The output of the above code is:

first call: 2, 5, 7   

second call: 2, 13, 15                                                                                                                                 

third call: 2, 29, 31                                                                                                                                  

sh: 1: pass: not found

Since your function signature is like void find (int a, int& b, int &c) where variable a is only passing a copy of its valu but the vairables b and c are passing the address of them so any modification of them affect the actual value.

Question2:

#include <iostream>

#include <string.h> // changed to string.h from sring.h

using namespace std;

//function with default parameter

void functionDefaultParameter(char c = '#', double x = 3.5, int y = 1 )

{

cout << "c = " << c << " x = " << x << " y = " << y << endl;

}

int main()

{

// reomoved the syntax errors

//1.

functionDefaultParameter();

//.2

functionDefaultParameter('A', 8.2);

//.3

functionDefaultParameter('B', 6.5, 13);

//.4

functionDefaultParameter('C');

//.5

functionDefaultParameter("H"); //it will raise an error

system("pause");

return 0;

}

a. Which of the following function calls is correct?

The first four function calls are correct.The fifth one is not correct because we are trying to pass a string constant to the function which is not defined in function parameter list.

b. If it is correct, what is the output?

output is:

c = # x = 3.5 y = 1                                                                                                                                  

c = A x = 8.2 y = 1                                                                                                                                  

c = B x = 6.5 y = 13                                                                                                                                 

c = C x = 3.5 y = 1                                                                                                                                  

sh: 1: pause: not found

The default value is automatically assign by the compiler if caller of the function doesn’t provide a value for the argument with default value.