Sei sulla pagina 1di 18

Object Oriented Programming with C++ [CE142] Enrolment No.

:18DCE117

CHAROTAR UNIVERSITY OF SCIENCE & TECHNOLOGY

DEVANG PATEL INSTITUTE OF ADVANCE TECHNOLOGY & RESEARCH

Department of Computer Engineering/Computer Science & Engineering/

Information Technology

Subject Name: Object Oriented Programming with C++


Semester: II
Subject Code: CE142
Academic year: 2018-19

Practical List
No. Aim of the Practical
1.
Write a C++ program that will output this passage by Deepak Chopra. 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 *
******************************
Question: Difference between \n and endl.

PROGRAM CODE:

#include<iostream>
using namespace std;
main()
{
string s;
cout<<"Enter author name :";

1
Object Oriented Programming with C++ [CE142] Enrolment No.:18DCE117

cin>>s;
cout<<"\t\t\t**********************************"<<endl;
cout<<"\t\t\t* Programming Assignment 1 *"<<endl;
cout<<"\t\t\t* Computer Programming 1 *"<<endl;
cout<<"\t\t\t* Author:"<<s<<" *"<<endl;
cout<<"\t\t\t* Due Date: Thursday, Dec. 20 *"<<endl;
cout<<"\t\t\t**********************************"<<endl;
cout<<"\nMade by : Aryan Vataliya";
cout<<"\nID : 18dce139";

OUTPUT:

CONCLUSION:
In this program I have learned how to use cout, cin and manipulator endl.

Write a program to create the following table. Use endl and setw manipulator.

2
Object Oriented Programming with C++ [CE142] Enrolment No.:18DCE117

2.

PROGRAM CODE:

#include<iostream>
#include<iomanip>
using namespace std;
main()
{
int i,j,a;

for(i=1;i<5;i++)
{
for(j=1;j<5;j++)
{
a=i*j;
cout<<setw(4)<<a;
}
cout<<"\n";
}
cout<<"\nMade by : Aryan Vataliya";
cout<<"\nID : 18dce139";
}

3
Object Oriented Programming with C++ [CE142] Enrolment No.:18DCE117

OUTPUT:

CONCLUSION:
Here I have learned how to use endl and setw manipulator.

4
Object Oriented Programming with C++ [CE142] Enrolment No.:18DCE117

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 CODE:

#include<iostream>
#include<iomanip>
using namespace std;
main()
{
float a,b,*c,*d,sum;
cout<<"Enter two no.";
cin>>a>>b;
c=&a;
d=&b;
sum=*c+*d;
cout<<"sum is "<<scientific<<setprecision(2)<<sum<<endl;
cout<<"sum is "<<fixed<<setprecision(2)<<sum<<endl;
cout<<"\nMade by : Aryan Vataliya";
cout<<"\nID : 18dce139";

OUTPUT:

CONCLUSION:
Here I have learned how to use setprecision manipulator.

5
Object Oriented Programming with C++ [CE142] Enrolment No.:18DCE117

Write a C++ program to find out sum of array element using Recursion.
4. Question: Show stepwise solution of winding and unwinding phase of
recursion

PROGRAM CODE:
#include<iostream>
using namespace std;
int add(int num[],int n);
main()
{
int n,i,a[10],sum=0;
cout<<"Enter the no of array:";
cin>>n;
cout<<"Enter the elements of array:";
for(i=1;i<=n;i++)
cin>>a[i];
sum=add(a,0);
cout<<endl<<"sum of arrays="<<add(a,n)<<endl<<endl;
cout<<"\nMade by : Aryan Vataliya";
cout<<"\nID : 18dce139";

}
int add(int num[],int n)
{
if(n<=0)
return 0;
else
return (num[n]+add(num,n-1));
}

6
Object Oriented Programming with C++ [CE142] Enrolment No.:18DCE117

OUTPUT:

CONCLUSION:
Here we found out the sum of array using winding and unwinding step of recursion.

7
Object Oriented Programming with C++ [CE142] Enrolment No.:18DCE117

Write a C++ program to find the number of vowels present in the given character
array using pointer arithmetic.

PROGRAM CODE:
#include<iostream>
#include<iomanip>
using namespace std;
main()
{
char str[10];
int i=0,c=0;
char *pt;
cout<<"Enter a string :";
cin.getline(str,sizeof(str));
pt=str;
while(*pt != '\0')
{
if(*pt=='a' || *pt=='e' || *pt=='i' || *pt=='o' || *pt=='u' || *pt=='A' || *pt=='E' || *pt=='I' || *pt=='O' ||
*pt=='U')
c++;
i++;
pt++;
}
cout<<"length is "<<i<<endl;
cout<<"vowels are "<<c<<endl;
cout<<"Consonant "<<(i-c)<<endl<<endl;
cout<<"\nMade by : Aryan Vataliya";
cout<<"\nID : 18dce139";
}

OUTPUT:

8
Object Oriented Programming with C++ [CE142] Enrolment No.:18DCE117

5.

CONCLISION:
In this practical I have learned who to use pointer arithmetic to find number of vowels in a character
array.

Find error in the following code and give reasons for each error:

9
Object Oriented Programming with C++ [CE142] Enrolment No.:18DCE117

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?

PROGRAM CODE(ERROR):
#include<iostream>
using namespace std;
int main()
{
int no1=10, no2=12;
int & x=no1;
int & r;
int & c = NULL;
int & d[2] = {no1,no2};
cout<<"x = "<< x+20;
cout<<"no1="<< no1+10;
return 0;
}

OUTPUT(ERROR):

PROGRAM CODE:
#include<iostream>
using namespace std;
int main()
{
int no1=10, no2=12;
int & x=no1;
int & r=no2;
//int & c = NULL;
int & d[2] = {no1,no2};

10
Object Oriented Programming with C++ [CE142] Enrolment No.:18DCE117

int & d1 = no1;


int & d2 = no2;
6. cout<<"x = "<< x+20<<endl;
cout<<"no1="<< no1+10<<endl<<endl;
cout<<"\nMade by : Aryan Vataliya";
cout<<"\nID : 18dce139";
return 0;
}

OUTPUT:

CONCLUSION:
Here in this practical I have solved the errors that it had and learned from the
following question answers.

Que 1: Can we declare an array of references?


Ans 1:No we cannot , because

Que 2: Can we assign NULL value to reference variable?


Ans 2: We cannot have NULL reference. We must always be able to assume that a
reference is connected to a legitimate(reasonable) piece of storage.

Que 3: Is Reference variable a pointer variable?


Ans 3:No a reference variable is not a pointer variable , because

11
Object Oriented Programming with C++ [CE142] Enrolment No.:18DCE117

Que 4: Can we declare a reference variable without initializing it?


Ans 4: A reference must be initialized when its created.

Que 5 : Does Reference Variable change the original value of variable?


Ans 5 : Yes as we change the reference variable the original value of variable will be
changed.

Find output of the following code: Explain how scope Resolution operator is used
to access global version of a variable.

12
Object Oriented Programming with C++ [CE142] Enrolment No.:18DCE117

PROGRAM CODE:
#include<iostream.h>
#include<conio.h>
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”;
cout<<"Name : Meet Shah"<<endl;
cout<<"ID : 18DCE117";
getch();
return 0;
}

OUTPUT:

CONCLUSION:
In this program I have learned about scope resolution operator.

Find Error in the following code of a program and give explanation why these errors

13
Object Oriented Programming with C++ [CE142] Enrolment No.:18DCE117

exist.
7.
CONSTANT POINTER

PROGRAM CODE:(ERROR)
#include <iostream>
using namespace std;
int main()
{
int var1 = 35,var2 = 20;
int *const ptr = &var1;
ptr = &var2;
cout<<"var1= "<<*ptr;
return 0;
}

OUTPUT:(ERROR)

PROGRAM CODE:
#include <iostream>
using namespace std;
int main()
{
int var1 = 35,var2 = 20;
int *const ptr = &var1;
//ptr = &var2;
cout<<"var1= "<<*ptr<<endl<<endl;
cout<<"Name : Meet Shah"<<endl;
cout<<"ID : 18DCE117";
return 0;
}

14
Object Oriented Programming with C++ [CE142] Enrolment No.:18DCE117

OUTPUT:

8.

POINTER TO CONSTANT

PROGRAM CODE:(ERROR)
#include <iostream>
using namespace std;
int main()
{
int var1 = 43;
const int* ptr = &var1;
*ptr = 1;
var1=34;
cout<<"var1 = "<< *ptr;
return 0;
}

OUTPUT:(ERROR)

PROGRAM CODE:
#include <iostream>
using namespace std;
int main()
{

15
Object Oriented Programming with C++ [CE142] Enrolment No.:18DCE117

int var1 = 43;


const int* ptr = &var1;
//*ptr = 1;
var1=34;
cout<<"var1 = "<< *ptr<<endl<<endl;
cout<<"Name : Meet Shah"<<endl;
cout<<"ID : 18DCE117";
return 0;
}
OUTPUT:

CONSTANT POINTER TO A CONSTANT

PROGRAM CODE:(ERROR)
#include <iostream>
using namespace std;
int main()
{
int var1 = 0,var2 = 0;
const int* const ptr = &var1;
*ptr = 1;
ptr = &var2;
cout<<"Var1 = "<<*ptr;
return 0;
}

OUTPUT:(ERROR)

16
Object Oriented Programming with C++ [CE142] Enrolment No.:18DCE117

PROGRAM CODE:
#include <iostream>
using namespace std;
int main()
{
int var1 = 0,var2 = 0;
const int* const ptr = &var1;
//*ptr = 1;
//ptr = &var2;
cout<<"Var1 = "<<*ptr<<endl<<endl;
cout<<"Name : Meet Shah"<<endl;
cout<<"ID : 18DCE117";
return 0;
}

OUTPUT:

CONCLUSION:
In this program I have learnd the concept of constant to pointer , pointer to constant

17
Object Oriented Programming with C++ [CE142] Enrolment No.:18DCE117

and constant pointer to a constant.

18

Potrebbero piacerti anche