Ravi Tutorials Provide Project Training

C++

C++ is a middle-level programming language developed by Bjarne Stroustrup starting in 1979 at Bell Labs. C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.
This reference will take you through simple and practical approach while learning C++ Programming language.

Question and solution chapter wise
1.1: What do you think are the major issues facing the software industry today?

Ans:Many software products are either not finished or not used or not delivered for some major errors. Today some of the quality issues that must be considered for software industry are:
1. Correctness.
2. Maintainability.
3. Reusability.
4. Openness and interoperability.
5. Portability.
6. Security.
7. Integrity.
8. User friendliness.

1.2:Briefly discuss the software evolution during the period 1950— 1990.

Ans:In 1950, the first three modern programming languages whose descendants are still in widespread today.
1. FORTRAN (1955), the “FORmula TRANslator”
2. LISP (1958) the “LISt Procssor”.
3. COBOL, the COmmon Business Oriented Language.
Some important language that were developed in 1950 to 1960 are:
1. 1951 Regional Assembly Language
2. 19952 Autocode
3. 1954 IPL
4. 1955 FLOW-MATIC
5. 1957COMTRAN
6. 1959COBOL
7. 1962APL

1.3: What is procedure-oriented programming? What are its main characteristics?


Ans:Conventional programming, using high level language such as COBOL, FORTAN and C is commonly known as procedure oriented programming.
Characteristics :
a) Emphasis is on doing things (algorithms)
b) Large programs are divided into small programs known as function.
c) Most of the function share global data.
d) Data move openly around the system from function to function.
e) Function transform data from one form to another.

Practice questions chapter wise.

1.4: Discuss an approach to the development of procedure-oriented programs.


Ans: In the procedure-oriented approach, the problem is viewed as a sequence of things to be done such as
1. reading
2. Calculating
3. Printing.
A number of functions are written to accomplish these tasks.

1.5: Describe how data are shared by functions in a procedure-oriented program.
Ans: In a multi-function program, many important data items are placed as global so that they may be accessed by all the functions. Each function may have its own local data.


1.6: What is object-oriented programming? How is it different from the procedure-oriented programming?


Ans: Object oriented programming (OOP)is an approach that provides a way of modularizing programs by creating partitioned memory area for both data and functions that can be used as templates for creation copies of such modules on demand.
Different between OOP(Object oriented programming) & POP(Procedure oriented programming):
1. OOP has data hading feature for which the data of a class cannot be accessed by the member function of other class but POP has no such feature.
2. In OOP we can design our own data-type which is same as built in data type. But in POP we can not do this.

1.7: How are data and functions organized in an object-oriented program?


Ans: Data and functions are belongs to a class. Data is called data member and functions are called member functions. There is a visibility-mode such as public and private. Generally data is private and functions are public.
rq17

1.8: What are the unique advantages of an object-oriented programming paradigm?


Ans: The unique advantage of object-oriented program paradigm is to have a working definition of OOP before we proceed further.

1.9: Distinguish between the following terms:
(a) Objects and classes
(b) Data abstraction and data encapsulation
(c) Inheritance and polymorphism
(d) Dynamic binding and message passing


Ans: (a) Objects are the basic run-time entities which contain data and code to manipulate data where the entire set of data and code of an object can be made as a user-defined data type with the help of a class. In short, objects are numbers of classes.
(b) Describing the functionality of a class independent of its implementation is called data abstraction. Where data encapsulation means the wrapping up of data and functions into a single unit.
(c) The mechanism of deriving a new class from an old one is called inheritance, where polymorphism means one thing with several distinct terms.
(d) Binding refers to the linking of a procedure call to be executed in response to the call. When binding occurs at run-time, then it is known as dynamic-binding.
Massage passing involves specifying the name of the object, the name of the function and the information to be sent

1.10: What kinds of things can become objects in 00P?


Ans:Objects are the basic run-time entities in an object-oriented system. They may represent a person, a place, a bank account, a table of data or any item that the program has to handle.

1.11: Describe inheritance as applied to OOP.


Ans:Inheritance is one of the most powerful feature of object-oriented programming. Inheritance is the process of creating new class from existing class. The new class is called derived class and existing class is called base class.

1.12:What do you mean by dynamic binding? How is it useful in OOP?


Ans:Binding refers to the linking of a procedure call to be executed in response to the call. When binding occurs at run time it is known as dynamic binding. Dynamic binding is useful in OOP such as a function call associated with a polymorphic reference depends on the dynamic type of that reference.

1.13:Now does object-oriented approach differ from object-based approach?


Ans:Object-based programming do not support inheritance and dynamic binding but object-oriented programming do so.

1.14:List a few areas of application of 001″ technology.


Ans:Areas of application of OOP technology are :
1. Real-time system.
2. Simulation and modeling.
3. Object oriented database.
4. Hypertext, hypermedia .
5. Decision support and office automation system.




1.15:State whether the following statements are TRUE or FALSE.
(a) In procedure-oriented programming, all data are shared by all functions.
(b) The main emphasis of procedure-oriented programming is on algorithms rather than on data.
(c) One of the striking features of object-oriented programming is the division of programs into objects that represent real-world entities.
(d) Wrapping up of data of different types into a single unit is known as encapsulation.
(e) One problem with 00P is that once a class is created it can never be changed.
(f) Inheritance means the ability to reuse the data values of one object by
(g) Polymorphism is extensively used in implementing inheritance.
(h) Object oriented programs are executed much faster than conventional programs.
(i) Object-oriented systems can scale up better from small to large.
(j) Object-oriented approach cannot be used to create databases.


Ans:
a> FALSE
b> TRUE
c> TRUE
d> FALSE
e> FALSE
f> TRUE
g> TRUE
h> FALSE
i> TRUE
j> FALSE




2.1: State whether the following statements are TRUE or FALSE.
(a) Since C is a subset of C++, all C peograms will run under C++ compilers.
(b) In C++, a function contained within a class is called a member function.
(c) Looking at one or two lines of code, we can easily recognize whether a program is written in C or C++.
(d) In C++, it is very easy to add new features to the existing structure of an object.
(e) The concept of using one operator for different purposes is known as aerator overloading. 10 The output function printfl) cannot be used in C++ programs.
Ans:
a> FALSE
b> TRUE
c> FALSE
*** most lines of codes are the same in C & C++
d> TRUE
e> TRUE
f> FALSE
2.2: Why do we need the preprocessor directive #include?


