Final)
-
Upload
ramkrishna-reddy-b -
Category
Documents
-
view
72 -
download
0
Transcript of Final)
INSTITUTE OF AERONAUTICAL ENGINEERINGDUNDIGAL
Oops through c++
LAB COURSE FILE
FOR MCA I/I SEM
PREPARED BY,
V.SREE KANTHA BABU,
ASST. PROF (MCA DEPT)
1
1.Write a C++ program to find both the largest and smallest number in a list of integers. #include<iostream.h> #include<conio.h> int main() { clrscr(); int *a,n; cout<<"Enter how many number are there ?\t"; cin>>n; a=new int[n]; cout<<"Enter Numbers"<<endl; for(int i=0;i<n;i++) cin>>a[i]; int temp; for(i=0;i<n;i++) for(int j=i+1;j<n;j++) if(a[i]>a[j]) { temp=a[j]; a[j]=a[i]; a[i]=temp; } cout<<"The Minimum Number is \t"<<a[0]<<endl; cout<<"The Maximum Number is \t"<<a[n-1]<<endl; getch(); return 0; }
Output:Enter how many number are there:5Enter Numbers15 2 34 56 78The Minimum Number is 2The Maximum Number is 78
2
2. Write a C++ program to find the sum of individual digits of a positive integer.
#include<iostream.h> #include<conio.h>
void main() { clrscr(); cout<<"Enter The Number\t"; long signed n,temp,sum=0; cin>>n;
while(n>0) {
temp=n%10; sum=temp+sum; n=n/10;}
cout<<"The sum of individuval digits" <<"of a entered positive integer is\t"<<sum; getch(); }
Output:
Enter The Number23
The sum of individuval digits a entered positive integer is 5
3
3. Write a C++ program to generate all the prime numbers between 1 and n , where n is a value supplied by the user.
#include<iostream.h> #include<conio.h> int prime(unsigned long int ); int main() { clrscr(); cout<<"Prime numbers between 1 to \t"; unigned long int n; cin>>n; for(unsigned long int i=2;i<=n;i++) if(prime(i)==1)
cout<<i<<" ";getch();return 0;
}
int prime(unsigned long int x) { for(unsigned long int i=2;i<=x;i++)
{ if(x%i==0&&i<x)
{ return 0; } }
return 1; }
Output:Prime numbers between 1 to 101 3 5 7
4
4. Write a C++ program to sort a list of numbers in ascending order. #include<iostream.h> #include<conio.h> int order(int &,int &); int main() { clrscr(); int *a,k; cout<<"How many number are there\t"; cin>>k; a=new int[k]; cout<<"Enter numbers"<<endl; for(int i=0;i<k;i++) cin>>a[i]; for(i=0;i<k;i++) {cout<<" "; for(int j=i+1;j<k;j++) {if(a[i]>a[j]) swap(a[i],a[j]);} cout<<a[i];} getch(); return 0; } int order(int &a,int &b) { int temp; temp=a; a=b; b=temp; return a,b; }
Output:How many number are there 5Enter numbers 12 45 79 06 90
The order of sorting array is:06 12 45 79 90
5
5. Write a C++ program to find out swapping of two integers. #include<iostream.h>#include<conio.h>void swap(int ,int );void swap(float ,float );void swap(char ,char );int main(){ clrscr(); swap(2,5); float a=20.5,b=30.26; swap(a,b); swap('a','x'); getch(); return 0;}
void swap(int a,int b){ int temp;
cout<<"Before swaping\t"<<a<<" "<<b<<endl; temp=a; a=b; b=temp;
cout<<"After swaping\t"<<a<<" "<<b<<endl;}
void swap(float a,float b){ float temp;
cout<<"Before swaping\t"<<a<<" "<<b<<endl; temp=a; a=b; b=temp;
cout<<"After swaping\t"<<a<<" "<<b<<endl;}
Output:Before swaping 2,5After swaping 5,2Before swaping 20.5 30.26After swaping 30.26 20.5
6
6.Write a c++ program to find number is Armstrong Number or not
#include<stdio.h>#include<conio.h>#include<math.h>
void main(){ int no,dig,temp,sum=0;
clrscr();
printf("Enter no : "); scanf("%d",&no);
temp=no;
while(temp>0) { dig=temp%10;
sum=sum+dig*dig*dig;
temp=temp/10; }
if (no==sum) printf("\n %d is an Armstrong Number",no); else printf("\n %d is not an Armstrong Number",no);
getch();
Output:
Enter no: 153153 is an Armstrong Number
7
7. Write a C++ program to generate the first n terms of the fiboncci sequence.
#include<iostream.h> #include<conio.h>
void main() {
clrscr(); cout<<"How many terms to be generated ?\t"; double k; cin>>k;
double f1=0,f2=1,f3;
cout<<f1<<" "<<f2; f3=f1+f2; for(double i=1;i<=k-2;i++) {
cout<<" "<<f3; f1=f2; f2=f3; f3=f1+f2;}getch();}
Output:
How many terms to be generated 100 1 1 2 3 5 8 13 21 34
8
8.Write a C++ program to implement the matrix ADT using a class. The operations supported by this ADT are:
a) Reading a matrix. c) Addition of matrices.b) Printing a matrix. d) Subtraction of matrices.e) Multiplication of matrices.
#include<iostream.h> #include<conio.h> struct mo
{ signed p,q,r,s,b,choice; }; int matrixr(int,struct mo); mo order(int); class matrix { int **a; signed m,n; public: matrix (){}; matrix(int,int); void readmatrix(); void printmatrix(); matrix addmatrix(matrix a,matrix b); matrix submatrix(matrix a,matrix b); matrix mulmatrix(matrix a,matrix b); ~matrix(){}; }; matrix::matrix(int x,int y) { m=x;n=y; a=new int*[m]; for(int i=0;i<m;i++) a[i]=new int[n]; } void matrix::readmatrix() { cout<<"Enter Matrix elements"<<endl; for(int i=0;i<m;i++) for(int j=0;j<n;j++) cin>>a[i][j];
9
} void matrix::printmatrix() { cout<<"The result Matrix is "<<endl; for(int i=0;i<m;i++) { { for(int j=0;j<n;j++) { cout.width(3); cout<<a[i][j];} } cout<<endl;}} matrix matrix::addmatrix(matrix a,matrix b) {
matrix c(a.m,a.n); for(int i=0;i<m;i++) for(int j=0;j<n;j++) c.a[i][j]=a.a[i][j]+b.a[i][j]; return c;
} matrix matrix::submatrix(matrix a,matrix b) { matrix c(a.m,a.n); for(int i=0;i<a.m;i++) for(int j=0;j<a.n;j++) c.a[i][j]=a.a[i][j]-b.a[i][j]; return c; } matrix matrix::mulmatrix(matrix a,matrix b) { matrix c(a.m,b.n); for(int i=0;i<a.m;i++) for(int j=0;j<b.n;j++) {c.a[i][j]=0; { for(int k=0;k<a.n;k++) c.a[i][j]+=a.a[i][k]*b.a[k][j];}} return c; } int main() { clrscr(); int a; struct mo k;
10
do { cout<<"1.Addition of two Matrices"<<endl; cout<<"2.Subtraction of two Matrices"<<endl; cout<<"3.Multiplication of two Matrices"<<endl; cout<<"Enter your chioice\t"; cin>>a; if(a>=0&&a<4) {k=order(a); if(!k.b==0) {if(k.choice==(1||2)) a=matrixr(a,k); else a=matrixr(a,k); }} }while(a>=0&&a<4); return 0; } int matrixr(int f,mo k) { matrix a(k.p,k.q),b(k.r,k.s),c; a.readmatrix(); b.readmatrix(); switch(f) { case 1: { c=a.addmatrix(a,b); c.printmatrix(); } break; case 2: {c=a.submatrix(a,b); c.printmatrix(); } break; case 3: { c=a.mulmatrix(a,b); c.printmatrix(); } }return 0; } mo order(int a) {
11
struct mo s; s.choice=a; cout<<"Enter 1st Matirx order"; cin>>s.p>>s.q; cout<<"Enter 2nd Matrix order"; cin>>s.r>>s.s; if(a==1||a==2) { if(s.p==s.r&&s.q==s.s) return s; else s.b=0; cout<<"Matrix addition/subtraction is not possible"<<endl; return s; } if(s.q==s.r||(s.p==s.r&&s.q==s.s)) return s; else s.b=0; cout<<"Matrix Multiplicatin is not possible"<<endl; return s; }
12
9. Write a C++ program to generate Pascal’s triangle. #include<iostream.h> #include<conio.h> long unsigned fact(long unsigned); long unsigned ncr(long unsigned ,long unsigned ); int main() { clrscr(); for(long unsigned i=0,k=35;i<12;i++,k=k-3) { for(long unsigned j=0,l=k;j<=i;j++,l=l+6) { gotoxy(l,i+1); cout.width(3); cout<<ncr(i,j); }cout<<endl<<endl; } getch(); return 0; } long unsigned ncr(long unsigned n,long unsigned r) { long unsigned x=(fact(n)/(fact(r)*fact(n-r))); return x; } long unsigned fact(long unsigned n) { long unsigned x=1; if(n==1||n==0) return x; else x=n*fact(n-1); return x; }
Output:
11 1
1 2 11 3 3 1
1 4 6 4 1
13
10. Write a C++ program that uses a recursive function for solving Towers of Hanoi problem. #include<iostream.h> #include<conio.h>
void diskchange(char l,char r,char c,int n) {
if(n>0){
diskchange(l,c,r,n-1);cout<<"\nMove "<<n<<" from "<<l<<" to "<<r;diskchange(c,r,l,n-1);
} }
int main() {
int n;cout<<"\nEnter the number of disks\n";cin>>n;
diskchange('L','R','C',n);cout<<"\nPress any key to exit\n";getch();
return 0; }
Output:Enter the number of disks 3Move disk1 from A to CMove disk2 from A to BMove disk1 from C to BMove disk3 from A to C Move disk1 from B to A Move disk2 fromB to C Move disk1 from A to C
14
11. Write a C++ program to determine if the given string is a palindrome or not. #include<iostream.h> #include<conio.h> #include<string.h> int main() { clrscr(); char *p=new char[20]; cout<<"enter string\t"; cin>>p; char *q=new char[20]; strcpy(q,p); char *r=new char[20]; r=strrev(q); if(!strcmp(r,p)) cout<<p<<"\t The given string is palindrome"; else cout<<p<<"\ t The given string is not palindrome"; getch(); return 0; }
Output:
Enter the name of stringPopThe given string is palindrome
15
12. write a c++ program to find out string ascending order.
#include<iostream.h>int i,j, min, min_pos;char min_name[5], temp[5];
for(i=0;i<3;i++){ strcpy(min_name, letters[i]); //takes the first letter as the index min_pos=i; for(j=i;j<3;j++) { if ((strcmp(min_name, letters[j])>0))
{strcpy(min_name,letters[j]);min_pos=j; / rember position
} } strcpy(temp,letters[i]); strcpy(letters[i],letters[min_pos]); strcpy(letters[min_pos],temp);}
Output:
Enter no of strings 3Ravi venu sudheer
Sorted names: ravi sudheer venu
16
13. Write a C++ program to perform single inheritance
#include <iostream>using namespace std;class base {
int i, j;public:void set(int a, int b) { i=a; j=b; }void show() {
cout << “This is base class”; }
};class derived : public base {
int k;public:derived(int x) { k=x; }void showk() { cout <<"This is derived class”;
}};int main(){
derived ob(3);ob.set(1, 2); // access member of baseob.show(); // access member of baseob.showk(); // uses member of derived classreturn 0;
}
Output:
This is base classThis is derived class
17
14. Write a C++ program to perform multiple inheritance
#include <iostream>using namespace std;class base1 {
protected:int x;public:void showx() { cout << x << "\n"; }
};class base2 {
protected:int y;public:void showy() {cout << y << "\n";}
};// Inherit multiple base classes.class derived: public base1, public base2{
public:void set(int i, int j) { x=i; y=j; }
};int main(){
derived ob;ob.set(10, 20); // provided by derivedob.showx(); // from base1ob.showy(); // from base2return 0;
}
18
15. Write a C++ program to perform multi-level inheritance
#include< iostream.h>#include< conio.h>
class student // Base Class{protected:int rollno;char *name;public:void getdata(int b,char *n){rollno = b;name = n;}void putdata(void){cout< < " The Name Of Student \t: "< < name< < endl;cout< < " The Roll No. Is \t: "< < rollno< < endl;}};
class test:public student // Derieved Class 1{protected:float m1,m2;public:void gettest(float b,float c){m1 = b;m2 = c;}void puttest(void){cout< < " Marks In CP Is \t: "< < m1< < endl;cout< < " Marks In Drawing Is \t: "< < m2< < endl;}};
class result:public test // Derieved Class 2{protected:float total;public:
19
void displayresult(void){total = m1 + m2;putdata();puttest();cout< < " Total Of The Two \t: "< < total< < endl;}};
void main(){clrscr();int x;float y,z;char n[20];cout< < "Enter Your Name:";cin>>n;cout< < "Enter The Roll Number:";cin>>x;result r1;r1.getdata(x,n);cout< < "ENTER COMPUTER PROGRAMMING MARKS:";cin>>y;cout< < "ENTER DRAWING MARKS:";cin>>z;r1.gettest(y,z);cout< < endl< < endl< < "************ RESULT **************"< < endl;r1.displayresult();cout< < "**********************************"< < endl;getch();
}
OUTPUT Enter Your Name: LionelEnter The Roll Number:44ENTER COMPUTER PROGRAMMING MARKS:95ENTER DRAWING MARKS:90
"************ RESULT **************The Name of Student: LionelThe Roll No. Is : 44Marks In CP Is : 95Marks In Drawing Is : 90Total Of The Two : 185
20
16. Write a C++ program to perform hierarichial inheritance
#include <iostream.h>
class vehicle {protected:int wheels;float weight;public:void initialize(int in_wheels, float in_weight);int get_wheels(void) {return wheels;}float get_weight(void) {return weight;}float wheel_loading(void) {return weight/wheels;}};
class car : public vehicle {int passenger_load;public:void initialize(int in_wheels, float in_weight, int people = 4);int passengers(void) {return passenger_load;}};
class truck : public vehicle {int passenger_load;float payload;public:void init_truck(int how_many = 2, float max_load = 24000.0);float efficiency(void);int passengers(void) {return passenger_load;}};
Void main(){vehicle unicycle;
unicycle.initialize(1, 12.5);cout << "The unicycle has " <<unicycle.get_wheels() << " wheel.n";cout << "The unicycle's wheel loading is " <<unicycle.wheel_loading() << " pounds on the single tire.n";
21
cout << "The unicycle weighs " <<unicycle.get_weight() << " pounds.nn";
car sedan;
sedan.initialize(4, 3500.0, 5);cout << "The sedan carries " << sedan.passengers() <<" passengers.n";cout << "The sedan weighs " << sedan.get_weight() << " pounds.n";cout << "The sedan's wheel loading is " <<sedan.wheel_loading() << " pounds per tire.nn";
truck semi;
semi.initialize(18, 12500.0);semi.init_truck(1, 33675.0);cout << "The semi weighs " << semi.get_weight() << " pounds.n";cout << "The semi's efficiency is " <<100.0 * semi.efficiency() << " percent.n";}
// initialize to any data desiredVoid vehicle::initialize(int in_wheels, float in_weight){wheels = in_wheels;weight = in_weight;}
Void car::initialize(int in_wheels, float in_weight, int people){passenger_load = people;wheels = in_wheels;weight = in_weight;}
Void truck::init_truck(int how_many, float max_load){passenger_load = how_many;payload = max_load;}
Float truck::efficiency(void){return payload / (payload + weight);}
22
17. Write a C++ program to perform hybraid inheritance
#include<iostream.h>class a{public: void f() { cout<<"base class\n"; }};class b:private a //visibility mode private{public: void f1() { cout<<"inheritance\n"; }};class c:public a //visibility mode public{public: void f2() { cout<<"hybrid\n"; }};class d:public b,public cvoid main(){d w;w.f(); //giving ambiguity error}
23
18. Write a C++ program to perform parameterized constructor.
#include <iostream>using namespace std;class myclass { int a, b;public: myclass(int i, int j) { a=i; b=j; } void show() { cout << a << " " << b; }};int main(){ myclass ob(3, 5); ob.show(); return 0;}
Output:
3 5
24
19. Write a C++ program that illustrates virtual functions.
#include<iostream.h> #include<conio.h> #include<string.h> class media { protected: char title[50]; float price; public: media(char *s,float a) { strcpy(title,s); price=a; } virtual void display(){}; }; class book: public media { int pages; public: book(char *s,float a,int p):media(s,a) { pages=p; } void display(); }; class tape:public media { float time; public: tape(char *s,float a,float t):media(s,a) { time=t; } void display(); }; void book::display() { cout<<"\nTitle"<<title;
25
cout<<"\nPages"<<pages; cout<<"\nPrice"<<price; } void tape::display() { cout<<"\nTitle"<<title; cout<<"\nTime"<<time; cout<<"\nPrice"<<price; } int main() { clrscr(); char *title=new char[30]; float price,time; int pages; cout<<"Enter book details"<<endl; cout<<"Title\t:";cin>>title; cout<<"Price\t:";cin>>price; cout<<"Pages\t:";cin>>pages; book b1(title,price,pages); cout<<"Enter tape ditails"<<endl; cout<<"Title\t:";cin>>title; cout<<"Price\t:";cin>>price; cout<<"Time\t:";cin>>time; tape t1(title,price,time); media* list[2]; list[0]=&b1; list[1]=&t1; cout<<"_______Book_______"<<endl; list[0]->display(); cout<<"_______Tape_______"<<endl; list[1]->display(); getch(); return 0; }
26
20. Write a C++ program to perform virtual destructors.
#include <iostream.h>class Base{ public: Base(){ cout<<"Constructor: Base"<<endl;} virtual ~Base(){ cout<<"Destructor : Base"<<endl;}};class Derived: public Base{ //Doing a lot of jobs by extending the functionality public: Derived(){ cout<<"Constructor: Derived"<<endl;} ~Derived(){ cout<<"Destructor : Derived"<<endl;}};void main(){ Base *Var = new Derived(); delete Var;}
Output:
Constructor: DerivedConstructor: BaseDestructor : DerivedDestructor : Base
27
21. Write a C++ program that uses (A) function templates. (B)class templates
(A) Function templates #include<iostream.h> #include<conio.h> template <class T> void ascen(T a[],int n); int main() { clrscr(); int a[10]={25,14,1,58,47,65,14,25,10,54}; float b[10]={25.25,14.21,1.36,58.54,47.24,65.74,14.58,25.147,10.10,54.68}; char c[10]={'a','f','e','a','u','c','h','i','x','p'}; int n=10; cout<<endl; ascen(a,n); cout<<endl; ascen(b,n); cout<<endl; ascen(c,n); getch(); return 0; } template <class T> void ascen(T a[],int n) { T temp; for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) if(a[i]>a[j]) { temp=a[j]; a[j]=a[i]; a[i]=temp; } cout<<"Minimum of the data is \t"<<a[0]<<endl; cout<<"Maximum of the data is \t"<<a[9]<<endl; cout<<"Data in ascending order "<<endl; for(i=0;i<n;i++)
28
{cout.precision(2); cout<<a[i]<<"\t";} }
(B)class templates
#include <iostream>using namespace std ;
template <class T>class stream{
public:void f() { cout << "stream<T>::f()"<< endl ;}
} ;
template <>class stream<char>{
public:void f() { cout << "stream<char>::f()"<< endl ;}
} ;
int main(){
stream<int> si ;stream<char> sc ;
si.f() ;sc.f() ;
return 0 ;}
Output:
stream<T>::f()stream<char>::f()
29
25. write a c++ program to find out friend class.
#include <iostream>using namespace std;
class MyClass{ friend class MyFriend; // Declare a friend class...
public MyClass() { m_private = true; cout << "m_private = " << m_private << endl; }
private bool m_private;};
class MyFriend{
public:
void change( MyClass myClass ) { myClass.m_private = false; // A friend can access private data... cout << "m_private = " << myClass.m_private << endl; }};
void main(){ MyClass myClass; MyFriend myFriend;
myFriend.change( myClass ); return 0;}
30
26. write a c++ program to find out pure virtual functions.
#include <iostream>using namespace std;
class A{
public:
virtual void display () = 0; // A pure virutal function};
class B: public A {
public:
virtual void display() { cout << " This is Class B's display() function ..." << endl; }};
Void main( ){ //A obj1; // Can't create an object of class type A, but...
B obj2; // class type B is fine becuase it implements foo()
Obj2.display(); return 0;}
Output:
This is Class B's display() function .
31
27. Write a c++ program to perform Implement the complex number ADT in C++ using a class. The complex ADT is used to represent complex numbers of the form c=a+ib, where a and b are real numbers. The operations supported by this ADT are: a) Reading a complex number. d) Subtraction of complex numbers. b) Writing a complex number. e) Multiplication of complex numbers. c) Addition of Complex numbers. f) Division of complex numbers.
Note: 1. overload << and >> operators in part a and part b.2. overload +, - , * , / operators in parts c, d, e and f.
#include<iostream.h> #include<conio.h> class complex { float r,im; public: complex(){} friend ostream & operator <<(ostream &,complex &); friend istream & operator >>(istream &,complex &); complex operator +(complex &); complex operator -(complex &); complex operator *(complex &); complex operator /(complex &); ~complex(){} }; istream & operator >>(istream &read,complex &b) { cout<<"Enter Real part of complex number\t"; read>>b.r; cout<<"Enter Imaginary part of complex number\t"; read>>b.im; return read; }
32
ostream & operator<<(ostream &out,complex &b) { out<<b.r; if(b.im>=0) cout<<"+"; cout<<b.im<<"i"; return out; } complex complex::operator +(complex &b) { complex c; c.r=r+b.r; c.im=im+b.im; return c; } complex complex::operator -(complex &b) { complex c; c.r=r-b.r; c.im=r-b.im; return c; } complex complex::operator *(complex &b) { complex c; c.r=(r*b.r)+(-1*(im*b.im)); c.im=(r*b.im)+(im*b.r); return c; } complex complex::operator /(complex &b) { int d,n,m; complex c; d=(b.r*b.r)+(b.im*b.im); n=(r*b.r)-(-1*(im*b.im)); m=(r*(-1*b.im))+(im*b.r); c.r=n; c.im=m; cout<<"("<<n<<"/"<<d<<")"; if(m>=0) cout<<"+"; cout<<"("<<m<<"/"<<d<<")i";
33
return c; } int main() { clrscr(); complex a,b,c; cin>>a; cin>>b; c=a+b; cout<<endl<<"Addition result"<<endl; cout<<c; cout<<endl<<"Subtraction result"<<endl; c=a-b; cout<<c; c=a*b; cout<<endl<<"Multiplication result"<<endl; cout<<c; cout<<endl<<"Division result"<<endl; c=a/b; getch(); return 0; }
34
29. write a c++ program to find out exception handling by multilple catch statements.
#include <iostream>using namespace std;.void Xhandler(int test){ Try {
if(test) throw test;else throw "Value is zero";
} catch(int i) {
cout << "Caught Exception #: " << i << '\n'; } Catch(const char *str) { Cout << "Caught a string: "; Cout << str << '\n'; }}void main(){cout << "Start\n";Xhandler(1);Xhandler(2);Xhandler(0);Xhandler(3);cout << "End";}
output:
Caught Exception #: 1Caught Exception #: 2Caught a string: Value is zeroCaught Exception #: 3End
35
30. Write a C++ program for rethrowing an exception.
#include <iostream>using namespace std;void Xhandler(){try {throw "hello"; // throw a char *}catch(const char *) { // catch a char *cout << "Caught char * inside Xhandler\n";throw ; // rethrow char * out of function}}Notevoid main(){cout << "Start\n";try{Xhandler();}catch(const char *) {cout << "Caught char * inside main\n";}cout << "End";return 0;}
output:
Caught char * inside XhandlerCaught char * inside mainEnd
36
32. Write a C++ program which copies one file to another.
#include<iostream.h> #include<fstream.h> int main(int argc,char *argv[]) { if(argc!=3) { cout<<"Arguments must be Theree"; return 1; } ifstream fin; fin.open(argv[1],ios::nocreate); if(!fin) { cout<<"File does not exits"; return 1; } ofstream fout; fout.open(argv[2],ios::out); char ch; while(!fin) { fin.get(ch); fout.put(ch); } fin.close(); fout.close(); return 0; }
Output:
File does not exists
37