0% found this document useful (0 votes)
105 views13 pages

Practical File074

The document is a practical list for the subject "Object Oriented Programming with C++" that includes 7 coding practices. It provides the program code, output, and conclusions for each practice. The practices cover basics of cout and endl manipulator, use of setw, controlling floating point precision, recursion to sum array elements, errors in reference variable usage, and using scope resolution operator. It also includes questions about concepts covered and their explanations.

Uploaded by

Naman Ghevariya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views13 pages

Practical File074

The document is a practical list for the subject "Object Oriented Programming with C++" that includes 7 coding practices. It provides the program code, output, and conclusions for each practice. The practices cover basics of cout and endl manipulator, use of setw, controlling floating point precision, recursion to sum array elements, errors in reference variable usage, and using scope resolution operator. It also includes questions about concepts covered and their explanations.

Uploaded by

Naman Ghevariya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Object Oriented Programming with C++ [CE144]

ID No.:19DCE074

CHAROTAR UNIVERSITY OF SCIENCE & TECHNOLOGY

DEVANG PATEL INSTITUTE OF ADVANCE TECHNOLOGY & RESEARCH

Department of Computer Engineering

Subject Name: Object Oriented Programming with C++


Semester: II
Subject Code: CE144
Academic year: 2019-20
Practical List
No. Aim of the Practical

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 : ??? *

* Due Date: Thursday, Dec. 20 *

******************************

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>

using namespace std;

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:

CONCLUSION: From this Practical We understand use of setw for creating


table.

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:

CONCLUSION: From This Practical We Understand Use of fixed, scientific and


setprecision () manipulators for controlling the precision of floting point numbers.

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.

QUESTION:- Show stepwise solution of winding and unwinding phase of recursion.

ANSWER:- we have n[]={2,3,4,10,5.2} and b=5;


WINDING PHASE:
Step1: s+=n[5]+sum(n,4); That is s+=5.2+sum(n,4).
Step2: s+=n[4]+sum(n,3); That is s+=10+sum(n,3).
Step3: s+=n[3]+sum(n,2); That is s+=4+sum(n,2).
Step4: s+=n[2]+sum(n,1); That is s+=3+sum(n,1).
Step5: s+=n[1]+sum(n,0); That is s+=2+sum(n,0).
Step6: return 0;

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.

QUESTION:- Can we declare an array of references? Can we assign NULL value to


reference variable? Is Reference variable a pointer variable? Can we declare a reference
variable without initializing it? Does Reference Variable change the original value of
variable?
ANSWER:-
(i) We cannot declare array of reference variable.
(ii) We cannot assign null value to it.
(iii) No reference variable are not pointer variable.
(iv) We cannot declare reference variable without initializing.
(v) Yes we can change the original variable using reference variable.

7
Object Oriented Programming with C++ [CE144]
ID No.:19DCE074

6 Find output of the following code:


#include<iostream>
#include<conio.h>
Using namespace std;
int m=30;
int main()
{ int m=20;
{ int m=10;
cout<<”we are in inner block”<<endl;
cout<<”value of m=”<<m<<”\n”;
cout<<”value of ::m=”<<::m<<”\n”; }
cout<<”we are in outer block”<<endl;
cout<<”value of m=”<<m<<”\n”;
cout<<”value of ::m=”<<::m<<”\n”;
getch();
return 0; }
OUTPUT:

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

You might also like