64
Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Department Of Computer Science B.Sc. 1 st Semester Lab Manual (CBCS Syllabus) OBJECT ORIENTED PROGRAMMING Using C++ Lab

Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal

Government First Grade College, Mudgal -584125

Affiliated under

Gulbarga University

Department Of

Computer Science

B.Sc. 1st

Semester Lab Manual (CBCS Syllabus)

OBJECT ORIENTED PROGRAMMING Using C++ Lab

Page 2: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

INDEX

OBJECT ORIENTED PROGRAMMING USING C++ Lab

Sl.

No Name of the Program

1

Program to read the ballots and count the votes cast for each candidate using an array variable count. In case, a number read is outside the range 1 to 5, the ballot should be considered as a ‘spoilt ballot’, and the program should also count the number of spoilt ballots.

2 Program to find the factorial of a number using ‘return by reference’ concept.

3 Program to find the largest of three numbers using Inline functions.

4 Program to read a matrix of size m x n from the keyboard and display the same on the screen. Make the row parameter of the matrix as a default argument.

5 Write a main that calls both the functions. Use the concept of

function overloading

6 Program in C++ to swap two variables of data types integer, floating

point number and character type using function overloading

7 Write a C++ program to create a class Sample. Include the following

class members

8 Write a C++ program using class to demonstrate Nesting of member

functions

9 The time object shows the time in hh:mm:ss format. Design a program to add 2 time objects using friend function.

10 Program to find the volume of a cube, cylinder, rectangle using

constructors overloading.

Page 3: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

1) An election is contested by five candidates. The candidates are numbered 1 to 5

and the voting is done by marking the candidate number on the ballot paper.

Write a program to read the ballots and count the votes cast for each candidate

using an array variable count. In case, a number read is outside the range 1 to 5,

the ballot should be considered as a ‘spoilt ballot’, and the program should also

count the number of spoilt ballots.

#include<iostream>

usingnamespace std;

main()

{

int a[5]={0,0,0,0,0};

int n,vote,sb=0,i;

cout<<"Enter the number of votes\n";

cin>>n;

for(i=0;i<n;i++)

{

cout<<"Enter your vote : ";

cin>>vote;

switch(vote)

{

case 1:a[0]++;

break;

case 2:a[1]++;

break;

case 3:a[2]++;

break;

case 4:a[3]++;

break;

case 5:a[4]++;

break;

default:sb++;

}

}

cout<<"\n\nResults of Election are as follows\n";

for(i=0;i<5;i++)

cout<<"\ncandidate "<<i+1<<" : "<<a[i];

cout<<"\nSpoiled Ballots : "<<sb;

}

Page 4: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

2) Write a program to find the factorial of a number using ‘return by reference’ concept.

#include<iostream>

usingnamespace std;

int&factorial(int,int&f);

main()

{

int n,fact=1;

cout<<"enter a number\n";

cin>>n;

fact=factorial(n,fact);

cout<<n<<"! = "<<fact;

}

int&factorial(int n,int&f)

{

int i;

if(n==0)

{

f=0;

return f;

}

else

{

for(i=1;i<=n;i++)

f=f*i;

return f;

}

}

Page 5: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

3) Write a program to find the largest of three numbers using Inline functions.

#include<iostream>

usingnamespace std;

inlineint largest(int a,int b,int c)

{

return((a>b)?((a>c)?a:c):((b>c)?b:c));

}

main()

{

int a,b,c;

cout<<"enter values for a,b,c : ";

cin>>a>>b>>c;

cout<<"Largest = "<<largest(a,b,c);

}

Page 6: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

4) Write a program to read a matrix of size m x n from the keyboard and display the

same on the screen. Make the row parameter of the matrix as a default argument.

#include<iostream>

usingnamespace std;

main()

{

void matrix(int n,int m=3);

int m,n;

cout<<"Enter the no of cols : ";

cin>>n;

matrix(n);

}

void matrix(int n,int m)

{

int a[20][20];

int i,j;

cout<<"enter the elements of the matrix \n";

for(i=0;i<m;i++)

for(j=0;j<n;j++)

cin>>a[i][j];

cout<<"\n Matrix \n";

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

cout<<a[i][j]<<"\t";

cout<<"\n";

}

}

Page 7: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

5) Write a main that calls both the functions. Use the concept of function

overloading.

#include<iostream>

usingnamespace std;

double power(double m,int n=2);

int power(int m,int n=2);

main()

{

int n;

double m;

cout<<"enter m and n : ";

cin>>m>>n;

cout<<m<<" to the power of "<<n<<" = "<<power(m,n);

cout<<"\n";

{

int m;

cout<<"enter m ";

cin>>m;

cout<<m<<" to the power of 2 = "<<power(m);

}

}

double power(double m,int n)

{

double p=1,i;

for(i=1;i<=n;i++)

p=p*m;

cout<<"fun 1\n";

return p;

}

int power(int m,int n)

{

int p=1,i;

for(i=1;i<=n;i++)

p=p*m;

cout<<"fun2\n";

return p;

Page 8: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

6) Write a program in C++ to swap two variables of data types integer,

floating point number and character type using function overloading.

#include<iostream>

usingnamespace std;

void swap(int a,int b)

{

int t;

t=a;

a=b;

b=t;

cout<<"\nAfter swapping integer values\n a = "<<a<<" b = "<<b;

}

void swap(float a,float b)

{

float t;

t=a;

a=b;

b=t;

cout<<"\nAfter swapping floating point values\n a = "<<a<<" b = "<<b;

}

void swap(char a,char b)

{

char t;

t=a;

a=b;

b=t;

cout<<"\nAfter swapping character type values\n a = "<<a<<" b = "<<b;

}

main()

{

int a,b;

float c,d;

char m,n;

cout<<"enter 2 integer values \n";

cin>>a>>b;

cout<<"enter 2 floating point values \n";

cin>>c>>d;

cout<<"enter 2 character values \n";

cin>>m>>n;

swap(a,b);

swap(c,d);

swap(m,n);

}

