Unit II CPP
Unit II CPP
Needs of Structure:--------------------------------------------------------------------
Suppose we want to store details of a “Student” like his/her Name, Age, Marks . In a typical scenario we will
have to create 3 variables to store these values, but what if we want to store details of 10 students. Then we have
to create 30 variables and that will be very inefficient and also tedious. In such case we can create a user defined
data type “Structure” using struct keyword named Student and include all the details as member variables of
this datatype to create a custom datatype. We can then simply create 10 variable of this structure datatype or a
single array size of 10.
Structures provide a more convenient and structured way to group related data together, Making it easier to
work with and manipulate the data as a single entity.
Structure:---------------------------------------------------------------------------------
Structure in C++ is a group of data elements grouped together under one name. These data elements are known as
members,
Structure is a user defined datatype that can hold different types of data types.
It is a way to group several related variables into one place.
To create a structure, we use the struct keyword and declare each of its members inside curly braces.
To access members of a structure, we can use the dot operator (.).
Syntax:-
The ‘struct’ keyword is used to create a structure. The general syntax to create a structure is as shown below:
struct StructureName {
member1;
member2;
member3;
.
.
.
memberN;
};
Structure in C++ can contain two types of members:-
1) Data member:- Data member are normal C++ variables.
2) Member functions:- These members are normal C++ functions. Along with variables,we can include functions
inside a structure declaration.
How to declare structure variables-----------------------------------------------------------
A structure variable can either be declared with structure declaration or as a separate declaration like basic types.
1) A variable declaration with structure declaration.
struct Point
{
int x, y;
} p1; // The variable p1 is declared with 'Point'
int main()
{
Point p1; // The variable p1 is declared like a normal variable
}
Ex:- 1.
struct Student
{
int rollno;
char name[10];
}s;
int main()
{
cout<<”rollno is: ”;
cin>>s.rollno;
cout<<”name is: ”;
cin>>s.name;
cout<<s.rollno;
cout<<s.name;
}
--------------------------------------------------------------------------------------------------------------------------------------------------
2.
struct Student
{
int rollno;
int age;
char gender;
};
int main()
{
Student s={101,24,'m'};
cout<<"roll number is:- "<<s.rollno<<endl;
cout<<"age is:- "<<s.age<<endl;
cout<<"gender is:- "<<s.gender<<endl;
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------------
3.
struct student
{
char first_name[10]={'r','a','m'};
char last_name[10]={'s','a','h','u'};
int rollno=10;
void display()
{
cout<<first_name<<" ";
cout<<last_name<<endl;
cout<<rollno;
}
}s;
int main()
{
s.display();
return 0;
}
enum/Enumeration:----------------------------------------------------------------------------
It is a user defined data type, which is used to assign names to the integral constants.
Each enumeration is a constant whose type is the enumeration.
enum keyword is used to define enumeration.
If we do not assign values to enum names then, automatically compiler will assign values to them starting from 0
to the first var_name , the second name has the value 1,the third has the value 2,and so on. But we can also assign
a specific value by adding an initializer.
We can assign values in any order. All unassigned names will get value as value of previous name +1.
Two or more names can have same value.
We can only assign integral constants to names, we cannot assign any float value to the names.
All enum constant must be unique in their scope.
Ex:-
int main()
{
enum list1{a, b=2, c};
enum list2{a, d, e};
list1 I;
I =a;
cout<<I;
}//this code will through an error,like redeclaration of enum a.
Syntax:- enum enum_name{list_of_var};//here enum_name is enumeration type ,the list of var is separated by commas
enum week{sun,mon,tue};
Needs of enum/Enumeration:-
There are two important reasons:-
1) enum can be declared in the local or global scope.
2) enum names are automatically initialized by the compiler but in macros we have to explicitly assign the
value.
3) At one point of time we can take list of value in enum but in macros we can take only one value at a time.
________________________________________________________________________________________________
EX:- Counting the element inside the enum
enum week{sunday,monday,tuesday,wednuesday,thursday,friday,saturday};
int main()
{
int ctr=0;
for(int i=sunday;i<=saturday;i++)
{
ctr++;
}
cout<<"Total number of element in enum is: "<<ctr<<endl;
return 0;
}
A C++ function definition consists of a function header and a function body. Here are all the parts of a function −
Return Type − A function may return a value. The return_type is the data type of the value the function returns.
Function Name − This is the actual name of the function. The function name and the parameter list together constitute
the function signature.
Parameters − A parameter is like a placeholder. When parameters are passed to a function, it is called actual
parameters. When parameters receives by a function is called formal parameters. Parameters are optional; that is, a
function may contain no parameters.
Function Body − The function body contains a collection of statements that define what the function does.
Function Calling:-----------------------------------------------------------------------------------
Function can be call from main () or from other function with or without arguments.
Function call sends the control of execution to the function definition (body) and function definition return a
value to the calling function.
Calling function does not use datatype.
There are three types of calling function:-
1. Call by Value:-
Function call takes values as arguments to send its function definition (body).
Here value of actual parameters will be copied to formal parameters and these two different
parameters store values in different location.
In this case, changes made to the parameter inside the function have no effect on the argument.
By default, C++ uses call by value to pass arguments. In general, this means that code within a
function cannot alter the arguments used to call the function
Syntax: fun_name(arguments);
Ex:-
void swap(int x, int y);
void swap(int x, int y) {
int temp;
temp = x;
x = y;
y = temp;
return;
}
int main () {
// local variable declaration:
int a = 100;
int b = 200;
cout << "Before swap, value of a :" << a << endl; //100
cout << "Before swap, value of b :" << b << endl; //200
// calling a function to swap the values:
swap(a, b);
cout << "After swap, value of a :" << a << endl; //100
cout << "After swap, value of b :" << b << endl; //200
return 0;
}
2. Call by Address:-
Address operator(&) is used in function call by address.
The Call by Address method of passing arguments to a function the address of an argument into the
formal parameter.
Inside the function, the address is used to access the actual argument used in the call. And pointer
variables are used in the function definition. This means that changes made to the parameter affect
the passed argument.
Here both actual and formal parameters refers to different memory location therefore.
Ex:-
void swap(int *x, int *y);
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
return;
}
int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
cout << "Before swap, value of a :" << a << endl; //100
cout << "Before swap, value of b :" << b << endl; //200
//calling a function to swap the values using variable reference:
swap(&a, &b);
cout << "After swap, value of a :" << a << endl; //200
cout << "After swap, value of b :" << b << endl; //100
return 0;
}
3. Call by Reference:-
The call by Reference method of passing arguments to a function the reference of an argument into
the formal parameter.
Inside the function, the reference is used to access the actual argument used in the call, this means
that changes made to the parameter affect the passed argument.
Here both actual and formal parameters refers to same memory location therefore, any changes
made to the formal parameters will get reflected to the actual parameters.
Ex:-
void swap(int &a, int &b)
{
int temp=a;
a=b;
b=temp;
}
int main()
{
int x=5;
int y=6;
cout<<”The value of x: ”<<x<<endl<<”The value of y: ”<<y;
swap(x,y);
cout<<”The value of x is: ”<<x<<endl<<”The value of y: ”<<y;
return 0;
}
Call by Value vs Call by Address:-------------------------------------------------------------
With this method, the changes made to the dummy With this method, using addresses we would have access to the
variables in the called function have no effect on the actual variables and hence we would be able to manipulate
values of actual variables in the calling function. them.
In call-by-values, we cannot alter the values of actual In call by reference, we can alter the values of variables through
variables through function calls. function calls.
This method is preferred when we have to pass some This method is preferred when we have to pass a large amount
small values that should not change. of data to the function.
Call By Address is a way of calling a function in which the call by reference is a method of passing arguments to a
address of the actual arguments is copied to the formal function by copying the reference of an argument into the
parameters formal parameter.
call by address, the memory is allocated for both actual call by reference, the memory is allocated only for actual
arguments and formal parameters. arguments and formal parameters share that memory.
A constant parameter is declared in the case when it is necessary that the value of the transferred object remains
unchanged in the body of the called function. This case is possible when the argument’s value is passed by the
address when function is called. To pass by address a pointer or a reference to variable is used.
If the argument is passed by value, then there is no sense to declare a parameter with the keyword ‘const’. Since, in
this case, the function gets a copy of the original variable. Changing a copy of a variable will not cause this variable
to change in the program.
Syntax:-
return_type FunName(const type1 parameter1, const type2 parameter2, ..., const typeN
parameterN)
{
...
}
Ex:-
int num(const int *x)
{
int c=*x+1;
return *x;
}
int main()
{
int a=6;
cout<<num(&a);//7
return 0;
}
Structure variable is passed using call by value. To take a structure variable as argument, function must declare a
structure argument in it's declaration. Any change in the value of formal parameter inside function body, will not
affect the value of actual parameter.
We can also pass address of a structure to a function. In this case, any change in the formal parameter inside
function's body will reflect in actual parameter also. To take a structure pointer as parameter a function declare a
structure pointer as it's formal parameter.
Ex:-struct Student
{
int rollno;
int age;
};
void display(Student s)
{
cout<<"Display function displaying student details:- "<<endl<<endl;
s.rollno=102;
cout<<"Rollno is: - "<<s.rollno<<endl;
cout<<"age is: - "<<s.age<<endl<<endl<<endl;
}
void show(Student *s)
{
cout<<"Show function displaying student details:- "<<endl<<endl;
s->rollno=102;
cout<<"Rollno is: - "<<s->rollno<<endl;
cout<<"age is: - "<<s->age<<endl<<endl<<endl;
}
int main()
{
Student a={101,25};
display(a);
cout<<a.rollno;
cout<<endl<<endl<<"------------------------------------------------------";
cout<<endl<<endl;
show(&a);
cout<<a.rollno;
return 0;
}
OR
struct employee {
char name[100];
int age;
float salary;
char department[50];
};
int main(){
struct employee manager, *ptr;
return 0;
}
Default argument:--------------------------------------------------------------------------------
In C++ programming, we can provide default value for function parameters through which we can call function
without passing any arguments.
A default argument is a value provided in a function declaration that is automatically assigned by the compiler if
the calling function doesn’t provide a value for the argument. In case any value is passed, the default value is
overridden.
If a function with default arguments is called without passing arguments, then the default parameters are used.
In case any value is passed, the default value is overridden. That means if arguments are passed while calling the
function, the default arguments are ignored.
Default arguments are different from constant arguments as constant arguments can’t be changed whereas default
arguments can be overwritten if required.
During the calling of function the values are copied from left to right, all the values will be given default value
will be on the right.
Function with default argument supports the concept of function polymorphism.
Ex:-
int sum( int a, int b, int c=7)
{
return a+b+c;
}
int main()
{
int a=3,b=4;
cout<<sum(a,b);
return 0;
}
A function terminates when either a return statement is encountered or the last statement in the function is executed.
Generally a return statement is used to terminate a function whether or not it returns a value.
Return statement:----------------------------------------------------------------------------------
The return statement is useful in two ways. First an immediate exit from the function is caused as soon as a return
statement is encountered and control passes back to statement following the called function in the calling code. A
return in main takes us back to the operating system which is main functions caller.
Second use of return statement is that it is used to return a value to calling code.
1. Methods not returning a value: In C/C++ one cannot skip the return statement, when the methods are of return type.
The return statement can be skipped only for void types.
Not using return statement in void return type function: When a function does not return anything, the void return
type is used. So if there is a void return type in the function definition, then there will be no return statement inside that
function (generally).
Syntax: void fun_name()
{
…………..;
…………..;
}
Ex:-
void Print()
{
cout<<"Welcome To The world";
}
int main()
{
Print();
return 0;
}
Using return statement in void return type function: Now the question arises, what if there is a return statement
inside a void return type function? Since we know that, if there is a void return type in the function definition, then
there will be no return statement inside that function. But if there is a return statement inside it, then also there will be
no problem if the syntax of it will be:
Ex:
void Print()
{
cout<<"Welcome to The World";
2. Methods returning a value: For methods that define a return type, the return statement must be immediately followed
by the return value of that specified return type.
Syntax:
return-type func()
{
return value;
}
Ex:-
int SUM(int a, int b)
{
return a + b;
// Driver method
int main()
{
int num1 = 10;
int num2 = 10;
int sum_of = SUM(num1, num2);
cout << "The sum is " << sum_of;
return 0;
}
NOTE:-
Returning anything from a function with a void return type, leads to a compile error.
Ex:- void msg( )
{
cout<<”hlo”;
return 0; //compile error
}
Empty return statement in a function with void return type works fine.
Ex:- void msg( )
{
cout<<”hlooo”;
return ;
}
Larger primitive return type of a function can be used to return a smaller primitive value.
struct Employee {
int Id;
string Name;
};
Employee data(Employee E)
{
E.Id = 45;
E.Name = "aman";
return (E);
}
int main()
{
Employee Emp;
Emp = data(Emp);
cout << "Employee Id: " << Emp.Id;
cout << "\nEmployee Name: " << Emp.Name;
return 0;
}
………………………………………..…OR……..………………………………………
struct MyS
{
int a;
int b;
}s;
MyS show(MyS k)
{
k.a=12;
k.b=14;
return (k);
}
int main()
{
MyS s;
s=show(s);
cout<<s.a<<endl<<s.b;
return 0;
}
Return by reference………………………………………….…………….
Return by reference is very different from Call by reference.
A C++ function can return a reference in a similar as it returns a pointer. when a function returns a reference, it
returns an implicit pointer to its return value.
This is a way, a function can be used on the left side of an assignment statement.
We should never return a local variable as a reference, reason being, as soon as the functions returns, local
variable will be erased, however, we still will be left with a reference which might be a security bug in the code.
Functions in C++ can return a reference as it’s returns a pointer.
When function returns a reference it means it returns a implicit pointer.
-----------------------------------------------------------------------------------------------------------------------------------------
Function Overloading:---------------------------------------------------------------------------
Function overloading is a feature of object-oriented programming where two or more functions can have the same
name but different parameters. When a function name is overloaded with different jobs it is called Function
Overloading.
In Function Overloading “Function” name should be the same and the arguments should be different. Function
overloading can be considered as an example of a polymorphism (static) feature in C++.
An overloaded function must have:-
Different type of parameters
Different number of parameters
Different sequence of parameters
Ex:-
#include <iostream>
using namespace std;
void add(int a, int b)
{
cout << "sum = " << (a + b);
}
void add(double a, double b)
{
cout << endl << "sum = " << (a + b);
}
int main()
{
add(10, 2);
add(5.3, 6.2);
return 0;
}
Advantages:-
It improves code readability and code reusability. And it eliminates the use of different function names for
the same kind of operation.
Inline function:-------------------------------------------------------------------------------------
When the program executes the function call instruction the CPU stores the memory address of the instruction
following the function call, copies the arguments of the function on the stack and finally transfers control to the
specified function. The CPU then executes the function code, stores the function return value in a predefined
memory location/register and returns control to the calling function. This can become overhead if the execution
time of function is less than the switching time from the caller function to called function.
For functions that are large and/or perform complex tasks, the overhead of the function call is usually
insignificant compared to the amount of time the function takes to run. However, for small, commonly-used
functions, the time needed to make the function call is often a lot more than the time needed to actually execute
the function’s code. This overhead occurs for small functions because execution time of small function is less
than the switching time.
C++ provides an inline functions to reduce the function call overhead.
Inline function in C++ is an enhancement feature that improves the execution time and speed of the program.
In case of inline functions, the compiler does not go through the above process of switching between the stack and
calling function. Instead, it copies the definition of the inline function and the calling instruction with that definition.
When the inline function is called whole code of the inline function gets inserted or substituted at the point of
inline function call.
This substitution is performed by the C++ compiler at compile time. Inline function may increase efficiency if it
is small. it only uses in small program.
Syntax:
inline return-type function-name(parameters)
{
// function code
}
Ex:
inline int cube(int s)
{
return s*s*s;
}
int main()
{
Template :-
Template are the foundation of generic programming, which involves writing code in a way that is independent of
any particular type.
A template is a blueprint or formula for creating a generic class or a function.
It allows you to define the generic classes and generic functions and thus provides support for generic programming.
Generic programming is a technique where generic types are used as parameters in algorithms so that they can work
for a variety of data types.
Why templates:- to Don’t repeat yourself, to make generic programming(not specific but general for all data type)
Templates can be represented in two ways:
Function templates
Class templates
Function Template
A function can be also used as a template function that helps to make a generic function.
It define a set of operations that can be applied to the various type of data.
A generic function is created by using the keyword template. The template defines what function will do.
Where T type is a placeholder name for a datatype used by the function. it is used within the function definition.
And a class keyword is used to specify a generic type in a template declaration.
Generic programming which involves writing code in a way that is independent of any particular type.
Syntax:- template < class T>
ret_type func_name(parameter_list)
{
// body of function
}
Ex:
template <class T>
T show(T a, T b)
{
cout<<" a is: "<<a<<endl<<"b is: "<<b<<endl;
}
int main()
{
int x=23,y=30;
char m='r',n='s';
float e=10.45,f=12.3;
show(x,y);
show(m,n);
show(e,f);
return 0;
}
Output:- a is 23
b is 30
Class Template
A class keyword is used to specify a generic type in a template declaration.
Class can also be declared to operate on different datatypes. such class are called class templates.
we can define function templates, we can also define class templates. The general form of a generic class declaration
is shown here −
template <class type>
class class-name
{
.
.
.
};
Here, type is the placeholder type name, which will be specified when a class is instantiated. You can define more
than one generic data type by using a comma-separated list.
We need a class implementation that is same for all classes, only the data types used are different.
Normally we need to create a different class for each data type or create different member variables and functions
within a single class.
In class templates we write a class that can be used for different data types.
Ex:-template<class T>
class A
{
public:
T a,b;
T add()
{
Array:------------------------------------------------------------------------------------------------
It is a group of variables of similar data types referred to by a single element.
Its elements are stored in a contiguous memory location.
The size of the array should be mentioned while declaring it.
Array elements are always counted from zero (0) onward.
Array elements can be accessed using the position of the element in the array.
The array can have one or more dimensions.
Declaring Arrays
To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an
array as follows −
type arrayName [ arraySize ];
Accessing Array Elements
An element is accessed by indexing the array name. This is done by placing the index of the element with in square brackets
after the name of the array. For example −
Cout<<balance[9];
int main()
{
// Array declaration by specifying size and initializing
// elements
int arr[6] = { 10, 20, 30, 40 };
return 0;
}
WAP to display and sum marks of five student using oneone-D array
int main()
{
int num[5],i,sum=0;
cout<<"enter marks of student: "<<endl;
for(i=0;i<5;i++)
{
cin>>num[i];
}
for(i=0;i<5;i++)
{
cout<<"Students marks are: "<<num[i]<<endl;
sum=sum+num[i];
}
cout<<"sum is: "<<sum;
return 0;
}
EXAMPLE:-
#include <iostream>
#include <string>
using namespace std;
int main() {
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0];//volvo
return 0;
}
Types of Array:-----------------------------------------------------------------------------------------------------------------------------
1-D array:- A One-Dimensional Array in C++ programming is a special type of variable that can store multiple values of
only a single data type such as int, float, double, char, structure, pointer, etc. at a contagious location in computer memory.
0 1 2 3 4
Size of array=5
Index of array is:- 0,1,2,3,4
First index:- 0(lower bound)
Last index:-4(upper bound)
The first index is called Lower Bound, and the last index is called an Upper Bound. Upper Bound of a one dimensional is
always Size – 1.
Store Numbers in a One Dimensional Array:- To store the number in each cell of the array we can use the
following syntax.
array_name[index]=value;
a[0]=26;
a[1]=15;
a[2]=34;
But, the elements in an array can be explicitly initialized to specific values when it is declared, by enclosing those initial
values in braces {}. For example:
int foo [5] = { 16, 2, 77, 40, 12071 };
The number of values between braces {} shall not be greater than the number of elements in the array. For example, in the
example above, foo was declared having 5 elements (as specified by the number enclosed in square brackets, []), and the
braces {} contained exactly 5 values, one for each element. If declared with less, the remaining elements are set to their
default values (which for fundamental types, means they are filled with zeroes). For example:
int bar [5] = { 10, 20, 30 };
When an initialization of values is provided for an array, C++ allows the possibility of leaving the square brackets empty[]. In
this case, the compiler will assume automatically a size for the array that matches the number of values included between the
braces {}:
int foo [] = { 16, 2, 77, 40, 12071 };
We can access any number stored in a 1D array using the following syntax.
Syntax:- array_name[index];
ex:- cout<<a[0]<<" "<<a[1]<<" "<<a[2];
output:-26 15 34
ex:-
int main()
{
int a[10], i;
cout<<"Enter 10 numbers\n";
for(i=0; i<10; i++)
{
cin>>a[i];
}
{
cout<<a[i]<<" ";
}
}
return 0;
}
cout<<" "<<a[i];
}
return 0;
2D array:-
A two-dimensional array can be termed as an array of arrays that stores homogeneous data in tabular form. Data in
multidimensional arrays are stored in row-major order.
Two square brackets are used. first square bracket represent row no and second represent column no.
Two D array is also called matrix.
Syntax:-datatype arr_name[rowsize][columnsize];
Initializing Two – Dimensional Arrays: There are various ways in which a Two-Dimensional array can be initialized.
First Method:
int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}
The above array has 3 rows and 4 columns. The elements in the braces from left to right are stored in the table also from
left to right. The elements will be filled in the array in order, the first 4 elements from the left in the first row, the next 4
elements in the second row, and so on.
Second Method:
int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};
int main()
{
// an array with 3 rows and 2 columns.
int x[3][2] = {{0,1}, {2,3}, {4,5}};
return 0;
}
Multi-D array:-
A multidimensional array is an array with more than one dimension. It is the homogeneous collection of items where each
element is accessed using multiple indices.
For example:
int arr1[2][4];
The array int arr1[2][4] can store total (2*4) = 8 elements.
In C++ int data type takes 4 bytes and we have 8 elements in the array ‘arr1’ of the int type.
Total size = 4*8 = 32 bytes.
int arr2[2][4][8];
Array int arr2[2][4][8] can store total (2*4*8) = 64 elements.
The Total size of ‘arr2‘ = 64*4 = 256 bytes.
When an array is passed to a function using array name only then this is known as passing array to a function.
Since array name is pointer variable which holds the address of the first element in the array list. therefore a pointer
variable is required in the function definition.
Syntax: fun_name(arr_name);//passing array to function.
Datatype fun_name(datatype *variable)//function declaration.
Ex:
void show(int *a);
void show(int *a)
{
cout<<*a;
}
int main()
{
int a[5]={1,2,3,4,5};
show(a);
return 0;
}
Typedef:-
It is a keyword in C++ is used for aliasing existing datatypes, userdefined datatypes,and pointers to a more
meaningful name.
It allows us to give descriptive names to standard datatypes, which can also help us self-document our
code.
Mostly typedefs are used for aliasing , only if the predefined name is too long or complex to write again
and again.
Syntax:-
It is used for direct member selection via the name of variables of type class, struct, union. It is also known
as the direct member access operator which helps us to extract the value or the function associated with the
particular structure, union, and class.
Syntax:- Variable_name.member;
It allows us to access elements in structure and unions. It is used with a pointer variable pointing to a class,
structure or union.
Syntax:- (pointer_name) (variable_name);
PRACTICAL TEST QUESTIONS(UNIT-2)
25. Write a C++ program to display details of 5 students’ detail should be contained student
name, roll number, marks using structure.
26. Write a C++ program to create a structure named “Date” which contains three members Day,
Month, Year and display current date entering by the user using function definition.
27. Write a C++ program to demonstrate enum with switch case.
28. Write a C++ program to create an enum having number of enum list or elements and count
the size of elements inside the enum.
29. Write a C++ program to swap two values without using third variable.
30. Write a C++ program to swap two values using third variable of call by address function
invoking.
31. Write a C++ program to illustrate working of call by value of a function invoking.
32. Write a C++ program to illustrate working of call by reference method of a function
invoking.
33. Write a C++ program to calculate simple interest using default arguments.
34. Write a C++ program using function template to add two integers and two float number.
35. Write a C++ program to create simple calculator using class templates.
36. Write a C++ program using inline function to calculate area of circle.
37. Write a C++ program to demonstrate function overloading.
38. Write a C++ program to find the size of 1-D, 2-D and multidimensional array.
39. Write a C++ program create and display one-D array of size 7 and also display average of all
the elements.
40. Write a C++ program to input 5 numbers in an array and print all the numbers from the
backside of the array.