Ans:‘#include’ directive causes the preprocessor to add-the contents of iostream file to the program.
2.3: How does a main() function in C++ differ from main{} in C?


Ans: In C main () by default returns the void type but in C++ it returns integer by default.
2.4: What do you think is the main advantage of the comment / / in C++ as compared to the old C type comment?


Ans: ‘//’ is more easy and time-saving than ‘/* */’


2.5:Describe the major parts of a C++ program.


Ans: Major parts of a C++ program :
1. Include files
2. Class declaration
3. Member function definitions
4. Main function program

2.1: State whether the following statements are TRUE or FALSE.
(a) Since C is a subset of C++, all C peograms will run under C++ compilers.
(b) In C++, a function contained within a class is called a member function.
(c) Looking at one or two lines of code, we can easily recognize whether a program is written in C or C++.
(d) In C++, it is very easy to add new features to the existing structure of an object.
(e) The concept of using one operator for different purposes is known as aerator overloading. 10 The output function printfl) cannot be used in C++ programs.
Ans:
a> FALSE
b> TRUE
c> FALSE
*** most lines of codes are the same in C & C++
d> TRUE
e> TRUE
f> FALSE
2.2: Why do we need the preprocessor directive #include?


Ans:‘#include’ directive causes the preprocessor to add-the contents of iostream file to the program.
2.3: How does a main() function in C++ differ from main{} in C?


Ans: In C main () by default returns the void type but in C++ it returns integer by default.
2.4: What do you think is the main advantage of the comment / / in C++ as compared to the old C type comment?


Ans: ‘//’ is more easy and time-saving than ‘/* */’


2.5:Describe the major parts of a C++ program.


Ans: Major parts of a C++ program :
1. Include files
2. Class declaration
3. Member function definitions
4. Main function program