Page 9: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

7. Write a C++ program to create a class Sample. Include the following

class members.

Read() – a private member function to read the value for any private

variable of type integer

Update() – a public member function to access the private member

function

Write()—a public member function to display the value of private data

member

#include<iostream>

usingnamespace std;

class sample

{

private: int n;

void read(int a)

{

n=a;

}

public: void update(int a)

{

read(a);

}

void write()

{

cout<<"\nPrivate member n : "<<n;

}

};

main()

{

int x;

cout<<"enter a value : ";

cin>>x;

sample s;

s.update(x);

s.write();

}

Page 10: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

8. Write a C++ program using class to demonstrate Nesting of member

functions.

#include<iostream>

usingnamespace std;

class Nest

{

int m1;

int m2;

public: void getdata(int a,int b)

{

m1=a;

m2=b;

display();

}

void display()

{

cout<<"m1 : "<<m1;

cout<<"\tm2 : "<<m2;

}

};

int main()

{

Nest n;

n.getdata(95,99);

}

Page 11: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

9. The time object shows the time in hh:mm:ss format. Design a program

to add 2 time objects using friend function.

#include<iostream>

usingnamespace std;

class time

{

int h;

int m;

int s;

public: void getdata(int a,int b,int c)

{

h=a;

m=b;

s=c;

}

void disp()

{

cout<<h<<":"<<m<<":"<<s;

}

friend time add(time t1,time t2);

};

time add(time t1,time t2)

{

time t;

int hh,mm,ss;

t.s=t1.s+t2.s;

ss=t.s/60;

t.s=t.s%60;

t.m=ss+t1.m+t2.m;

mm=t.m/60;

t.m=t.m%60;

t.h=mm+t1.h+t2.h;

if(t.h>=24)

{

t.h=t.h-24;

}

return t;

}

main()

{

int a,b,c;

Page 12: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

time t1,t2,t3;

cout<<"\nenter 3 values(hh:mm:ss)";

cin>>a>>b>>c;

t1.getdata(a,b,c);

cout<<"\nenter 3 values(hh:mm:ss)";

cin>>a>>b>>c;

t2.getdata(a,b,c);

cout<<"\n\nEntered time values are\n";

t1.disp();

cout<<"\t";

t2.disp();

cout<<"\n\nSum of two time objects = ";

t3=add(t1,t2);

t3.disp();

}

Page 13: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

10. Write a program to find the volume of a cube, cylinder, rectangle using

constructors overloading.

#include<iostream>

usingnamespace std;

class volume

{

float v;

public: volume(float s)

{

v=s*s*s;

cout<<"\nVolume of cube of side "<<s<<" = "<<v;

}

volume(float l,float b,float h)

{

v=l*b*h;

cout<<"\nVolume of rectangle of length,breadth & height ";

cout<<l<<b<<h<<" = "<<v;

}

volume(float r,float h)

{

v=(22/7)*r*r*h;

cout<<"\nVolume of cylinder with radius & height ";

cout<<r<<h<<" = "<<v;

}

};

main()

{

float s,l,b,h,r;

cout<<"\nenter length of side of a cube : ";

cin>>s;

volume v1(s);

cout<<"\nenter length,breadth & height of rectangle : ";

cin>>l>>b>>h;

volume v2(l,b,h);

cout<<"\nenter radius & height of the cylinder : ";

cin>>r>>h;

volume v3(r,h);

}

Page 14: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal

Government First Grade College, Mudgal -584125

Affiliated under

Gulbarga University

Department Of

Computer Science

B.Sc. 2nd Semester Lab Manual (CBCS Syllabus)

Data Structure using C++ Lab

Page 15: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

INDEX

Data Structure using C++ Lab

Sl. No Name of the Program

1 Program to insert and delete an element in an array

2 Program to perform linear search using array;

3 Program to perform the Binary Search operations in array;

4 Program to perform STACK OPERATIONS

5 Program to perform operations on QUEUE.

6 Program to demonstrate a concept of Linked list.

7 Program to perform TRAVERSE a tree in PREORDER

8 Program to TRAVERSE a tree in INORDER

9 Program to TRAVERSE a tree in POSTORDER

10 Program to calculate POSTFIX expression

11 Program to perform SELECTION sort

12 Program to perform BUBBLE sort

13 Program to perform INSERTION sort

14 Program to perform RADIX sort

Page 16: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

//1.Program to insert and delete an element in an array

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<process.h>

class arrays

{

private: int a[10],n,i,k,item,ch,j;

public: void opers();

};

void arrays:: opers()

{

cout<<"\n\n OUTPUT FOR ARRAY OPERATIONS \n\n";

cout<<"enter the number of elements you want:\n";

cin>>n;

cout<<"enter the "<<n<<" elements:\n";

for(i=1;i<=n;i++)

cin>>a[i];

do

{

cout<<"\n enter the choice: \n 1.insert \n 2.delete \n 3.exit\n";

cin>>ch;

switch(ch)

{

case 1:

cout<<"\n enter the item to be inserted \n";

cin>>item;

cout<<"\n enter the position to insert the item\n";

cin>>k;

j=n;

while(j>=k)

{

a[j+1]=a[j];

j--;

}

a[k]=item;

n++;

cout<<"After insertion the array\n";

for(i=1;i<=n;i++)

cout<<"\t"<<a[i];

break;

case 2:

if(n==0)

cout<<"The array is empty,deletion is not possible\n";

else

{

cout<<"enter the position of the item to be deleted\n";

cin>>k;

Page 17: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

item=a[k];

j=k;

while(j<=n-1)

{

a[j]=a[j+1];

j++;

}

n--;

if(n==0)

cout<<"\n now the array is empty\n";

else

cout<<"\n After deletion the array is:\n";

for(i=1;i<=n;i++)

cout<<"\t"<<a[i];

}

break;

default:

cout<<"the code is mismatch try again!\n";

break;

}

}

while(ch!=3);

}

void main()

