Qs 3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

ASSIGNMENT 3

Qs 1.Write two versions of a function ‘sum‘ that sums its two integer
parameters int sum( int x int y) and void sum(int x ,,int y) Call thse functions
in the main and display the output.What is the difference In calling of these
two functions in the main.

Answer:

#include<iostream>

using namespace std;

int sum(int x, int y)

return x+y;

void sum(int x,float y)

cout<<”Sum is:”<<x+y<<endl;

int main()

cout<<”Sum from main:"<<sum(1,2)<<endl;

sum(2,3);
return 0;

Output:

Sum from main:3

Sum is:5

Qs 2.What is difference between passing by value and passing by reference.

Answer: In call by value a new copy of variables is made , any changes in


function variable are not reflected in real variable.Whereas in call by
reference a new copy of variable is not formed original variables are passed
so changes in variables is reflected in original variables.

Qs 3. Write two version of a function that multiply the next higher values of
its two integer parameters int prod(int x, int y) ( parameters passed by value ,
result returned after changing x to x+1 and y to y+1 within the function) and
int prod(int &x,int &y)(parameters passed by reference carrying out the same
multiplicatiojn as earlier).Call these function in th meain and disoplay the
output,Demonstarte the difference in the vlue of parmeters after calling of
these two function in the main.

Answer

#include <iostream>

int prod(int x , int y)

x=x+1;
y=y+1;

return x*y;

int prod1(int&x, int&y)


{

x=x+1;

y=y+1;

return x*y;

int main()

int x=1,y=2;

cout<<”Calling by value:”<<prod(x,y);

cout<<endl<<”Values after call by value: x=”<<x<<” y=”<<y<<endl;

cout<<”Calling by reference:”<<prod1(x,y);

cout<<endl<<”Values after call by reference: x=”<<x<<” y=”<<y<<endl;

return 0;

Output

Calling by value:2
Values after call by value: x=1 y=2

Calling by reference:2

Values after call by reference: x=2 y=3

Qs 4.Write funcrtion int rectangle_area(int length,int height),float


reactangel_area(float length, float height), void rectangle_area(float
length,int height).The task of these function is to calculate the area of the
rectangle.Write main to execute these function and disoplay the output.

Answer

#include<iostream>

using namespace std;

int rectangle_area(int length,int height)

return length*height;

float rectangle_area(float length,float height)

return length*height;

void rectangle_area(float length,int height)

{
cout<<”Area by void rectangle_

=”<< length*height<<endl;

int main()

cout<<”Area by int rectangle_area=”<<rectangle_area(10,1)<<endl;

cout<<”Area by float rectangle_area=”


<<rectangle_area(10.0,1.0)<<endl;

rectangle_area(10.0,1);

return 0;

Output:

Area by int rectangle_area=10

Area by float rectangle_area=10.0

Area by void rectangle_area=10.0

Qs 5. Write a function DotProduct which takes four paramerter of float type


and copmputes the dot product of two two_dimensionjal vectors and return
the dot product as float type the parameters are x1,y1, x2,y2.Write another
function with the same name which takes six parameters of float type and
computes the dot product of two three-dimensional vectors and return the
dotprodcut as float type. The parameter are x1,y1,z1,x2,y2,z2.

Answer:
#include<iostream>

using namespace std;

float DotProduct(float x1,float y1,float x2,float y2)

return x1*x2+y1*y2;

float DotProduct(float x1,float y1,float z1,float x2,float y2,float z2)

return x1*x2+y1*y2+z1*z2;

Qs 6. What are advantages of using inline function.Explain using suitable


example.

Answer

Declaring a function as inline results in a the replacement of the function call


by the function itself.If the function is small this results in the saving of
function calling overhead because the computer does not have to jump
instruction from main to subprogram.

Example:

#include<iostream>

using namespace std;

inline int add(int x,int y)


{

return x+y;

int main()

for (int i=0;i<100000;i++)

add(x,y);

return 0;

Had inline not been used , this would have resulted in lot of jumping from
main to add , but after the addition of inline this problem has been solved as
the jumpes would not be required during function call.

Qs 7. Write inline functions square_ and cube_ which take int as parameters.
These functions return square and cube of integer respectively.Also write
function square_of_integer and cube_of_integer which take integer as
parameters and return square and cube of integer to main
respectively.Understand how these functions are called in the main.

Answer

#include<iostream>

using namespace std;

inline int square_(int x)


{

return x*x;

inline int cube_(int x)

return x*x*x;

int square_of_integer(int x)

return x*x;

int cube_of_integer(int x)

return x*x*x;

int main()

cout<<square_(1)<<endl;

cout<<cube_(2)<<endl;
cout<<square_of_integer(2)<<endl;

cout<<cube_of_integer(3)<<endl;

return 0;

Output

27

Qs 8. Wrte down the rules for the default parameters in a function.Write an


example program the explains it in detail.

Answer:

The rules are as follows:

1. All variables right to the first variable having default value must have
default values.

Example: int fun(int a=10,int b,int c=10); //ERROR : NOT ALLOWED

2.Overloading functions that have default values is not allowed if ambiguity


can arise when calling them.

Example: int fun(char c);


int fun(char c, char t=’a’);//ERROR: NOT ALLOWED
//because if function call is made as fun(‘a’) compiler cannot identify the
//function to call

3.Function having forward declaration can be provided default value in either


declaration of definition but not both.

Example: int fun(int c=10);

int func(int c=10) //ERROR not allowed

return c*c;

Qs 9. Write a function int rectangle_area with default parameters to


compute the area of the rectangle having length and height equal to
one.How this function can be called in the man to computer the area of the
rectangle having (i) length=1 and width=1 and (ii) length=10 and width=5.

Answer

#include<iostream>

using namespace std;

int rectangle_area(int l=1,int b=1)

return l*b;

}
int main()

cout<<rectangle_area();

cout<<endl<<rectangle_area(10,5);

return 0;

Question 10.Write recursive function gcd which takes two integer


parameters. The task of this function is to find the greatest common divisor
of two integers. Write a driver to test this function.

Answer:

#include<iostream>

using namespace std;

int gcd(int a,intb)

if(b==0)

return a;

return gcd(b,a%b);

int main()

{
cout<<”GCD of 15 and 20 is ”<<gcd(15,20)<<endl;

return 0;

Output:

GCD of 15 and 20 is 5

Question 11.Write a recursive function factorial that takes an integer


parameter and returns the factorial of the integer. It also gives a warning if
the given integer is negative. The main should display the values of factorial
of 0 to 5 in the following tabular format. leaving 10 spaces in the beginning of
each line:

0! = 0

l! = I

Answer:

#include<iostream>

using namespace std;

int factorial(int n)

if (n<0)

cout<<”Negative no entered"<<endl;
return -1;

if(n==0)

return 1;

return n*factorial(n-1);

int main()

for(int i=0;i<6;i++)

cout<<string(‘ ‘,10)<<i<<”!=”<<factorial(i)<<endl;

return 0;

Output:

0!=1

1!=1

2!=2

3!=6

4!=24

5!=120
Question 12.Write a function ‘sumDiff' which either adds two integers and
returns the sum or subtract: the two integers and returns the difference. The
function takes a bool parameter to decide action of summing or subtracting.
Write main to test the function.

Answer:

#include<iostream>

using namespace std;

int sumDiff(int a,int b, bool t)

if(t==1)

return a+b;

return a-b;

int main()

cout<<”Sum of 5 and 9 is “<<sumDiff(5,9)<<endl;

return 0;

Output

Sum of 5 and 9 is 14

You might also like