2.1: Write a program to display the following output using a single cout statement
Maths = 90
Physics = 77
Chemistry = 69





Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include
#include
int main()
{
           char *sub[]={"Maths","Physics","Chemestry"};
           int mark[]={90,77,69};
           for(int i=0;i<3 code="" i="">
            {
cout<"="<
             }
    return 0;
}

output

Maths = 90
Physics = 77
Chemistry = 69





2.2: Write a program to read two numbers from the keyboard and display the larger value on the screen.


Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include
#include
int main()
{
    float a,b;
        cout<<" Enter two values  :"<
    cin>>a>>b;
    if(a>b)
        cout<<" larger value = "<
    else
        cout<<" larger value = "<
    return 0;
}

output

Enter two values : 10 20
larger value = 20





2.3:Write a program to input an integer from the keyboard and display on the screen “WELL DONE” that many times.


Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Solution:
#include
#include
int main()
{
    int n;
    char *str;
    str="WELL DONE";
    cout<<" Enter an integer value ";
    cin>>n;
    for(int i=0;i
    {
        cout<
    }
    return 0;
}

output

Enter an integer value 5
WELL DONE
WELL DONE
WELL DONE
WELL DONE
WELL DONE


2.4: Write a program to read the values a, b and c and display x, where
x = a / b –c.

Test the program for the following values:
(a) a = 250, b = 85, c = 25
(b) a = 300, b = 70, c = 70


Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include
#include
int main()
{
     float a,b,c,x;
     cout<<" Enter the value of a,b, &c :"<
     cin>>a>>b>>c;
     if((b-c)!=0)
     {
            x=a/(b-c);
            cout<<" x=a/(b-c) = "<
     }
     else
     {
         cout<<"  x= infinity "<
     }
     return 0;
}

During First Run:

output

Enter the value of a,b, &c : 250 85 25
x=a/(b-c) = 4.166667

During Second Run:

output

Enter the value of a,b, &c : 300 70 70
x= infinity





2.5: Write a C++ program that will ask for a temperature in Fahrenheit and display it in Celsius


Solution:
1
2
3
4
5
6
7
8
9
10
11
12
#include
#include
int main()
{
    float f,theta;
    cout<<" Enter  the temperature in Feranhite    scale  : ";
    cin>>f;
    theta=((f-32)/9)*5;
    cout<<" Temperature in Celsius =  "<
    return 0;
}

output

Enter the temperature in Feranhite scale : 105
Temperature in Celsius = 40.555557





2.6:Redo Exercise 2.5 using a class called temp and member functions.


Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include
#include
class temp
{
    float f,theta;
public:
    float conversion(float f);
};
float temp::conversion(float f)
{
    theta=((f-32)/9)*5;
    return theta;
}
int main()
{
    temp t;
    float f;
    cout<<" Enter temperature in Farenheite scale :"<
    cin>>f;
    cout<<" Temperature in Celsius scale = "<
    return 0;
}

output

Enter the temperature in Feranhite scale : 112
Temperature in Celsius = 44.444443





2.1: Identify the error in the following program.
1
2
3
4
5
6
7
8
9
#include
void main()
{
    int i = 0;
    i = i + 1;
    cout « i « " ";
    /*comment \*//i = i + 1;
    cout << i;
}
Ans: /* comment\*//i=i+1;  Syntax error


2.2:Identify the error in the following program.
1
2
3
4
5
6
#include
void main()
{
     short i=2500, j=3000;
     cour>> "i+j=">> -(i+j);
}


Ans:cout >> “i+j=”>> -(i + j);  Illegal structure operation.


2.3:What will happen when you run the following program?
1
2
3
4
5
6
7
8
9
10
11
#include
void main()
{
     int i=10, j=5;
     int modResult=0;
     int divResult=0;
     modResult = i%j;
     cout<" ";
     divResult = i/modResult;
     cout<
}


Ans:divResult = i/modResult;  floating point Error or divide by zero


Note: If this kind of Error exist in a program, the program will successfully compile but it will show Run Error.




3.1:Enumerate the rules of naming variables in C++. How do they differ from ANSI C rules?


Ans: Rules of naming variables in C++ are given below :
a. Any character from ‘a’ to ‘z’ or ‘A’ to ‘Z’ can be used.
b. Digit can be used but not at the beginning.
c. Underscore can be used but space is not permitted.
d. A keyword cannot be used as variable name.
In C++, a variable can be declared any where in the program but before the using of the variable.
In C all variables must be declared at the beginning of the program.


3.2:An unsigned int can be twice as large as the signed int. Explain how?


Ans:
In case of unsigned int the range of the input value is : 0 to 2m – 1. [where m is no. of bit]
In case of signed int the range of the input value is : -2m1 to + (2m1 – 1)
reviwe ques
So, maximum value for unsigned int can be twice as large as the signed int.
* Here the absolute value of lower value -2m1 for signed int must be considered for finding average value of signed int.


3.3:Why does C++ have type modifiers?


Ans: To serve the needs of various situation.


3.4:What are the applications of void data type in C++?


Ans: Two normal uses of void are
(1) to specify the return type of a function when it is not returning any value.
(2) To indicate any empty argument list to a function.
Example : void function (void)
Another interesting use of void is in the declaration of generic pointers.
Example :
void *gp; //gp is generic pointer.
A pointer value of any basic data type can be assigned to a generic pointer
int * ip;
gp = ip; // valid.


3.5:Can we assign a void pointer to an int type pointer? If not, why? Now can we achieve this?


Ans:We cannot assign a void pointer to an int type pointer directly. Because to assign a pointer to another pointer data type must be matched. We can achieve this using casting.
Example :
void * gp;
int *ip;
ip = (int * ) gp
/br<>
3.6:Describe, with examples, the uses of enumeration data types.


Ans:An enumerated data type is a user-defined type. It provides a way for attaching names to numbers in ANSIC
Example :
enum kuet (EEE, CSE, ECE, CE, ME, IEM);
The enum keyword automatically enumerates
EEE to 0
CSE to 1
ECE to 2
CE to 3
ME to 4
IEM to 5
In C++ each enumerated data type retains its own separate type.


3.7:Describe the differences in the implementation of enum data type in ANSI C and C++.


Ans:Consider the following example :
enum kuet (EEE, CSE, ECE, CE, ME, IEM);
Here, kuet is tag name.
In C++ tag name become new type name. We can declare a new variables Example:
kuet student;
ANSI C defines the types of enum to be int.
In C int value can be automatically converted to on enum value. But in C++ this is not permitted.
Example:
student cgp = 3.01 // Error in C++
// OK in C.
student cgp = (student) 3.01 //OK in C++





3.8:Why is an army called a derived data type?


Ans:Derived data types are the data types which are derived from the fundamental data types. Arrays refer to a list of finite number of same data types. The data can be accessed by an index number from o to n. Hence an array is derived from the basic date type, so array is called derived data type.


3.9:The size of a char array that is declared to store a string should be one larger than the number of characters in the string. Why?


Ans:An additional null character must assign at the end of the string that’s why the size of char array that is declared to store a string should be one larger than the number of characters in the string.


3.10:The const was taken from C++ and incorporated in ANSI C, although quite differently. Explain.


Ans:In both C and C++, any value declared as const cannot be modified by the program in any way. However there are some differences in implementation. In C++ we can use const in a constant expression, such as const int size = 10; char name [size];
This would be illegal in C. If we use const modifier alone, it defaults to int. For example, const size = 10; means const int size = 10;
C++ requires const to be initialized. ANSI C does not require an initialization if none is given, it initializes the const to o. In C++ a const is local, it can be made as global defining it as external. In C const is global in nature , it can be made as local declaring it as static.


3.11:How does a constant defined by cowl differ from the constant defined by the preprocessor statement %define?


Ans:Consider a example : # define PI 3.14159


The preprocessor directive # define appearing at the beginning of your program specifies that the identifier PI will be replace by the text 3.14159 throughout the program.


The keyword const (for constant) precedes the data type of a variable specifies that the value of a variable will not be changed throughout the program.


In short, const allows us to create typed constants instead of having to use # define to create constants that have no type information.


3.12:In C++. a variable can be declared anywhere in the scope. What is the significance of this feature?


Ans:It is very easy to understand the reason of which the variable is declared.


3.13:What do you mean by dynamic initialization of a variable? Give an example.


Ans:When initialization is done at the time of declaration then it is know as dynamic initialization of variable


Example :
float area = 3.14159*rad * rad;

3.14:What is a reference variable? What is its major use?


Ans:A reference variable provides an alias (alternative name) for a previously defined variable.
A major application of reference variables is in passing arguments to functions.


3.15:List at least four new operators added by C++ which aid OOP.


Ans:
New opperators added by C++ are :
1. Scope resolution operator ::
2. Memory release operator delete &nbsp delete
3. Memory allocation operator &nbsp new
4. Field width operator &nbsp setw
5. Line feed operator &nbsp endl


3.16:What is the application of the scope resolution operator :: in C++?


Ans:A major application of the scope resolution operator is in the classes to identify the class to which a member function belongs.





3.17:What are the advantages of using new operator as compared to the junction ntallocOr


Ans:Advantages of new operator over malloc ():
1. It automatically computes the size of the data object. We need not use the operator size of.
2. It automatically returns the correct pointer type, so that there is no need to use a type cast.
3. It is possible to initialize the object while creating the memory space.
4. Like any other operator, new and delete can be overloaded.


3.18:Illustrate with an example, how the seize manipulator works.


Ans:
setw manipulator specifies the number of columns to print. The number of columns is equal the value of argument of setw () function.
For example :
setw (10) specifies 10 columns and print the massage at right justified.
cout << set (10) << “1234”; will print






1 2 3 4
If argument is negative massage will be printed at left justified.
cout <
1 2 3 4







3.19:How do the following statements differ?
(a) char *const p;
(b) char canal *p;



Ans:
(a) Char * const P; means constant pointer.
(b) Char const * P; means pointer to a constant.
In case of (a) we con not modify the address of p.
In case of (b) we can not modify the contents of what it points to.


3.1 What will happen when you execute the following code?
1
2
3
4
5
6
7
#include
void main()
{
     int i=0;
     i=400*400/400;
     cout<
}


Ans: i = 400*400/400; Here, 400*400 = 160000 which exceeds the maximum value of int variable. So wrong output will be shown when this program will be run.
Correction :
1
int i = 0;
should be changed as
1
long int i = 0;
to see the correct output.




3.2Identify the error in the following program.
1
2
3
4
5
6
include
void main()
{
     int num[]={1,2,3,4,5,6};
     num[1]==[1]num ? cout<<"Success" : cout<<"Error";
}

Ans: num [1] = [1] num?. You should write index number after array name but here index number is mention before array name in [1] num
So expression syntax error will be shown.
Correction : num[1] = num[1]? is the correct format

3.3 Identify the errors in the following program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include
void main()
{
     int i=5;
     while(i)
  {
    switch(i)
    {
    default:
    case 4:
    case 5:
    break;
    case 1:
    continue;
    case 2:
    case 3:
    break;
    }
i-;
 }
}

Ans:
1
2
case 1 :
continue;
The above code will cause the following situation:
Program will be continuing while value of i is 1 and value of i is updating. So infinite loop will be created.
Correction: At last line i- should be changed as i–;




3.4 Identify the errors in the following program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include
#define pi 3.14
int squareArea(int &);
int circleArea(int &);
void main()
{
     int a-10;
     cout << squareArea(a) << " ";
     cout « circleArea(a) « ";
     cout « a « endl;
 {
     int squareArea(int &a)
  {
     return a *== a;
  }
   int circleArea(int &r)
  {
   return r = pi * r * r;
  }

Ans: Assignment operator should be used in the following line:
1
return a *==a;
That means the above line should be changed as follows:
1
return a *=a;

3.5 Missing





3.6 Find errors, if any, in the following C++ statements.
(a) long float x;
(b) char *cp = vp; // vp is a void pointer
(c) int code = three; // three is an enumerator
(d) int sp = new; // allocate memory with new
(e) enum (green, yellow, red);
(f) int const sp = total;
(g) const int array_size;
(h) for (i=1; int i<10 br="" cout="" i="" n=""> (i) int & number = 100;
(j) float *p = new int 1101;
(k) int public = 1000;
(l) char name[33] = "USA";
 
Ans:

No. Error Correction
(a) too many types float x; or double x;
(b) type must be matched char *cp = (char*) vp;
(c) No error
(d) syntax error int*p = new int [10];
(e) tag name missing enum colour (green, yellow, red)
(f) address have to assign instead of content int const * p = &total;
(g) C++ requires a const to be initialized const int array-size = 5;
(h) Undefined symbol i for (int i = 1; i <10 cout="" i="" n="" td="">
(i) invalid variable name int number = 100;
(j) wrong data type float *p = new float [10];
(k) keyword can not be used as a variable name int public1 = 1000;
(l) array size of char must be larger than the number of characters in the string char name [4] = “USA”;





3.1 Write a function using reference variables as arguments to swap the values of a pair of integers.


Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include
#include
void swap_func(int &a,int &b)
{
    cout<<" Before swapping "<
        <<" a = "<" b = "<
    int temp;
    temp=a;
    a=b;
    b=temp;
    cout<<" After swapping "<
        <<" a = "<" b = "<
}
int main()
{
    int x,y;
    cout<<"  Enter two integer value : "<
    cin>>x>>y;
    swap_func (x,y);
    return 0;
}

output

Enter two integer value : 56 61
Before swapping
a = 56
b = 61
After swapping
a = 56
b = 61





3.2 Write a function that creates a vector of user given size M using new operator.


Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include
#include
int main()
{
    int m;
    int *v;
    cout<<"  Enter vector size : "<
    cin>>m;
    v=new int [m];
    cout<<" to check your performance  insert "<" integer value"<
    for(int i=0;i
    {
        cin>>v[i];
    }
    cout<<" Given integer value are :"<
    for(i=0;i
    {
        if(i==m-1)
        cout<
        else
        cout<",";
    }
    cout<
    return 0;
}

output

Enter vector size : 5
to check your performance insert 5 integer value
7 5 9 6 1
Given integer value are :
7, 5, 9, 6, 1


3.3 Write a program to print the following outputs using for loops


1
22
333
4444
55555
………………
Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include
#include
int main()
{
    int n;
    cout<<" Enter your desired number :"<
    cin>>n;
    cout<
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=i;j++)
        {
            cout<
        }
        cout<
    }
    return 0;
}

output

Enter your desired number : 6
1
22
333
4444
55555
666666





3.4 Write a program to evaluate the following investment equation
V = P(1+r)n
and print the tables which would give the value of V for various
of the following values of P, r and n:
P: 1000, 2000, 3000,……………,10,000
r: 0.10, 0.11, 0.12,………………….,0.20
n: 1, 2, 3,…………………………………..,10
(Hint: P is the principal amount and V is the value of money at the end of n years. This equation can be recursively written as
V = P(1 + r)
P = V
In other words, the value of money at the end of the first year becomes the principal amount for the next year and so on)


Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include
#include
#include
#define  size 8
int main()
{
    float v,pf;
    int n=size;
    float p[size]={1000,2000,3000,4000,5000,6000,7000,8000};//9000,1000};
    float r[size]={0.11,0.12,0.13,0.14,0.15,0.16,0.17,0.18};//,0.19,0.20};
    cout<"n=1";
        for(int i =2;i<=size;i++)
    cout<"n="<
    cout<<"\n";
    for(i=0;i
    {
        cout<"p=";
        for(int j=0;j
        {
           if(j==0)
            pf=p[i];
            v=pf*(1+r[i]);
            cout.precision(2);
                    cout.setf(ios::fixed, ios::floatfield);
            cout<
              pf=v;
         }
         cout<<"\n";
     }
    return 0;
}

output

n=1            n=2            n=3           n=4            n=5           n=6           n=7
p=1110     1232.1      1367.63     1518.07     1685.06     1870.41    2076.16
p=2240   2508.8    2809.86     3147.04    3524.68     3947.65   4421.36
p=3390   3830.7    4328.69     4891.42     5527.31     6245.86   7057.82
p=4560   5198.4    5926.18      67 55.84    7701.66     8779.89   10009.08
p=5750    6612.5    7604.37     8745.03     10056.79   11565.3    13300.1
p=6960   8073.6    9365.38    10863.84   12602.05  14618.38  16957.32
p=8190   9582.3    11211.29     13117.21     15347.14    17956.15   21008.7
p=9440   11139.2   13144.26   15510.22    18302.06   21596.43  25483.79


3.5 An election is contested by five candidates. The candidates are numbered 1 to 5 and the voting is done by marking the candidate number on the ballot paper. Write a program to read the ballots and count the vote cast for each candidate using an array variable count. In case, a number read is outside the range 1 to 5, the ballot should be considered as a “spoilt ballot” and the program should also count the numbers of “spoilt ballots”.


Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include
#include
int main()
{
    int count[5];
    int test;
    for(int i=0;i<5 code="" i="">
    {
        count[i]=0;
    }
    int spoilt_ballot=0;
    cout<<" You can vot candidate 1 to 5 "<
     <<" press 1 or 2 or 3 or 4 or 5 to vote "<
     <<" candidate 1 or 2 or 3 or 4 or 5 respectively "<
     <<" press any integer value outside the range 1 to 5 for NO VOTE    "<" press any negative value to terminate  and see result :"<
    while(1)
        {
            cin>>test;
            for(int i=1;i<=5;i++)
            {
                if(test==i)
                {
                    count[i-1]++;
                }
            }
           if(test<0 code="">
                break;
          else if(test>5)
                spoilt_ballot++;
        }
        for(int k=1;k<=5;k++)
        cout<<" candidate "<
        cout<
        cout<
        for(k=0;k<5 code="" k="">
        cout<
        cout<
        cout<<" spoilt_ballot "<
        return 0;
}

output

You can vot candidate 1 to 5
press 1 or 2 or 3 or 4 or 5 to vote
candidate 1 or 2 or 3 or 4 or 5 respectively
press any integer value outside the range 1 to S for NO VOTE
press any negative value to terminate and see result :
1
1
1
5
4
3
5
5
2
1
3
6
-1
candidate 1 candidate 2 candidate 3 candidate 4 candidate S
4   1   2   1   3
spoilt_ballot 1





3.6 A cricket has the following table of batting figure for a series of test matches:
Player’s name             Run             Innings         Time  not outSachin                       8430                  230                        18Saurav                      4200                  130                          9Rahul                       3350                  105                         11.                                      .                        .                              ..                                      .                        .                              . .                                      .                        .                              .
Write a program to read the figures set out in the above forms, to calculate the batting arranges and to print out the complete table including the averages.


Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
         #include
#include
char *serial[3]={" FIRST "," SECOND " ," THIRD "};//global declaration
int main()
{
    int n;
     char name[100][40];
    int *run;
    int *innings;
    int *time_not_out;
    cout<<" How many players' record would you insert ? :";
    cin>>n;
    //name=new char[n];
    run=new int[n];
    innings=new int[n];
    time_not_out=new int[n];
    for(int i=0;i
    {
        if(i>2)
        {
            cout<<"\n Input details of "<"th"<<" player's"<
        }
        else
        {
            cout<<" Input details of "<"player's : "<
        }
            cout<<" Enter name : ";
            cin>>name[i];
            cout<<" Enter run : ";
            cin>>run[i];
            cout<<" Enter innings : ";
            cin>>innings[i];
            cout<<" Enter times not out : ";
            cin>>time_not_out[i];
    }
        float *average;
        average=new float[n];
        for(i=0;i
        {
            float avrg;
            average[i]=float(run[i])/innings[i];
        }
        cout<
        cout<"player's name "<"run"<"innings"<"Average"<"times not out"<
        for(i=0;i
        {
            cout<
        }
        cout<
        return 0;
}

output

How many players record would you insert ? :2
Input details of FIRST player’s :
Enter name : Sakib-Al-Hassan
Enter run : 1570
Enter innings : 83
Enter times not out : 10
Input details of SECOND player’s :
Enter name : Tamim
Enter run : 2000
Enter innings : 84
Enter times not out : 5
player’s name run innings Average times not out
Sakib-Al-Hassan 1570 83 18.915663 10
Tamim 2000 84 23.809525 5





3.7  Write a program to evaluate the following function to 0.0001% accuracy
(a) sinx = x – x3/3! + x5/5! – x7/7! +…………
(b) SUM = 1+(1/2)2 + (1/3)3 +(1/4)4 + ………
(c) Cosx = 1 –x2/2! + x4/4! – x6/6! + ………


Solution (a):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include
#include
#include
#define accuracy 0.0001
#define pi 3.1416
long int fac(int a)
{
    if(a<=1)
        return 1;
    else
        return a*fac(a-1);
}
int main()
{
    float y,y1,x,fx;
    int n=1;
    int m;
    //const float pi=3.1416;
    cout<<" Enter the value of angle in terms of degree:  ";
        cin>>x;
        float d;
        d=x;
    int sign;
        sign=1;
if(x<0 code="">
{
        x=x*(-1);
        sign=-1;
 }
again:
    if(x>90 && x<=180)
    {
        x=180-x;
    }
    else if(x>180 && x<=270)
    {
        x=x-180;
        sign=-1;
    }
    else if(x>270 && x<=360)
    {
        x=360-x;
        sign=-1;
    }
    else if(x>360)
    {
        int m=int(x);
        float fractional=x-m;
        x=m%360+fractional;
        if(x>90)
            goto again;
        else
        sign=1;
    }
    x=(pi/180)*x;
    m=n+1;
    fx=0;
    for(;;)
    {
          long int h=fac(n);
          y=pow(x,n);
          int factor=pow(-1,m);
          y1=y*factor;
          fx+=y1/h;
          n=n+2;
          m++;
          if(y/h<=accuracy)
              break;
    }
    cout<<"sin("<")= "<
    return 0;
}

output

Enter the value of angle in terms of degree: 120
sin(120)= 0.866027


Solution (b):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include
#include
#define accuracy 0.0001
int main()
{
    int n;
    float sum,n1,m;
    n=1;sum=0;
    for(int i=1;;i++)
    {
            n1=float(1)/n;
            m=pow(n1,i);
            sum+=m;
            if(m<=accuracy)
              break;
            n++;
            }
    cout<"\n";
    return 0;
}
        Sample Output(b)
          Solution: (c)
         #include
#include
#define accuracy 0.0001
        long int fac(int n)
{
    if(n<=1)
        return 1;
    else
        return n*fac(n-1);
}
        int main()
{
      float y,y1,x,fx;
      int n=1;
      int m;
      const float pi=3.1416;
      cout<<" Enter the value of angle in terms of degree:  ";
      cin>>x;
      if(x<0 code="">
        x=x*(-1);
      x=(pi/180)*x;
        fx=1;
         m=2;
        float y2;
        long int h;
        for(;;)
        {
            h=fac(m);
            int factor=pow(-1,n);
            y1=pow(x,m);
            y2=(y1/h)*factor;
            fx+=y2;
            if(y1/h<=accuracy)
                break;
            m=m+2;
            n++;
        }
        cout<"\n";
}

output

Enter the value of angle in terms of degree: 60
0.866025





3.8: Write a program to print a table of values of the function
Y = e-x
For x varying from 0 to 10 in steps of 0.1. The table should appear as follows
TABLE   FOR   Y =EXP[-X];
   X     0.1      0.2       0.3      0.4      0.5      0.6     0.7      0.8      0.900 1.0
.
.
9.0


Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include
#include
#include
          int main()
{
        float x,y;
        cout<<"                          TABLE FOR  Y=EXP(-X)             :\n\n";
        cout<<"x";
        for(float k=0;k<.7;k=k+0.1)
        cout<
        cout<<"\n";
        for(k=0;k<10 k="k+0.1)</code">
        cout<<"-";
        cout<<"\n";
               for(float j=0;j<10 code="" j="">
        {
            cout<
            for(float i=0;i<.7;i=i+0.1)
            {
                    x=i+j;
                    y=exp(-x);
                    cout.precision(6);
                                        cout.setf(ios::fixed,ios::floatfield);
                    cout<
            }
            cout<<"\n";
        }
        return 0;
}
Note: Here we work with 0.4 for a good looking output.

output

TABLE FOR Y=EXP(-X)
x               0               0.1                  0.2                0.3                    0.4
0              1               0.904837      0.818731       0.740818      0.67032
1       0.367879       0.332871       0.301194        0.272532     0.246597
2       0.135335       0.122456       0.110803        0.100259     0.090718
3       0.049787      0.045049      0.040762       0.036883     0.033373
4       0.018316       0.016573       0.014996       0.013569      0.012277
5       0.006738      0.006097      0.005517        0.004992     0.004517
6       0.002479      0.002243      0.002029       0.001836      0.001662
7       0.000912      0.000825      0.000747        0.000676     0.000611
8       0.000335     0.000304      0.000275        0.000249      0.000225
9       0.000123      0.000112       0.000101        0.000091       0.000083


3.9 Write a program to calculate the variance and standard deviation of
N numbers
Variance =1/N ∑(xi -x)2
Standard deviation=√1/N ∑(xi -x)2
Where x = 1/N ∑xi


Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include
#include
         int main()
{
     float *x;
             cout<<" How many number ? :";
     int n;
     cin>>n;
     x=new float[n];
     float sum;
     sum=0;
     for(int i=0;i
     {
            cin>>x[i];
            sum+=x[i];
     }
     float mean;
     mean=sum/n;
     float v,v1;
     v1=0;
     for(i=0;i
     {
            v=x[i]-mean;
            v1+=pow(v,2);
     }
     float variance,std_deviation;
     variance=v1/n;
     std_deviation=sqrt(variance);
     cout<<"\n\n variance = "<"\n standard deviation = "<"\n";
    return 0;
}

output

How many number ? :5
10
2
4
15
2
variance = 26.24
standard deviation = 5.122499





3.10 An electricity board charges the following rates to domestic users to
discourage large consumption of energy:
For the first 100 units                       – 60P per unit
For the first 200 units                      – 80P per unit
For the first 300 units                      – 90P per unit
All users are charged a minimum of Rs. 50.00. If the total amount is more than Rs. 300.00 then an additional surcharge of 15% is added.
Write a program to read the names of users and number of units consumed and print out the charges with names.


Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include
#include
int main()
{
    int unit;
    float charge,additional;
    char name[40];
    while(1)
    {
        input:
              cout<<" Enter consumer name & unit consumed :";
                cin>>name>>unit;
                if(unit<=100)
                {
                    charge=50+(60*unit)/100;
                }
                else if(unit<=300 && unit>100)
                {
                    charge=50+(80*unit)/100;
                }
                else if(unit>300)
                {
                    charge=50+(90*unit)/float(100);
                    additional=(charge*15)/100;
                    charge=charge+additional;
                }
                cout<"Name"<"Charge"<
                cout<
                cout<<" Press o for exit / press 1 to input again :";
                int test;
                cin>>test;
                if(test==1)
                goto input;
                else if(test==0)
                break;
    }
    return 0;
}

output

Enter consumer name & unit consumed :sattar 200
Name                 Charge
sattar                 210
Press o for exit / press 1 to input again :1
Enter consumer name & unit consumed :santo 300
Nmae             Charge
santo              290
Press o for exit / press 1 to input again : 0




4.1: State whether the following statements are TRUE or FALSE.
(a) A function argument is a value returned by the function to the calling program.
(b) When arguments are passed by value, the function works with the original arguments in the calling program.
(c) When a function returns a value, the entire function call can be assigned to a variable.
(d) A function can return a value by reference.
(e) When an argument is passed by reference, a temporary variable is created in the calling program to hold the argument value.
(f) It is not necessary to specify the variable name in the function prototype.


Ans: 
(a) FALSE (d) TRUE
(b) FALSE (e) FALSE
(c) TRUE (f) TRUE


4.2:What are the advantages of function prototypes in C++?


Ans: Function prototyping is one of the major improvements added to C++ functions. The prototype describes the function interface to the compiler by giving details such as the number and type of arguments and the type of return values.


4.3:Describe the different styles of writing prototypes.


Ans:
General form of function prototyping :
return_type function_name (argument_list)
Example :
int do_something (void);
float area (float a, float b);
float area (float, float);


4.4:Find errors, if any, in the following function prototypes.
(a) float average(x,y);
(b) int mul(int a,b);
(c) int display(….);
(d) void Vect(int? &V, int & size);
(e) void print(float data[], size = 201);


Ans: 
No. Error Correction
(a) Undefined symbol x, y float average (float x, floaty)
(b) Undefined symbol b int mul (int a, int b);
(c) No error
(d) invalid character in variable name void vect (int &v, int &size);
(e) Undefined symbol ‘s’ void print (float data [ ], int size = 20);


4.5:What is the main advantage of passing arguments by reference?


Ans: When we pass arguments by reference, the formal arguments in the called function become aliases to the ‘actual’ arguments in the calling function.


4.6:When will you make a function inline? Why?


Ans: When a function contains a small number of statements, then it is declared as inline function. By declaring a function inline the execution time can be minimized.


4.7:How does an inline function differ from a preprocessor macro?


Ans: The macos are not really functions and therefore, the usual error checking does not occur during compilation. But using inline-function this problem can be solved.


4.8:When do we need to use default arguments in a function?


Ans: When some constant values are used in a user defined function, then it is needed to assign a default value to the parameter.
Example :
1
2
3
4
Float area (float r, float PI = 3.1416)
    {
           return PI*r*r;
    }


4.9: What is the significance of an empty parenthesis in a function declaration?


Ans: An empty parentheses implies arguments is void type.


4.10:What do you meant by overloading of a function? When do we use this concept?


Ans: Overloading of a function means the use of the same thing for different purposes.
When we need to design a family of functions-with one function name but with different argument lists, then we use this concept.


4.11: Comment on the following function definitions:
(a)
1
2
3
4
5
6
7
int *f( )
{
int m = 1;
.....
.....
return(&m);
}
(b)
1
2
3
4
5
6
double f( )
{
.....
.....
return(1);
}
(c)
1
2
3
4
5
6
7
int & f()
{
int n - 10;
.....
.....
return(n);
}


Ans:
No. Comment
(a) This function returns address of m after execution this function.
(b) This function returns 1 after execution.
(c) returns address of n




4.1: Identify the error in the following program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include
int fun()
{
     return 1;
}
float fun()
{
     return 10.23;
}
void main()
{
     cout <<(int)fun() << ' ';
     cout << (float)fun() << ' '
}


Solution: Here two function are same except return type. Function overloading can be used using different argument type but not return type.
Correction : This error can be solved as follows :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
     
#include
int fun()
{
    return 1;
}
float fun1()
{
    return 10.23;
}
void main()
{
     cout<"  ";
     cout<"  ";
}





4.2: Identify the error in the following program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include
void display(const Int constl=5)
{
   const int const2=5;
   int arrayl[constl];
   int array2[const2];
   for(int 1=0; i<5 1="" code="">
  
     arrayl[i] = i;
     array2[i] = i*10;
     cout <' ' << array2[i] << ' ' ;
  }
}
void main()
{
   display(5);
}


Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include
void display()
{
     const int const1=5;
     const int const2=5;
     int array1[const1];
     int array2[const2];
       for(int i=0;i<5 code="" i="">
       {
           array1[i]=i;
           array2[i]=i*10;
           cout<"  "<"  ";
       }
}
void main()
{
    display();
}





4.3: Identify the error in the following program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include
int gValue=10;
void extra()
{
     cout << gValue << ' ';
}
void main()
{
    extra();
    {
    int gValue = 20;
    cout << gValue << ' ';
    cout << : gValue << ' ';
    }
}


Solution:
Here cout << : gvalue << " "; replace with cout <<::gvalue p="">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include
int gValue=10;
void extra()
{
     cout << gValue << ‘ ‘;
}
void main()
{
    extra();
    {
    int gValue = 20;
    cout << gValue << ‘ ‘;
    cout <<::gvalue code="">" ";
    }
}





4.4: Find errors, if any, in the following function definition for displaying a matrix: void display(int A[][], int m, int n)
1
2
3
4
5
6
{
  for(1=0; i
  for(j=o; j
   cout<<" "<
   cout<<"\n";
}


Solution:
First dimension of an array may be variable but others must be constant.
Here int A [ ] [ ] replace by the following code:
int A [ ] [10];
int A[10] [10];
int A[ ] [size];
int A [size] [size];
Where const int size = 100;
any other numerical value can be assigned to size.

4.1: Write a function to read a matrix of size m*n from the keyboard.


Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include
#include
void matrix(int m,int n)
{
    float **p;
    p=new float*[m];
    for(int i=0;i
    {
        p[i]=new float[n];
    }
    cout<<" Enter "<"by"<" matrix elements one by one "<
    for(i=0;i
    {
        for(int j=0;j
        {
            float value;
            cin>>value;
            p[i][j]=value;
        }
    }
    cout<<" The given matrix is :"<
    for(i=0;i
    {
        for(int j=0;j
        {
            cout<
"   ";
        }
        cout<<"\n";
    }
}
int main()
{
    int r,c;
    cout<<" Enter size of matrix : ";
    cin>>r>>c;
    matrix(r,c);
    return 0;
}

output

Enter size of matrix : 3   4
Enter 3 by 4 matrix elements one by one
1    2    3    4
2    3    4    5
3    4    5    6
The given matrix is :
1    2    3    4
2    3    4    5
3    4    5    6


4.2: Write a program to read a matrix of size m*n from the keyboard and display the same on the screen using function.


Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include
#include
void matrix(int m,int n)
{
    float **p;
    p=new float*[m];
    for(int i=0;i
    {
        p[i]=new float[n];
    }
    cout<<" Enter "<" by "<" matrix elements one by one "<
    for(i=0;i
    {
        for(int j=0;j
        {
            float value;
            cin>>value;
            p[i][j]=value;
        }
    }
    cout<<" The given matrix is :"<
    for(i=0;i
    {
        for(int j=0;j
        {
            cout<
"   ";
        }
        cout<<"\n";
    }
}
int main()
{
    int r,c;
    cout<<" Enter size of matrix : ";
    cin>>r>>c;
    matrix(r,c);
    return 0;
}

output

Enter size of matrix : 4   4
Enter 4 by 4 matrix elements one by one
1    2    3    4    7
2    3    4    5    8
3    4    5    6    9
The given matrix is :
1    2    3    4    7
2    3    4    5    8
3    4    5    6    9


4.3: Rewrite the program of Exercise 4.2 to make the row parameter of the matrix as a default argument.


Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include
#include
void matrix(int n,int m=3)
{
    float **p;
    p=new float*[m];
    for(int i=0;i
    {
        p[i]=new float[n];
    }
    cout<<" Enter "<" by "<" matrix elements one by one "<
    for(i=0;i
    {
        for(int j=0;j
        {
            float value;
            cin>>value;
            p[i][j]=value;
        }
    }
    cout<<" The given matrix is :"<
    for(i=0;i
    {
        for(int j=0;j
        {
            cout<
"   ";
        }
        cout<<"\n";
    }
}
int main()
{
    int c;
    cout<<" Enter column of matrix : ";
    cin>>c;
    matrix(c);
    return 0;
}

output

Enter column of matrix  :  3
Enter 3 by 3 matrix elements one by one
1    2    3
2    3    4
3    4    5
The given matrix is :
1    2    3
2    3    4
3    4    5


4.4: The effect of a default argument can be alternatively achieved by overloading. Discuss with examples.


Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include
#include
void matrix(int m,int n)
{
    float **p;
    p=new float*[m];
    for(int i=0;i
    {
        p[i]=new float[n];
    }
    cout<<" Enter "<"by"<" matrix elements one by one "<
    for(i=0;i
    {
        for(int j=0;j
        {
            float value;
            cin>>value;
            p[i][j]=value;
        }
    }
    cout<<" The given matrix is :"<
    for(i=0;i
    {
        for(int j=0;j
        {
            cout<
"   ";
        }
        cout<<"\n";
    }
}
void matrix(int m,long int n=3)
{
    float **p;
    p=new float*[m];
          for(int i=0;i
    {
        p[i]=new float[n];
    }
    cout<<" Enter "<" by "<" matrix elements one by one "<
    for(i=0;i
    {
        for(int j=0;j
        {
            float value;
            cin>>value;
            p[i][j]=value;
        }
    }
    cout<<" The given matrix is :"<
    for(i=0;i
    {
        for(int j=0;j
        {
            cout<
"   ";
        }
        cout<<"\n";
    }
}
int main()
{
    int r;
    cout<<" Enter row of matrix : ";
    cin>>r;
    matrix(r);
    return 0;
}

output

Enter column of matrix  :  2
Enter 2 by 3 matrix elements one by one
1    0    1
0    2    1
The given matrix is :
1    0    1
0    2    1





4.5: Write a macro that obtains the largest of the three numbers.


Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include
#include
float  large(float a,float b,float c)
{
    float largest;
    if(a>b)
    {
        if(a>c)
            largest=a;
        else
            largest=c;
    }
    else
    {
        if(b>c)
            largest=b;
        else
            largest=c;
    }
    return largest;
}
int main()
{
    float x,y,z;
    cout<<" Enter three values : ";
    cin>>x>>y>>z;
    float largest=large(x,y,z);
    cout<<" large = "<
    return 0;
}

output

Enter three values : 4  5  8
large = 8


4.6: Redo Exercise 4.16 using inline function. Test the function using a main function.


Solution:
Blank


4.7: Write a function power() to raise a number m to power n. The function takes a double value for m and int value for n and returns the result correctly. Use a default value of 2 for n to make the function to calculate the squares when this argument is omitted. Write a main that gets the values of m and n from the user to test the function.
Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include
#include
#include
long double power(double m,int n)
{
    long double mn=pow(m,n);
    return mn;
}
long double power(double m,long int n=2)
{
    long double mn=pow(m,n);
    return mn;
}
int main()
{
    long double mn;
    double m;
    int n;
    cout<<" Enter the value of m & n"<
    cin>>m>>n;
    mn=power(m,n);
    cout<<" m to power n : "<
    mn=power(m);
    cout<<" m to power n : "<
    return 0;
}

output

Enter the value of m & n
12    6
m to power n : 2985984
m to power n: 144


4.6: Redo Exercise 4.16 using inline function. Test the function using a main function.


Solution:
Blank
4.7: Write a function power() to raise a number m to power n. The function takes a double value for m and int value for n and returns the result correctly. Use a default value of 2 for n to make the function to calculate the squares when this argument is omitted. Write a main that gets the values of m and n from the user to test the function.


Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include
#include
#include
long double power(double m,int n)
{
    long double mn=pow(m,n);
    return mn;
}
long double power(double m,long int n=2)
{
    long double mn=pow(m,n);
    return mn;
}
int main()
{
    long double mn;
    double m;
    int n;
    cout<<" Enter the value of m & n"<
    cin>>m>>n;
    mn=power(m,n);
    cout<<" m to power n : "<
    mn=power(m);
    cout<<" m to power n : "<
    return 0;
}

output

Enter the value of m & n
12    6
m to power n : 2985984
m to power n: 144


4.8: Write a function that performs the same operation as that of Exercise 4.18 but takes an int value for m. Both the functions should have the same name. Write a main that calls both the functions. Use the concept of function overloading.


Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include
#include
#include
long double power(int m,int n)
{
    long double mn= (long double)pow(m,n);
    return mn;
}
long double power(int m,long int n=2)
{
    long double mn=(long double)pow(m,n);
    return mn;
}
int main()
{
    long double mn;
    int m;
    int n;
    cout<<" Enter the value of m & n"<
    cin>>m>>n;
    mn=power(m,n);
    cout<<" m to power n : "<
    mn=power(m);
    cout<<" m to power n : "<
    return 0;
}

output

Enter the value of m & n
15    16
m to power n : 6.568408e+18
m to power n: 225