Practical File074
Practical File074
ID No.:19DCE074
1. Write a C++ program that will print output in the following form. Make sure your
output looks exactly as shown here (including spacing, line breaks, punctuation, and
the title and author). Use cout and cin objects and endl manipulator.
******************************
* Programmimg Assignment 1 *
* Computer Programmimg I *
* Author : ??? *
******************************
PROGRAM :
#include<iostream>
using namespace std;
int main()
{
int i;
1
Object Oriented Programming with C++ [CE144]
ID No.:19DCE074
for(i=0;i<=30;i++)
cout<<"*";
cout<<endl<<"* Programming Assigment 1 *"<<endl;
cout<<"* Computer Programming I *"<<endl;
cout<<"* Author: ???\t *"<<endl;
cout<<"* Due Date: Thurshday,Dec.20 *"<<endl;
for(i=0;i<=30;i++)
cout<<"*";
cout<<endl;
return 0;
}
OUTPUT:
CONCLUSION:
We have concluded the use of endl, cout manipulator.
QUESTION:- Difference between \n and endl.
ANSWER:-
1. Endl causes the output buffer to be flushed where \n does not.
2. Endl is Manipulator which are instructions to the output stream that modify the
output in various ways. While \n is not manipulator.
2
Object Oriented Programming with C++ [CE144]
ID No.:19DCE074
2 Write a program to create the following table. Use endl and setw manipulator.
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
PROGRAM :
#include<iostream>
#include<iomanip>
int main()
{ int i;
for(i=1;i<5;i++)
{ cout<<"--------------"<<endl<<"|"<<i*1<<setw(3)<<"|"<<i*2<<setw(3)<<"|"<<i*3
<<setw(3)<<"|"<<i*4<<setw(3)<<"|"<<endl; }
for(i=1;i<18;i++)
cout<<"-";
return 0;}
OUTPUT:
3
Object Oriented Programming with C++ [CE144]
ID No.:19DCE074
3 Write a C++ program to add two floating numbers using pointer. The result should
contain only two digits after the decimal. Use fixed, scientific and setprecision ()
manipulators for controlling the precision of floating point numbers.
PROGRAM :
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float *n1,*n2,x,y,sum;
cout<<"enter the First Number: ";
cin>>x;
cout<<"enter the Second Number: ";
cin>>y;
n1=&x;
n2=&y;
sum=*n1+*n2;
cout.precision(2);
cout<<"the sum with fixed manipulators is: "<<fixed<<sum<<endl;
cout<<"the sum with scientific manipulators is: "<<scientific<<sum<<endl;
return 0;
}
OUTPUT:
4
Object Oriented Programming with C++ [CE144]
ID No.:19DCE074
4 Write a C++ program to find out sum of array element using Recursion.
PROGRAM:
#include<iostream>
using namespace std;
float sum(float n[],int);
int main()
{
float num[5],add;
cout<<"enter any five number of array to sum: "<<endl;
cin>>num[0]>>num[1]>>num[2]>>num[3]>>num[4];
add=sum(num,4);
cout<<"the sum of element of array is: "<<add<<endl;
return 0;
}
float sum(float n[],int b)
{
float s=0;
if(b<0)
return s;
s+=n[b]+sum(n,b-1);
return s;
}
OUTPUT:
5
Object Oriented Programming with C++ [CE144]
ID No.:19DCE074
CONCLUSION:
From this Practical We understand Use of recursion in c++ for adding elements of
array.
UNWIDING PHASE:
Step7: 2+0.
Step8: 3+2+0.
Step9: 4+3+2+0.
Step10: 10+4+3+2+0.
Step11: 5.2+10+4+3+2+0.
Step12: Return 24.2.
6
Object Oriented Programming with C++ [CE144]
ID No.:19DCE074
5 Find error in the following code and give reasons for each error:
#include<iostream>
using namespace std;
int main()
{ int no1=10, no2=12;
int & x=no1;
int & r; //declare a reference variable without initializing it
int & c = NULL; //assign NULL value to reference variable
int & d[2] = {no1,no2}; //declare an array of references
cout<<"x = "<< x+20; cout<<"no1="<< no1+10;
return 0; }
OUTPUT:
CONCLUSION:
From this pratical we conclude the use of reference variable.
7
Object Oriented Programming with C++ [CE144]
ID No.:19DCE074
CONCLUSION:
From this practical we concluded the use of scope resolution operator (::) for having
different value of same variable declared as global and local variable.
QUESTION:- Explain how scope Resolution operator is used to access global
version of a variable.
ANSWER:-
8
Object Oriented Programming with C++ [CE144]
ID No.:19DCE074
7
Find Error in the following code of a program and give explanation why these errors
exist.
1. #include <iostream>
using namespace std;
int main()
{ int var1 = 35,var2 = 20;
int *const ptr = &var1;
ptr = &var2; //changing address of constant pointer variable.
cout<<"var1= "<<*ptr;
return 0; }
OUTPUT:
Explanation: Here pointer variable has prefix const means it is constant pointer
whose address once assign cannot be change.so error occur as we try to change
address of assign to pointer variable.
2. #include <iostream>
using namespace std;
int main()
{
int var1 = 43;
const int* ptr = &var1;
*ptr = 1; //changing value of pointer to constant variable.
var1=34;
cout<<"var1 = "<< *ptr;
return 0;
}
OUTPUT:
9
Object Oriented Programming with C++ [CE144]
ID No.:19DCE074
Explanation: Here pointer variable of type integer have prefix to int const which
declare a pointer to constant in which the value of variable assign to pointer cannot be
change using pointer. Thus keeping it’s value constant. so error occur when we try to
change value of variable using pointer to constant variable.
3. #include <iostream>
using namespace std;
int main()
{ int var1 = 0,var2 = 0;
const int* const ptr = &var1;
*ptr = 1; //changing value of constant pointer to constant variable.
ptr = &var2; //changing value of constant pointer to constant variable.
cout<<"Var1 = "<<*ptr;
return 0; }
OUTPUT:
Explanation: Here pointer variable is constant pointer to constant which means once
we assign the address to pointer variable we cannot change it and also the value of
variable whose address is assign to pointer cannot be change using pointer.so error
occur when we try to change address and value of pointer variable ptr.
10
Object Oriented Programming with C++ [CE144]
ID No.:19DCE074
8 Write a program to enter a size of array. Create an array of size given by user using
“new” Dynamic memory management operator (free store operator). Enter the data to
store in array and display the data after adding 2 to each element in the array. Delete
the array by using “delete” memory management operator.
PROGRAM :
#include<iostream>
using namespace std;
int main()
{
int a[100],n,i,*p;
p=new int[n];
cout<<"Enter the number what you want to addition:"<<endl;
cin>>n;
cout<<"Enter The Numbers:"<<endl;
for(i=0;i<n;i++)
{ cin>>a[i]; }
for(i=0;i<n;i++)
{ *p=a[i]+2;
cout<<*p; }
cout<<endl;
cout<<"MORADIYA TIRTH V."<<endl;
cout<<"19DCE074";
}
OUTPUT:
11
Object Oriented Programming with C++ [CE144]
ID No.:19DCE074
9 Find the output of following program. Explain the use of bool data type.
PROGRAM :
#include<iostream>
using namespace std;
int main()
{
bool a = 321, b;
cout << "Bool a Contains : " << a<<endl;
int c = true;
int d = false;
cout<<"c = "<<c <<endl<<"d = "<<d; c = a + a;
cout << "\nInteger c contain : " << c; b = c + a;
cout << "\nBool b contain : " <<b;
return 0;
}
OUTPUT:
12
Object Oriented Programming with C++ [CE144]
ID No.:19DCE074
10 Define three functions named divide(). First function takes numerator and
denominator as an input argument and checks it’s divisible or not, Second function
takes one int number as input argument and checks whether the number is prime or not
and Third function takes 3 float number as argument and finds out average of the
numbers. Use concept of Function Overloading / static binding.
PROGRAM :
13