{

clrscr();

arrays ar;

ar.opers();

getch();

}

OUTPUT FOR ARRAY OPERATIONS

enter the number of elements you want:

4

enter the 4 elements:

12 34 56 67

enter the choice:

1.insert

2.delete

3.exit

1

enter the item to be inserted

23

enter the position to insert the item

3

Page 18: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

After insertion the array

12 34 23 56 78

enter the choice:

1.insert

2.delete

3.exit

2

enter the position of the item to be deleted

3

After deletion the array is:

12 34 56 78

enter the choice:

1.insert

2.delete

3.exit

3

the code is mismatch try again!

Page 19: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

//2.Program to perform linear search using array;

#include<iostream.h>

#include<conio.h>

class linear

{

private: int i,n,item,a[10],loc;

public: void search();

};

void linear:: search()

{ cout<<"OUT PUT FOR LINEAR SEARCH"<<endl;

cout<<"Enter no of elements"<<endl;

cin>>n;

cout<<"Enter the elements"<<endl;

for(i=0;i<n;i++)

cin>>a[i];

cout<<"Enter the item to search"<<endl;

cin>>item;

for(i=0;i<n;i++)

if(a[i]==item)

{

loc=i;

cout<<"Search is sucessful at location "<<loc;

}

if(loc!=item)

cout<<"\n Search is unsucessful\n";

}

void main()

{

clrscr();

linear ln;

ln.search();

getch();

}

OUT PUT FOR LINEAR SEARCH

Enter no of elements

4

Enter the elements

12 34 45 67

Enter the item to search

67

Search is successful at location 3

Page 20: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

//3.program to perform the binary search operations in array;

#include<iostream.h>

#include<conio.h>

class binsear

{

private: int a[20],st,i,j,temp,end,n,mid,loc,item;

public: void search();

};

void binsear:: search( )

{

cout<<"\n OutPut of binary search\n";

cout<<"enter number of elements:\n";

cin>>n;

cout<<"enter the elemens of array:\n";

for(i=0;i<n;i++)

cin>>a[i];

cout<<"enter the elements to search:\n";

cin>>item;

st=1;

end=n;

mid = ((st+end)/2);

while((st<=end)&&(a[mid]!=item))

{

if(item<a[mid])

end=mid-1;

else

st=mid+1;

mid=((st+end)/2);

}

if(a[mid]==item)

cout<<"search is successful at location:"<<mid;

else

cout<<"search is unsucessful \n";

}

void main( )

{

binsear br;

clrscr();

br.search();

getch();

}

OUTPUT OF BINARY SEARCH

enter number of elements:

5

enter the elemens of array:

12 34 56 67 23

enter the elements to search:

67

search is successful at location:3

Page 21: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

//4. Program to perform STACK OPERATIONS

#include<iostream.h>

#include<conio.h>

#include<process.h>

class stacks

{

private: int top,st[10],item,i,ch;

public: void operations();

};

void stacks::operations()

{

top=0;

cout<<"output for stack operations:\n";

cout<<"\n enter the choice:\n 1.push \n 2.pop \n 3.exit\n";

cin>>ch;

while(ch!=3)

{

switch(ch)

{

case 1: cout<<"enter the item to insert in stack:\n";

cin>>item;

st[++top] = item;

break;

case 2: --top;

if(top<=0)

{

cout<<"empty stack";

top=0;

}

break;

case 3:exit(1);

default: cout<<"illegal entry\n";

}

cout<<"\n After stack operation\n";

for(i=1;i<=top;i++)

cout<<"\n"<<st[i];

cout<<"\n enter the choice 1.push 2.pop 3.exit\n";

cin>>ch;

}

}

Page 22: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

void main()

{

stacks st;

clrscr();

st.operations();

getch();

}

OUTPUT FOR STACK OPERATIONS:

enter the choice:

1.push

2.pop

3.exit

1

enter the item to insert in stack:

23

After stack operation

23

enter the choice 1.push 2.pop 3.exit

1

enter the item to insert in stack:

24

After stack operation

23

24

enter the choice 1.push 2.pop 3.exit

1

enter the item to insert in stack:

25

After stack operation

23

24

25

enter the choice 1.push 2.pop 3.exit

2

After stack operation

23

24

enter the choice 1.push 2.pop 3.exit

3

Page 23: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

//5.Program to peform operations on QUEUE.

#include<iostream.h>

#include<conio.h>

#define maxq 10

class queue

{

private: int rear,front,q[10],ch,i,item;

public:

void calc();

};

void queue::calc()

{

front=1;

rear=0;

cout<<"Select your choice"<<endl;

cout<<"1.Insert\n 2.Delete\n 3.Exit"<<endl;

cout<<"Enter your choice"<<endl;

cin>>ch;

do

{

switch(ch)

{

case 1:{

if(rear==maxq)

cout<<"queue is full"<<endl;

else

cout<<"Enter item to be inserted"<<endl;

cin>>item;

q[++rear]=item;

}

break;

case 2:{

if(front>=rear)

cout<<"queue is empty"<<endl;

else

item=q[front++];

}

break;

default:{

cout<<"illegal entry"<<endl;

}

break;

}

cout<<"After queue operation"<<endl;

for(i=front;i<=rear;i++)

cout<<q[i]<<endl;

cout<<"enter choice"<<endl;

cout<<"1.Insert 2.Delete 3.Exit"<<endl;

cin>>ch;

}

Page 24: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

while(ch!=3);

}

void main()

{

clrscr();

queue q;

q.calc();

getch();

}

OUT PUT FOR QUEUES:

enter choice:

1.insert 2.delete 3.exit

1

Enter item to be inserted

12

enter choice:

1.insert 2.delete 3.exit

1

Enter item to be inserted

After queue operation

12

13

enter choice

1.Insert 2.Delete 3.Exit

1

Enter item to be inserted

14

After queue operation

12

13

14

enter choice

1.Insert 2.Delete 3.Exit

2

After queue operation

13

14

enter choice

1.Insert 2.Delete 3.Exit

3

Page 25: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

//6. Program to demonstrate a concept of Linked list.

#include<iostream.h>

#include<malloc.h>

#include<conio.h>

struct list

{

int info;

struct list*next;

};

typedef struct list node;

node *first,*ptr,*start;

void main()

{

clrscr();

first=(node*)malloc(sizeof(node));

cout<<"Enter the elements at end enter 0"<<endl;

cin>>first->info;

ptr=start=first;

while(first->info!=0)

{

first=(node*)malloc(sizeof(node));

cin>>first->info;

ptr->next=first;

ptr=first;

}

ptr->next=0;

cout<<"List after creation"<<endl;

while(start->next!=0)

{

cout<<start->info<<"->";

start=start->next;

}

cout<<"Null";

getch();

}

OUTPUT:-

Enter the elements at end enter 0

12

14

35

67

99

0

List after creation

12->14->35->67->99->Null

Page 26: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

//7. Program to perform TRAVERSE a tree in PREORDER

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<alloc.h>

#define null 0

struct tree

{

int info;

struct tree *left,*right;

};

typedef struct tree node;

node * root;

void preorder(node *p)

{

if(p!=null)

{

cout<<p->info;

preorder(p->left);

preorder(p->right);

}

}

void read1(node **p2)

{

node *p1;

int item;

p1=(node *)malloc(sizeof(node));

cin>>item;

if(item!=0)

{

p1->info=item;

p1->left=null;

p1->right=null;

*p2=p1;

}

if(item!=0)

{

cout<<"the left child of is:\n"<<p1->info;

read1(&p1->left);

cout<<"the right child of is:\n"<<p1->info;

read1(&p1->right);

}

}

Page 27: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

void main()

{

root=null;

clrscr();

cout<<"\n\nOUTPUT FOR PREORDER TRAVERSAL\n\n";

cout<<"enter the root or 0 to quit\n";

read1(&root);

cout<<"\nThe preorder traversal of the tree is:\n";

preorder(root);

getch();

}

OUTPUT FOR PREORDER TRAVERSAL

enter the root or 0 to quit

24

the left child of is:

24 6

the left child of is:

6 0

the right child of is:

6 0

the right child of is:

24 5

the left child of is:

5 8

the left child of is:

8 0

the right child of is:

8 0

the right child of is:

5 0

The preorder traversal of the tree is:

24658

Page 28: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

//8. Program to TRAVERSE a tree in INORDER

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<alloc.h>

#define null 0

struct tree

{

int info;

struct tree *left,*right;

};

typedef struct tree node;

node *root;

void inorder(node *p)

{

if(p!=null)

{

inorder(p->left);

cout<<p->info;

inorder(p->right);

}

}

void read1(node **p2)

{

node *p1;

int item;

p1=(node *)malloc(sizeof(node));

cin>>item;

if(item!=0)

{

p1->info=item;

p1->left=null;

p1->right=null;

*p2=p1;

}

if(item!=0)

{

cout<<"the left child of is:\n"<<p1->info;

read1(&p1->left);

cout<<"the right child of is:\n"<<p1->info;

read1(&p1->right);

}

}

Page 29: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

void main()

{

root=null;

clrscr();

cout<<"\n\nOUTPUT FOR INORDER TRAVERSAL\n\n";

cout<<"enter the root or 0 to quit\n";

read1(&root);

cout<<"\n The inorder traversal of the tree is:\n";

inorder(root);

getch();

}

OUTPUT FOR INORDER TRAVERSAL

enter the root or 0 to quit

24

the left child of is:

24 12

the left child of is:

12 0

the right child of is:

12 43

the left child of is:

43 67

the left child of is:

67 0

the right child of is:

67 0

the right child of is:

43 0

the right child of is:

24 0

The inorder traversal of the tree is:

12674324

Page 30: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

//9. Program to TRAVERSE a tree in POSTORDER

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<alloc.h>

#define null 0

struct tree

{

int info;

struct tree *left,*right;

};

typedef struct tree node;

node *root;

void postorder(node *p)

{

if(p!=null)

{

postorder(p->left);

postorder(p->right);

cout<<p->info;

}

}

void read1(node **p2)

{

node *p1;

int item;

p1=(node *)malloc(sizeof(node));

cin>>item;

if(item!=0)

{

p1->info=item;

p1->left=null;

p1->right=null;

*p2=p1;

}

if(item!=0)

{

cout<<"the left child of is:\n"<<p1->info;

read1(&p1->left);

cout<<"the right child of is:\n"<<p1->info;

read1(&p1->right);

}

}

Page 31: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

void main()

{

root=null;

clrscr();

cout<<"\n\nOUTPUT FOR POSTORDER TRAVERSAL\n\n";

cout<<"enter the root or 0 to quit\n";

read1(&root);

cout<<"\nThe inorder traversal of the tree is:\n";

postorder(root);

getch();

}

OUT PUT FOR POST ORDER TRAVERSAL

enter the root or 0 to quit

24

the left child of is:

24 3

the left child of is:

3 0

the right child of is:

3 6

the left child of is:

6 0

the right child of is:

6 0

the right child of is:

24 5

the left child of is:

5 9

the left child of is:

9 0

the right child of is:

9 0

the right child of is:

5 0

The inorder traversal of the tree is:

639524

Page 32: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

//10.Program to calculate POSTFIX expression

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<process.h>

class postfix

{

private: char item;

float top,op1,op2,value,st[20];

public: void pstfxopers();

};

void postfix::pstfxopers()

{

cout<<"\n\n OUTPUT FOR POSTFIX EXPRESSION \n\n";

cout<<"\n enter the posfix expression at end enter ) \n";

cin>>item;

top=0;

while(item!=')')

{

if((item>='0') && (item<='9'))

st[++top]=item-'0';

else

{

op1=st[top--];

op2=st[top--];

switch(item)

{

case '+': value=op2+op1;

break;

case '-': value=op2-op1;

break;

case '*': value=op2*op1;

break;

case '/': value=op2/op1;

break;

default:

{

cout<<"invalid entry\n";

}

}

st[++top] = value;

}

fflush(stdin);

cin>>item;

}

cout<<"\n value of the entered postfix exp:\n"<<value;

}

Page 33: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

void main()

{

clrscr();

postfix p;

p.pstfxopers();

getch();

}

OUTPUT FOR POSTFIX EXPRESSION

enter the posfix expression at end enter )

23*7+45*9

)

value of the entered postfix exp:

20

Page 34: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

//11. Program to perform SELECTION sort

#include<iostream.h>

#include<conio.h>

class selsort

{

private: int a[20],k,n,i,j,loc,min,temp;

public: void sort();

};

void selsort::sort()

{

cout<<"\n OUT PUT FOR SELECTION SORT\n";

cout<<"enter the number of elements:\n";

cin>>n;

cout<<"enter the " <<n<< " elements\n";

for(i=1;i<=n;i++)

cin>>a[i];

for(j=1;j<=n;j++)

{

min=a[j];

loc=j;

for(k=j+1;k<=n;k++)

if(min>a[k])

{

min = a[k];

loc=k;

temp=a[j];

a[j]=a[loc];

a[loc]=temp;

}

}

cout<<"sorted elements are: \n";

for(i=1;i<=n;i++)

cout<<"\n"<<a[i];

}

void main()

{

selsort srt;

clrscr();

srt.sort();

getch();

}

Page 35: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

OUT PUT FOR SELECTION SORT

enter the number of elements:

5

enter the 5 elements

12 24 16 89 87

sorted elements are:

12

16

24

87

89

Page 36: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

//12. Program to perform BUBBLE sort

#include<iostream.h>

#include<conio.h>

class bublesrt

{

private: int i,j,n,a[20],temp;

public: void sort();

};

void bublesrt:: sort()

{

cout<<" OUTPUT FOR BUBBLE SORT\n";

cout<<"enter the number of elements to sort:\n";

cin>>n;

cout<<"enter "<< n <<"elements in an array:\n";

for(i=1;i<=n;i++)

cin>>a[i];

for(i=1;i<=n-1;i++)

for(j=1;j<=n-i;j++)

if(a[j]>a[j+1])

{

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}

cout<<"sorted elements are:\n";

for(i=1;i<=n;i++)

cout<<"\n"<<a[i];

}

void main()

{

bublesrt br;

clrscr();

br.sort();

getch();

}

OUTPUT FOR BUBBLE SORT

enter the number of elements to sort:

5

enter 5elements in an array:

12 45 34 67 40

sorted elements are:

12

34

40

45

67

Page 37: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

// 13.Program to perform INSERTION sort

#include<iostream.h>

#include<conio.h>

class insersort

{

private: int a[10],ptr,i,n,k,temp;

public: void sort();

};

void insersort::sort()

{

cout<<"\n OUT PUT FOR INSERTION SORT\n";

cout<<"enter the number of elements:\n";

cin>>n;

cout<<"enter the "<<n<<" elements:\n";

for(i=1;i<=n;i++)

cin>>a[i];

for(i=2;i<=n;i++)

{

temp=a[i];

ptr=i-1;

while(temp<a[ptr])

{

a[ptr+1]=a[ptr];

ptr--;

}

a[ptr+1]=temp;

}

cout<<"sorted elements are:\n";

for(i=1;i<=n;i++)

cout<<"\n"<<a[i];

}

void main()

{

insersort isrt;

clrscr();

isrt.sort();

getch();

}

Page 38: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

OUT PUT FOR INSERTION SORT

enter the number of elements:

5

enter the 5 elements:

12 45 24 78 99

sorted elements are:

12

24

45

78

99

Page 39: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

// 14.Program to perform RADIX sort

#include<iostream.h>

#include<conio.h>

class radixsrt

{

private: int c[20],a[20],d,i,j,k,l,m,n,rem,temp,temp1;

public: void radixsort();

};

void radixsrt:: radixsort()

{

cout<<"\n OUT PUT FOR RADIX SORT\n";

cout<<"enter the value of n:\n";

cin>>n;

cout<<"enter the no.of digits:\n";

cin>>d;

cout<<"enter the "<<d<<" numbers:\n";

for(i=1;i<=n;i++)

cin>>a[i];

temp1=1;

for(l=1;l<=d;l++)

{

i=1;

for(j=0;j<=9;j++)

{

for(k=1;k<=n;k++)

{

temp=a[k]/temp1;

rem=temp%10;

if(rem==j)

{

c[i]=a[k];

i++;

}

}

}

}

temp1=temp1*10;

cout<<" at pass "<<l<<endl;

for(m=1;m<=n;m++)

{

a[m]=c[m];

cout<<"\n"<<a[m];

}

}

Page 40: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

void main()

{

radixsrt rsrt;

clrscr();

rsrt.radixsort();

getch();

}

OUT PUT FOR RADIX SORT

enter the value of n:

4

enter the no.of digits:

2

enter the 2 numbers:

-25

50

-10

60

at pass 3

50

-10

60

40

Page 41: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal

Government First Grade College, Mudgal -584125

Affiliated under

Gulbarga University

Department Of

Computer Science

B.Sc. 3rd

Semester Lab Manual (CBCS Syllabus)

Numerical Computing Lab

Page 42: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

INDEX

OBJECT ORIENTED PROGRAMMING

Sl. No Name of the Program

1 C++ program for bisection method

2 C++ program for secant method

3 C++ program for Newton Raphson metthod

4

C++ program for fixed point iteration equation,

x=cosx

5 C++ Program for Newton Gregory Forward

Interpolation Formula

6 C++ program for trapezoidal rule

7 C++ program for picard's method

8 C++ program for Taylor's series

9 C++ program for Euler's Method dy/dx=1+xy,y(0)=2 compute y(0.1) and y(0.2) with h=0, correct up to five decimal places.

10 C++ program for Simpson's 1/3 rd rule

11 C++ program for Simpson's 3/8 th rule

12 C++ program for Runge-Kutta 4th order method to solve differential equation dy/dx=x-y/2.

Page 43: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

1. C++ program for bisection method

#include<iostream.h>

#include<conio.h>

#include<cmath>

#include<iomanip>

using namespace std;

double f(double x);

double f(double x)

{

double a=pow(x,3)-x-11.0;

return a;

}

int main()

{

cout.precision(4);

cout.setf(ios::fixed);

double a,b,c,e,fa,fb,fc;

a:cout<<"enter the initial guesses:\n";

cout<<"a=";

cin>>a;

cout<<"b=";

cin>>b;

cout<<"\n enter the degree of accuracy desired"<<endl;

cin>>e;

if(f(a)*f(b)>0)

{

cout<<"please enter a different initial guess"<<endl;

goto a;

}

else

{

while(fabs(a-b)>=e)

{

c=(a+b)/2.0;

fa=f(a);

fb=f(b);

fc=f(c);

cout<<"a="<<a<<" "<<"b="<<b<<" "<<"c="<<c<<"fc="<<fc<<endl;

if (fc==0)

Page 44: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

{

cout<<"the root of the equetion is"<<c;

break;

}

if(fa*fc>0)

{

a=c;

}

else if(fa*fc<0)

{

b=c;

}

}

}

cout<<"the root of the equation is"<<c;

getch();

}

OUTPUT:

enter the initial guesses:

a=1

b=3

enter the degree of accuracy desired

0.0001

a=1.0000 b=3.0000 c=2.0000fc=-5.0000

a=2.0000 b=3.0000 c=2.5000fc=2.1250

a=2.0000 b=2.5000 c=2.2500fc=-1.8594

a=2.2500 b=2.5000 c=2.3750fc=0.0215

a=2.2500 b=2.3750 c=2.3125fc=-0.9460

a=2.3125 b=2.3750 c=2.3438fc=-0.4691

a=2.3438 b=2.3750 c=2.3594fc=-0.2256

a=2.3594 b=2.3750 c=2.3672fc=-0.1025

a=2.3672 b=2.3750 c=2.3711fc=-0.0406

a=2.3711 b=2.3750 c=2.3730fc=-0.0096

a=2.3730 b=2.3750 c=2.3740fc=0.0059

a=2.3730 b=2.3740 c=2.3735fc=-0.0018

a=2.3735 b=2.3740 c=2.3738fc=0.0021

a=2.3735 b=2.3738 c=2.3737fc=0.0001

a=2.3735 b=2.3737 c=2.3736fc=-0.0009

the root of the equation is2.3736

Page 45: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

2. C++ program for secant method

#include<iostream.h>

#include<conio.h>

#include<iomanip>

#include<cmath>

using namespace std;

double f(double x);

double f(double x)

{

double a=pow(x,3)-x-11.0;

return a;

}

int main()

{

cout.precision(4);

cout.setf(ios::fixed);

double a,b,c,e;

cout<<"enter the initial guess\na=";

cin>>b;

cout<<"b=";

cin>>c;

cout<<"enter the degree of accuracy\n";

cin>>e;

do

{

a=b;

b=c;

c=b-(b-a)/(f(b)-f(a))*f(b);

if (f(c)==0)

{

cout<<"\n the root of the equation is"<<c;

return 0;

}

}

while(abs(c-b)>=e);

cout<<"\n the root of the equation is"<<c;

getch ();

}

Page 46: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

OUTPUT:

enter the initial guess

a=9

b=10

enter the degree of accuracy

0.0001

the root of the equation is2.3736

Page 47: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

3. C++ program for Newton Raphson metthod

#include<iostream.h>

#include<conio.h>

#include<cmath>

#include<iomanip>

using namespace std;

double f(double x);

double f(double x)

{

double a=pow(x,3.0)-x-11.0;

return a;

}

double fprime(double x);

double fprime(double x)

{

double b=3*pow(x,2.0)-1.0;

return b;

}

int main()

{

double x,x1,e,fx,fx1;

cout.precision(4);

cout.setf(ios::fixed);

cout<<"enter the initial guess:n";

cin>>x1;

cout<<"enter the desired accuracy:n";

cin>>e;

fx=f(x);

fx1=fprime(x);

cout<<"x{i}"<<" "<<"x{i+1}"<<" "<<"|x{i+1}-x{i}|"<<endl;

do

{

x=x1;

fx=f(x);

fx1=fprime(x);

x1=x-(fx/fx1);

cout<<x<<" "<<x1<<" "<<abs(x1-x)<<endl;

}

while (fabs(x1-x)>=e);

cout<<"the root of the equation is"<<x1<<endl;

Page 48: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

getch ();

}

OUTPUT:

enter the initial guess:n6

enter the desired accuracy:n0.00001

x{i} x{i+1} |x{i+1}-x{i}|

6.0000 4.1402 1.8598

4.1402 3.0330 1.1072

3.0330 2.5116 0.5214

2.5116 2.3815 0.1301

2.3815 2.3737 0.0078

2.3737 2.3736 0.0000

2.3736 2.3736 0.0000

the root of the equation is2.3736

Page 49: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

4. C++ program for fixed point iteration equation, x=cosx

#include<iostream.h>

#include<conio.h>

#include<cmath>

using namespace std;

double f(double x)

{

return cos(x);

}

int main()

{

double p,p0=1,eps=0.001;

int i=1,n=1000;

while(i<=n)

{

p=f(p0);

if (fabs(p-p0)<eps)

{

cout<<p<<endl;

break;

}

cout<<"iteration"<<i<<":p="<<p<<endl;

i++;

p0=p;

cout<<"the solution is"<<p<<endl;

if(i>n)

{

cout<<"solution not found(method diverges)"<<endl;

break;

}

}

cout<<"the approximation solution is x="<<p<<"in the iteration"<<i-1<<endl;

getch();

}

OUTPUT:

iteration1:p=0.540302

the solution is0.540302

iteration2:p=0.857553

Page 50: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

the solution is0.857553

iteration3:p=0.65429

the solution is0.65429

iteration4:p=0.79348

the solution is0.79348

iteration5:p=0.701369

the solution is0.701369

iteration6:p=0.76396

the solution is0.76396

iteration7:p=0.722102

the solution is0.722102

iteration8:p=0.750418

the solution is0.750418

iteration9:p=0.731404

the solution is0.731404

iteration10:p=0.744237

the solution is0.744237

iteration11:p=0.735605

the solution is0.735605

iteration12:p=0.741425

the solution is0.741425

iteration13:p=0.737507

the solution is0.737507

iteration14:p=0.740147

the solution is0.740147

iteration15:p=0.738369

the solution is0.738369

iteration16:p=0.739567

the solution is0.739567

0.73876

the approximation solution is x=0.73876in the iteration16

Page 51: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

5. C++ Program for Newton Gregory Forward Interpolation Formula

#include<iostream.h>

#include<conio.h>

#define maxN 100

#define order_of_diff 4

using namespace std;

main()

{

float arr_x[maxN+1],arr_y[maxN+1];

float numerator=1.0;

float denomenator=1.0;

float x,y,p,h,diff_table[maxN+1][order_of_diff+1];

int i,j,k,n;

cout<<"enter the value of n\n";

cin>>n;

cout<<"enter the value of x& y\n";

for(i=0;i<n;i++)

cin>>arr_x[i]>>arr_y[i];

cout<<"enter the value of x at which value of y is to be calculated";

cin>>x;

h=arr_x[1]-arr_x[0];

for(i=0;i<n-1;i++)

diff_table[i][1]=arr_y[i+1]-arr_y[i];

for(j=2;j<=order_of_diff;j++)

for(i=0;i<n-j;i++)

diff_table[i][j]=diff_table[i+1][j-1]-diff_table[i][j-1];

i=0;

while(!(arr_x[i]>x))

i++;

i--;

p=(x-arr_x[i])/h;

y=arr_y[i];

for(k=1;k<=order_of_diff;k++)

{

numerator*=p-k+1;

denomenator*=k;

y+=(numerator/denomenator)*diff_table[i][k];

}

cout<<"when x="<<x<<"y="<<y;

getch();

Page 52: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

OUTPUT:

enter the value of n

5

enter the value of x& y

45 114.84

50 96.16

55 83.32

60 74.48

65 68.48

enter the value of x at which value of y is to be calculated

46

when x=46y=110.526

Page 53: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

6.C++ program for trapezoidal rule

#include<iostream.h> #include<conio.h> #include<cmath> using namespace std; float f(float(x)) { return(pow(x,3)+pow(x,2)-(4*x)-5); } float g(float(x)) { return(3*pow(x,2)+2*x-4); } float h(float(x)) { return(6*x+4); } int main() { long double a,b,d,i,n,I=0,J=0,A,K=0,E=0; cout<<"given f(x)=x^3+2x^2-4x-5"<<endl; cout<<"enter lower limit"<<endl; cin>>a; cout<<"enter upper limit"<<endl; cin>>b; cout<<"enter the number of intervals:"<<endl; cin>>n; d=(b-a)/n; for(i=0;i<=n;i++) { I=I+f(a+(i*d)); } for(i=1;i<n;i++) { J=J+f(a+(i*d)); } A=(d/2)*(I+J); cout<<"the value of integral under the limits:"<<endl; cout<<A<<endl; for(i=0;i<=n;i++) { K=K+a+(i*d); } E=-((b-a)*d*d*(h(K/n)/12)); cout<<"the total error is:"<<endl; cout<<E<<endl;

Page 54: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

getch(); } OUTPUT:

given f(x)=x^3+2x^2-4x-5 enter lower limit 0 enter upper limit 4 enter the number of intervals: 100 the value of integral under the limits: 33.3408 the total error is: -0.00859733

Page 55: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

7.C++ program for picard's method

#include<iostream.h> #include<math.h> #include<conio.h> using namespace std; #define y1(x) (1+(x)+pow(x,2)/2) #define y2(x) (1+(x)+pow(x,2)/2+pow(x,3)/3+pow(x,4)/8) #define y3(x) (1+(x)+pow(x,2)/2+pow(x,3)/3+pow(x,4)/8+pow(x,5)/15+pow(x,6)/48) int main() { double start_value,end_value,allowed_error,temp; double y1[30],y2[30],y3[30]; int count; cout<<"\nstart value:\t"; cin>>start_value; cout<<"\nend value:\t"; cin>>end_value; cout<<"\nallowed error:\t"; cin>>allowed_error; for(temp=start_value,count=0;temp<=end_value;temp=temp+allowed_error,count++) { y1[count]=y1(temp); y2[count]=y2(temp); y3[count]=y3(temp); } cout<<"\nx"; for(temp=start_value;temp<=end_value;temp=temp+allowed_error) { cout<<"\t"<<temp; } cout<<"\n\ny(1)"; for(temp=start_value,count=0;temp<=end_value;temp=temp+allowed_error,count++) { cout<<"\t"<<y1[count]; } cout<<"\n\ny(2)"; for(temp=start_value,count=0;temp<=end_value;temp=temp+allowed_error,count++) { cout<<"\t"<<y2[count]; } cout<<"\n\ny(3)";

for(temp=start_value,count=0;temp<=end_value;temp=temp+allowed_error,count++)

{

Page 56: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

cout<<"\t"<<y3[count];

}

getch();

OUTPUT:

start value: 0

end value: 3

allowed error: 0.4

x 0 0.4 0.8 1.2 1.6 2 2.4 2.8

y(1) 1 1.48 2.12 2.92 3.88 5 6.28 7.72

y(2) 1 1.50453 2.34187 3.7552 6.06453 9.66667 15.0352 22.7205

y(3) 1 1.5053 2.36917 3.9833 7.11311 13.1333 24.3249 44.2335

Page 57: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

8.C++ program for Taylor's series

#include<iostream.h>

#include<cmath>

#include<iomanip>

#include<conio.h>

using namespace std;

int main()

{

double x0=0,y0=1,h=0.1,y1,y2,y3,y4,y;

y1=3*x0+y0*y0;

y2=3+2*y0*y1;

y3=2*y1*y1+2*y0*y2;

y4=6*y1*y2+2*y0*y3;

y=y0+(y1*h)+(y2*pow(h,2))/2+(y3*pow(h,3))/6+(y4*pow(h,4))/24;

cout<<"the value of y when x=0.1 is"<<setprecision(5)<<fixed<<y<<endl;

getch();

}

OUTPUT:

the value of y when x=0.1 is1.12722

Page 58: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

9.C++ program for Euler's Method dy/dx=1+xy,y(0)=2 compute y(0.1) and y(0.2) with

h=0, correct up to five decimal places.

#include<iostream.h> #include<conio.h> #include<iomanip> #include<cmath> using namespace std; double df(double x,double y) { double a=1+x*y; return a; } int main() { int n; double x0,y0,x,y,h; cout.precision(5); cout.setf(ios::fixed); cout<<"\n enter the initial values of x and y respectively:\n"; cin>>x0>>y0; cout<<"\n for what values of xdo you want to find the value of y\n"; cin>>x; cout<<"\n enter the width of the sub interval(h):\n"; cin>>h; cout<<"x"<<setw(19)<<"y"<<setw(19)<<"dy/dx"<<setw(16)<<"y_new\n"; cout<<"_____________\n"; while(fabs(x-x0)>0.0000001) { y=y0+(h*df(x0,y0)); cout<<x0<<setw(16)<<y0<<setw(16)<<df(x0,y0)<<setw(16)<<y<<endl; y0=y; x0=x0+h; } cout<<x0<<setw(16)<<y<<endl; cout<<"the approximate value of y at x="<<x<<"is"<<y<<endl; getch(); }

Page 59: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

OUTPUT:

enter the initial values of x and y respectively: 0 2 for what values of xdo you want to find the value of y 0.1 enter the width of the sub interval(h): 0.1 x y dy/dx y_new _____________ 0.00000 2.00000 1.00000 2.10000 0.10000 2.10000 the approximate value of y at x=0.10000is2.10000

Page 60: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

10.C++ program for Simpson's 1/3 rd rule

#include<iostream.h>

#include<cmath>

#include<conio.h>

using namespace std;

double f(double x)

{

double a=1/(1+x*x);

return a;

}

int main()

{

cout.precision(4);

cout.setf(ios::fixed);

int n,i;

double a,b,c,h,sum=0,integral;

cout<<"\n enter the limits of integration,\n\n initial limit,a=";

cin>>a;

cout<<"\n final limit,b=";

cin>>b;

cout<<"\n enter the no of subintervals(it should be multiple of 3),\nn=";

cin>>n;

double x[n+1],y[n+1];

h=(b-a)/n;

for(i=0;i<n+1;i++)

{

x[i]=a+i*h;

y[i]=f(x[i]);

}

for(i=1;i<n;i+=2)

{

sum=sum+4.0*y[i];

}

for(i=2;i<n-1;i+=2)

{

sum=sum+2.0*y[i];

}

integral=h/3.0*(y[0]+y[n]+sum);

cout<<"\n the define integral is"<<integral<<"\n"<<endl;

getch();

}

Page 61: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

OUTPUT:

enter the limits of integration,

initial limit,a=0

final limit,b=6

enter the no of subintervals(it should be multiple of 3),

n=6

the define integral is1.3662

Page 62: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

11.C++ program for simpson's 3/8th rule.

#include<iostream.h>

#include<conio.h>

#include<cmath>

using namespace std;

double f(double x)

{

double a=1/(1+x*x);

return a;

}

int main()

{

cout.precision(4);

cout.setf(ios::fixed);

int n,i;

double a,b,c,h,sum=0,integral;

cout<<"\n enter the limits of integration,\n\n initial limit,a=";

cin>>a;

cout<<"\n final limit,b=";

cin>>b;

cout<<"\n enter the no of sub intervals(it should be multiple of 3),\nn=";

cin>>n;

double x[n+1],y[n+1];

h=(b-a)/n;

for(i=0;i<n+1;i++)

{

x[i]=a+i*h;

y[i]=f(x[i]);

}

for(i=1;i<n;i++)

{

if(i%3==0)

sum=sum+2*y[i];

else

sum=sum+3*y[i];

}

integral=3*h/8*(y[0]+y[n]+sum);

cout<<"\n the definite integral is"<<integral<<"\n"<<endl;

getch();

}

Page 63: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

OUTPUT:

enter the limits of integration,

initial limit,a=0

final limit,b=6

enter the no of sub intervals(it should be multiple of 3),

n=6

the definite integral is1.3571

Page 64: Depar tment Of Comp uter Science...Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal Government First Grade College, Mudgal -584125 Affiliated under Gulbarga University Depar tment

12.C++ program for Runge-Kutta 4th order method to solve differential equation

dy/dx=x-y/2.

#include<iostream.h> #include<conio.h> using namespace std; float dydx(float x,float y) { return((x-y)/2); } float rungekutta(float x0,float y0,float x,float h) { int n=(int)((x-x0)/h); float k1,k2,k3,k4,k5,k; float y=y0; for(int i=1;i<=n;i++) { k1=h*dydx(x0,y); k2=h*dydx(x0+0.5*h,y+0.5*k1); k3=h*dydx(x0+0.5*h,y+0.5*k2); k4=h*dydx(x0+h,y+k3); y=y+(1.0/6.0)*(k1+2*k2+2*k3+k4); x0=x0+h; } return y; } int main() { float x0=0,y=1,x=2,h=0.2; cout<<"\n the value of y at"<<x<<"is:"<<rungekutta(x0,y,x,h); getch(); } OUTPUT:

the value of y at2is:1.01971