Download docx - computer practicals

Transcript
Page 1: computer practicals

INDEX

S.No Program Page No.

1 Program to show average marks of 6 students using 1-D Array

2 Program to search an element of an array using Linear search method

3 Program to search an element of an array using Binary search4 Program to sort an array in ascending order using Selection sort

5 Program to sort the given array in ascending order using bubble sort method.

6Program to merge the contents of two sorted arrays A & B into third array C. Where array A is sorted in ascending order, B is sorted in descending order; the resultant array is in ascending order.

7 Program to insert an element into Array8 Program to delete an element into Array

9 A menu drive program to perform basic operations on the two complex numbers using structures

10 A menu drive program for number conversion (User defined).11 A menu drive program to Implement STACK as an array12 A menu drive program to implement stack as a linked list13 A Menu drive program to implement queue as an array14 Menu drive program to implement Queue as a linked list

15Program to create a text file(.txt) and display number of words, alphabets, vowels and consonants and number of lowercase and uppercase letters using the concept of DATA FILE HANDLING.

16 Program to read a text file and create a duplicate file by toggling the characters using the concept of data file handling

17Program to create a text file to input roll no. and marks of ten students and display them on screen after reading from the text file, using data file handling

18 Program to read file “sports.dat” and copy only those records where event name is ATHLETIC using the concept of Data file handling

19 Program to print and find the sum of Fibonacci series using recursion

20 Program to print and find the sum of even /odd numbers using recursion

21 Program to find the smallest and the largest element in a dynamically created array using pointers

22 Program to swap two integers using pointers

23 Program to find the length of a string and print the reversed string using pointers

1

Page 2: computer practicals

//Program to show average marks of 6 students using 1-D Array#include <iostream.h>

#include<conio.h>

void main()

{

int i;

float mark[6];

cout << "Enter the marks of your 6 subjects:: \n";

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

{

cin >> mark[i];

}

float sum=0;

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

{

sum += mark[i];

}

float ave = sum/6;

cout << "Average Marks is::" << ave << '\n';

}

Output –Enter a the mark of your 6 sujects::455667586059Average Marks is::57.5

2

Page 3: computer practicals

// Program to search an element of an array using Linear search method.

#include<iostream.h>#include<conio.h>

void main(){

clrscr();int A[50]; int n; int p; int subscript; /*Note: subscript and index are same.*/cout<<"Enter the array size : ";cin>>n; // n=size upto which user wants to insert values in array

cout<<"\n\nEnter elements of array : \n";for(int i=0; i<n; i++)cin>>A[i];

cout<<"\n\nThe array formed = ";for(i=0; i<n; i++)cout<<A[i]<<" ";cout<<"\n\nEnter the element to be searched : ";cin>>p; // p=element to be searchedvoid linear_search ( int A[], int n, int p); //function declarationlinear_search(A,n,p);getch();

}void linear_search(int A[], int n, int p){

int count=0; int B[50]; int flag=0;for(int i=0; i<n; i++){ flag=count;

if( A[i]==p){

B[count]=i;count++;flag=count;

}3

Page 4: computer practicals

}if(flag==0)cout<<"\n\nRequested element not found.";elsefor(i=0; i<=n; i++){ if(A[i]==p)

cout<<"\n\nElement "<<p<<" is found";cout<<"\n\nSubscript = "<<i<<"\n\nPosition = "<<i+1<<"\n\n\n";

}}}

Output: -

Enter the array size : 5

Enter elements of array :

79232412

The array formed = 7 9 23 24 12

Enter the element to be searched : 24

Element 24 is found

Subscript = 3

Position = 4

// Program to search an element of an array using Binary search4

Page 5: computer practicals

#include<iostream.h>

#include<process.h>

#include<conio.h>

void main()

{

clrscr();

int A[50], n,p;

cout<<"Enter the Size of array : ";

cin>>n;

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

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

cin>>A[i];

cout<<"\n\nArray formed is : ";

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

cout<<A[i]<<" ";

cout<<"\n\nEnter the element to be searched : ";

cin>>p;

void binary_search(int A[], int n, int p); //declaration

binary_search(A,n,p);

geth();

}

void binary_search(int A[], int n, int p)

{

int L,U,mid; char ch;

lb: L=0; U=n-1;

while(L<=U) //i.e loop will continue if L<=u. if L>U loop will end

{

5

Page 6: computer practicals

mid=(L+U)/2;

if(A[mid]==p)

{

cout<<"\n\nElement "<<p<<" found. Search Successful.";

cout<<"\n\nSubscript = "<<mid<<" \n\nPosition = "<<mid+1;

break;

}

else if(p<=A[mid])

U=mid-1;

else

L=mid+1;

}//end of while loop

if(L>U)

{

cout<<"\n\nUnsuccessful search.";

cout<<"\n\n\n\nWant to search again. : "; cin>>ch;

if(ch=='y'||ch=='Y')

{

cout<<"\n\n\n\nEnter the element to be searched : ";

cin>>p;

goto lb;

}

else

exit(1);

}

}

Output:6

Page 7: computer practicals

Enter the Size of array : 5Enter the elements :258911Array formed is : 2 5 8 9 11Enter the element to be searched : 8Element 8 found. Search Successful.Subscript = 2Position = 3

//Program to sort an array in ascending order using Selection sort

7

Page 8: computer practicals

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int A[80],n;

lb: cout<<"Enter the size of array : ";

cin>>n;

if(n<=0||n>80)

{

cout<<"\n\nEnter size of array less than or equal to 80.";

goto lb;

}

cout<<"\n\nEnter the elements of array : \n\n";

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

cin>>A[i];

void selection_sort(int A[],int n);

selection_sort(A,n);

cout<<"\n\n\n\n\nSorted array is : \n\n";

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

cout<<A[i]<<" ";

getch();

}

void selection_sort(int A[], int n)

{

int small;

int k,count=0;

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

{ 8

Page 9: computer practicals

small=A[i]; count++;

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

{

if(A[j]<small)

{

small=A[j];

A[j]=A[i];

A[i]=small;

}

}

cout<<"\n\nArray after iteration "<<count<<" is :\n\n";

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

cout<<A[k]<<" ";

}

}

Output:Enter the size of array : 5

Enter the elements of array :

1

8

2

3

9

Array after iteration 1 is :

1 2 8 3 9

Array after iteration 2 is :

1 2 3 8 99

Page 10: computer practicals

Array after iteration 3 is :

1 2 3 8 9

Array after iteration 4 is :

1 2 3 8 9

Sorted array is :

1 2 3 8 9

//Program to sort the given array in ascending order using bubble sort method

10

Page 11: computer practicals

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int A[80],n;

cout<<"Enter desired size of array (<80): "; cin>>n;

cout<<"\n\nEnter the array : ";

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

cin>>A[i];

cout<<"\n\nArray of elements is as shown below :";

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

cout<<A[i]<<" ";

cout<<"\n\nNow Elements will be arranged in asce order using bubble sort :";

void bubble_sort(int A[],int n);

bubble_sort(A , n);

getch();

}

void bubble_sort (int A[], int n)

{

int temp; int count=0;

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

{

for(int j=0; j<n-1; j++)

{

if(A[j+1]<A[j])

{

count++;

11

Page 12: computer practicals

temp=A[j+1];

A[j+1]=A[j];

A[j]=temp;

cout<<"\n\n Array for iteration "<<count<<" is : ";

for(int k=0; k<n; k++)

cout<<A[k]<<" ";

}

}

}

}

Output:Enter desired size of array (<80): 5

Enter the array :

9

7

1

3

4

Array for iteration 1 is : 7 9 1 3 4

Array for iteration 2 is : 7 1 9 3 4

Array for iteration 3 is : 7 1 3 9 4

Array for iteration 4 is : 7 1 3 4 9

Array for iteration 5 is : 1 7 3 4 9

Array for iteration 6 is : 1 3 7 4 9

Array for iteration 7 is : 1 3 4 7 9

Sorted array is : 1 3 4 7 9

12

Page 13: computer practicals

/* Program to merge the contents of two sorted arrays A & B into third array C. Where array A is sorted in ascending order, B is sorted in descending order, the resultant array is in ascending order.

#include<iostream.h>

#include<conio.h>

Void main(int [], int, int[], int, int[])

{

int A[50], B[50], C[50], MN = 0, M, N;+

cout<<”How many elements do you want to creat first array with?(max 50)..”;

cin>>M;

cout<<”\nEnter First Array’s element[ascending]..\n”;

for(int i = 0; i<M; i++)

cin>>A[i];

cout<<”\n How many elements do you want to create second array with?

(max.50)…”;

cin>>N;

MN = M + N;

cout=<<”\nEnter Second Array’s elements[descending]…\n;

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

cin>>B[i];

Merge(A,M,B,N,C);

cout<<”\n\n The Merged array is as shown below..\n”;

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

cout<<C[i]<<””;

cout<<endl;

return 0;

}

Void Merge (int A[], int M, int B[], int N, int c[])

13

Page 14: computer practicals

{

int a,b,c;

for(a=0, b=-1, c=0; a<m && b>=0;)

{

If (A[a]<=B[b])C[c++]=A[a++];

}

else

{

while(b>=0)

C[c++] = B[b--];

}

}

Output –

How many elements do you want to create first array with?(max. 50) … 5Enter First Array’s elements [ascending] ….2 5 8 9 12How many elements do you want to create second array with?(max. 50)…7Enter second Array’s elements [descending] ….16 12 10 8 7 3 1The Merged array is as shown below …..1 2 3 5 7 8 8 9 10 12 12 16

/* Program to insert an element into Array14

Page 15: computer practicals

#include<iostream.h>

#include<process.h>

int FindPos(int[], int, int);

int main ()

{

Int AR[50], ITEM, N, index;

cout<<”How many elements you want in array (max=50)”;

cin>>N;

cout<<”\nEnter Array elements (must be sorted in asc order)\n”;

for(int i=0; i<N; i++)

cin>>AR[i];

char ch=’y’;

while (ch == ‘y’ || ch==’y’)

{

cout <<”\nEnter Element to be inserted…”;

cin>>ITEM;

if (N == 50)

{

cout<<”Overflow!!\n”;

exit(1);

}

Index = FindPos(AR, N, ITEM);

for (i=N; i>index; i--)

{

AR[i] = AR[i-1];

}

AR[index] = ITEM;

N +=1;15

Page 16: computer practicals

cout <<”\n want to insert more elements?(y/n)..”;

cin>>ch;

}

Cout <<”The array now is shown below…\n”;

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

cout<<AR[i]<<” ”;

cout<<endl;

return 0;

}

Int FindPos(int AR[], int size, int item)

{

int pos;

if(item < AR[0])

pos = 0;

else

{

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

{

If (AR[i] <= item && item <AR[i+1])

{

Pos = i + 1;

break;

}

}

If(i==size-1)

Pos = size;

}

return pos;}

16

Page 17: computer practicals

Output –How many elements you want in array (max=50).. 6Enter Array elements (must be sorted in asc order)2 6 9 10 12 15Enter element to be inserted …. 11Want to insert more elements? (y/n) …..yEnter element to be inserted … 4Want to insert more elements? (y/n)…nThe array now is as shown below …..2 4 6 9 10 11 12 15

/* Program to delete an element into Array17

Page 18: computer practicals

#include<iostream.h>

#include<process.h>

int FindPos(int[], int, int);

int main ()

{

Int AR[50], ITEM, N, index;

cout<<”How many elements you want in array (max=50)”;

cin>>N;

cout<<”\nEnter Array elements (must be sorted in asc order)\n”;

for(int i=0; i<N; i++)

cin>>AR[i];

char ch=’y’;

while (ch == ‘Y’ || ch==’y’)

{

cout <<”\nEnter Element to be deleted…”;

cin>>ITEM;

if (N == 0)

{

cout<<”underflow!!\n”;

exit(1);

}

Index = FindPos(AR, N, ITEM);

If(index!=-1)

AR[index]=0;

else

cout<<”Sorry!! No such element in the array.\n”;

cout<<”\nThe array now is as shown below…\n”;

cout<<”Zero (0) signifies deleted element\n”;18

Page 19: computer practicals

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

{

cout<<AR[i]<<” ”;

cout<<endl;

cout<<After this emptied space will be shifted to the end of array\n”;

for(i=index; i<N; i++)

{

AR[i] = AR[i+1];

}

N -=1;

cout <<”\n want to delete more elements? (y/n)..”;

cin>>ch;

}

Cout <<”The array now is shown below…\n”;

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

cout<<AR[i]<<” ”;

cout<<endl;

return 0;

}

Int FindPos(int AR[], int size, int item)

{

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

{

if(AR[i]==item)

return i;

}

Return –1;

}

Output : -19

Page 20: computer practicals

How many elements you want in array …. 10Enter Array elements …2 4 5 3 7 9 12 15 33 40Enter element to be deleted ………. 9The array now is as shown below …Zero (0) signifies deleted element2 4 5 3 7 0 12 15 33 40After this emptied space will be shifted to the end of arrayWant to delete more elements?(y/n) …..nThe array now is shown below…2 4 5 3 7 12 15 33 40

/* A menu drive program to perform basic operations on the two complex numbers using structures. */

20

Page 21: computer practicals

#include<iostream.h>

#include<math.h>

#include<conio.h>

struct complex

{

int i,r;

}x,y;

void add(complex a, complex b)

{

cout<<"\n\n Resultant of addition of complex numbers = "<<a.r+b.r<<" +

("<<a.i+b.i<<")i ";

}

void subtract(complex a, complex b)

{

int ch;

l:

cout<<"\n\n\t1. Subtract complex 2 from complex 1 : ";

cout<<"\n\n\t2.Subtract complex 1 from complex 2 : ";

cout<<"\n\n\tEnter your choice : "; cin>>ch;

switch(ch)

{

case 1 :

cout<<"\n\nResultant of subtraction of complex number => "<<"("<<a.r-b.r<<")"<<"

+ ("<<a.i-b.i<<") i";

21

Page 22: computer practicals

break;

case 2 :

cout<<"\n\nResultant of subtraction of complex number => "<<"("<<b.r-a.r<<")"<<"

+ ("<<b.i-a.i<<") i";

break;

default :

cout<<"\n\nPlease enter desired keyword. ";

goto l;

}

}

void multiply(complex a, complex b)

{

cout<<"\n\n Resultant of multiplication of complex number => "<<"("<<a.r*b.r-

a.i*b.i<<")"<<" + ("<<a.r*b.i+a.i*b.r<<") i";

}

void divide(complex a,complex b)

{

int ch;

l:

cout<<"\n\n\t1. Divide complex 2 from complex 1 : ";

cout<<"\n\n\t2.Divide complex 1 from complex 2 : ";

cout<<"\n\n\tEnter your choice : "; cin>>ch;

switch(ch)

{

case 1 :

cout<<"\n\nResultant of division of complex number => "

cout<<((a.r*b.r)+(a.i*b.i))/(pow(b.r,2)+pow(b.i,2))

cout<<" + ("<<(a.i*b.r)-(a.r*b.i))/(pow(b.r,2)+pow(b.i,2))<<") i";

break;22

Page 23: computer practicals

case 2 :

cout<<"\n\nResultant of division of complex number => "

cout<<((b.r*a.r)+(b.i*a.i))/(pow(a.r,2)+pow(a.i,2))

cout<<" + ("<<((b.i*a.r)-(b.r*a.i))/(pow(a.r,2)+pow(a.i,2))<<") i";

break;

default : cout<<"\n\nPlease enter desired keyword. "; goto l;

}

}

void main()

{

clrscr();

int choice;

char ch;

cout<<"Complex number 1 : ";

cout<<"\n\n\tEnter the real part : "; cin>>x.r;

cout<<"\n\n\tEnter the imaginary part : "; cin>>x.i;

cout<<"\n\n";

cout<<"Coplex number 1 => "<<x.r<<"+("<<x.i<<")i";

cout<<"\n\n\n\nComplex number 2 : ";

cout<<"\n\n\tEnter the real part : "; cin>>y.r;

cout<<"\n\n\tEnter the imaginary part : "; cin>>y.i;

cout<<"\n\n";

cout<<"\n\nComplex number 2 => "<<y.r<<"+("<<y.i<<")i";

do

{

cout<<"\n\n\nChoose from the folowing : ";

cout<<"\n\n1. Add two complex numbers ";23

Page 24: computer practicals

cout<<"\n\n2. Subtract two complex numbers ";

cout<<"\n\n3. Multiply two complex numbers ";

cout<<"\n\n4. Divide two complex numbers ";

cout<<"\n\nEnter your choice : "; cin>>choice;

switch(choice)

{

case 1:

add(x,y);

break;

case 2:

subtract(x,y);

break;

case 3:

multiply(x,y);

break;

case 4:

divide(x,y);

break;

}

cout<<"\n\nWant to Choose again => ";

cin>>ch;

}

while(ch=='y'||ch=='Y');

getch();

}

Output:

24

Page 25: computer practicals

Complex number 1 : Enter the real part : 2

Enter the imaginary part : 5

Complex number 1 => 2+(5)iComplex number 2 : Enter the real part : 1 Enter the imaginary part : 3Complex number 2 => 1+(3)iChoose from the folowing :

1. Add two complex numbers2. Subtract two complex numbers3. Multiply two complex numbers4. Divide two complex numbers

Enter your choice : 1 Resultant of addition of complex numbers = 3 + (8)iWant to Choose again => YChoose from the folowing :

1. Add two complex numbers2. Subtract two complex numbers3. Multiply two complex numbers4. Divide two complex numbers

Enter your choice : 2 1. Subtract complex 2 from complex 1 : 2. Subtract complex 1 from complex 2 : Enter your choice : 1Resultant of subtraction of complex number => (1) + (2) iWant to Choose again => YChoose from the following :1. Add two complex numbers2. Subtract two complex numbers3. Multiply two complex numbers4. Divide two complex numbersEnter your choice : 3Resultant of multiplication of complex number => (-13) + (11) i

25

Page 26: computer practicals

Want to Choose again => yChoose from the folowing :1. Add two complex numbers2. Subtract two complex numbers3. Multiply two complex numbers4. Divide two complex numbersEnter your choice: 4 1. Divide complex 2 from complex 1: 2. Divide complex 1 from complex 2: Enter your choice: 1Resultant of division of complex number => 1.7 + (-0.1) iWant to Choose again => N

/* A menu drive program for number conversion (User defined).

26

Page 27: computer practicals

#include<iostream.h>

#include<string.h>

#include<ctype.h>

#include<math.h>

#include<conio.h>

int i; long p,a[80];

void decimal_to_binary (long num)

void binary_to_decimal (long num)

void decimal_to_octal (long num)

void octal_to_decimal (long num)

void decimal_to_hexa (long num)

void hexa_to_decimal (char b[80])

void main()

{

clrscr();

int ch; char choice,b[80];

long a;

cout<<"\n\nChoose from the given menu : ";

do

{

cout<<"\n\n1. Binary to decimal ";

cout<<"\n\n2. Decimal to binary ";

cout<<"\n\n\n3. Decimal to octal ";

cout<<"\n\n4. Octal to decimal ";27

Page 28: computer practicals

cout<<"\n\n\n5. Decimal to Hexadecimal ";

cout<<"\n\n6. Hexadecimal to decimal ";

cout<<"\n\n\nEnter your choice : ";

cin>>ch;

switch(ch)

{

case 1: cout<<"\n\nEnter the binary number : ";

cin>>a;

binary_to_decimal(a);

break;

case 2: cout<<"\n\nEnter the decimal number : ";

cin>>a;

decimal_to_binary(a);

break;

case 3: cout<<"\n\nEnter the decimal number : ";

cin>>a;

decimal_to_octal(a);

break;

case 4: cout<<"\n\nEnter the octal number : ";

cin>>a;

octal_to_decimal(a);

break;

case 5: cout<<"\n\nEnter the decimal number : ";

cin>>a;

decimal_to_hexa(a);28

Page 29: computer practicals

break;

case 6: cout<<"\n\nEnter the Hexadecimal value : ";

cin.get();

cin.getline(b,80);

hexa_to_decimal(b);

break;

}

cout<<"\n\n\n\nWant to choose again : ";

cin>>choice;

}

while(choice=='y'||choice=='Y');

getch();

}

void decimal_to_binary(long num)

{

i=0;

p=num;

while(num)

{

a[i]=num%2;

num=num/2;

i++;

}

cout<<"\n\nBinary equivalent of decimal number "<<p<<" => ";

for(int k=i-1; k>=0; k--)

cout<<a[k];29

Page 30: computer practicals

}

void binary_to_decimal(long num)

{

i=0;

p=num;

long no=0;

while(num)

{

a[i]= num%10 * pow(2,i);

no+=a[i];

num=num/10;

i++;

}

cout<<"\n\nDecimal equivalent of binary number "<<p<<" => "<<no;

}

void decimal_to_octal(long num)

{

i=0;

p=num;

while(num)

{

a[i]=num%8;

num=num/8;

i++;

}

cout<<"\n\nOctal equivalent of decimal number "<<p<<" => ";30

Page 31: computer practicals

for(int k=i-1; k>=0; k--)

cout<<a[k];

}

void octal_to_decimal(long num)

{

i=0;

p=num;

long no=0;

while(num)

{

a[i]= num%10 * pow(8,i);

no+=a[i];

num=num/10;

i++;

}

cout<<"\n\nDecimal equivalent of octal number "<<p<<" => "<<no;

}

void decimal_to_hexa(long num)

{

i=0; p=num;

while(num)

{

a[i]= num%16;

num=num/16;

i++;31

Page 32: computer practicals

}

cout<<"\n\nHexadecimal equivalent of decimal number "<<p<<" => ";

for(int k=i-1; k>=0; k--)

{

if(a[k]>=0&&a[k]<10)

cout<<a[k];

else

switch(a[k])

{

case 10:

cout<<"A";

break;

case 11:

cout<<"B";

break;

case 12:

cout<<"C";

break;

case 13:

cout<<"D";

break;

case 14:

cout<<"E";

break;

case 15:

cout<<"F";

break;

}

} 32

Page 33: computer practicals

}

void hexa_to_decimal(char b[80])

{

long d=0;

for(int l=strlen(b)-1,m=0; l>=0; l--,m++)

{

if(isdigit(b[l]))

{

a[l]=b[l];

a[l]=1+a[l]-49; // not value of '1' = 49 & '9'=57 in integer

a[l]=a[l]*pow(16,m);

}

else if(isalpha(b[l]))

{

if(islower(b[l]))

b[l]=toupper(b[l]);

switch(b[l])

{ case 'A' : a[l]=10; a[l]=a[l]* pow(16,m); break;

case 'B' : a[l]=11; a[l]=a[l]* pow(16,m); break;

case 'C' : a[l]=12; a[l]=a[l]* pow(16,m); break;

case 'D' : a[l]=13; a[l]=a[l]* pow(16,m); break;

case 'E' : a[l]=14; a[l]=a[l]* pow(16,m); break;

case 'F' : a[l]=15; a[l]=a[l]* pow(16,m); break;

default : cout<<"\n\nInvalid Hexadecimal number .";

}

}

d+=a[l];33

Page 34: computer practicals

}

cout<<"\n\nThe equivalent decimal number of hexadecimal "<<b<<" => "<<d;

}

Output: -Choose from the given menu:1. Binary to decimal2. Decimal to binary3. Decimal to octal4. Octal to decimal5. Decimal to Hexadecimal6. Hexadecimal to decimal

Enter your choice : 1Enter the binary number : 100011Decimal equivalent of binary number 100011 => 35

Want to choose again : y1. Binary to decimal2. Decimal to binary3. Decimal to octal4. Octal to decimal5. Decimal to Hexadecimal6. Hexadecimal to decimal

Enter your choice : 2Enter the decimal number : 35

Binary equivalent of decimal number 35 => 100011Want to choose again : y

1. Binary to decimal2. Decimal to binary3. Decimal to octal4. Octal to decimal5. Decimal to Hexadecimal6. Hexadecimal to decimalEnter your choice : 3Enter the decimal number : 25Octal equivalent of decimal number 25 => 31Want to choose again : y

34

Page 35: computer practicals

1. Binary to decimal2. Decimal to binary3. Decimal to octal4. Octal to decimal5. Decimal to Hexadecimal6. Hexadecimal to decimal

Enter your choice: 4Enter the octal number: 31

Decimal equivalent of octal number 31 => 25

Want to choose again: y1. Binary to decimal2. Decimal to binary3. Decimal to octal4. Octal to decimal5. Decimal to Hexadecimal6. Hexadecimal to decimal

Enter your choice :5Enter the decimal number : 26Hexadecimal equivalent of decimal number 26 => 1A

Want to choose again : y

1. Binary to decimal2. Decimal to binary3. Decimal to octal4. Octal to decimal5. Decimal to Hexadecimal6. Hexadecimal to decimalEnter your choice :6Enter the Hexadecimal value : 1AThe equivalent decimal number of hexadecimal 1A => 26Want to choose again : N

/* A menu drive program to Implement STACK as an array

#include<iostream.h>

35

Page 36: computer practicals

#include<conio.h>#include<process.h>

int pop (int[],int&);int push (int[],int&,int);void display (int[],int);

const int size=50;

void main(){ clrscr(); char m,ch; int k,stack[size],item,top=-1,res; do {

cout<<"\nChoose from the following : \n\n"cout<<"\n 1. Push"cout<<"\n 2. Pop"cout<<"\n 3. Display"cout<<"\n 4. Exit"cout<<"\n\nEnter your choice : "; cin>>k;

switch(k) { case 1:

ch='y'; while(ch=='y'||ch=='Y') {

cout<<"\nEnter the element : ";cin>>item;res=push(stack,top,item);if(res==-1){cout<<"\nOverflow !!!!";exit(1); }

36

Page 37: computer practicals

cout<<"\nThe stack formed is : \n\n";display(stack,top);cout<<"\n\n\nWant to enter again ?: ";cin>>ch;

} break;

case 2: ch='y'; while(ch=='y'||ch=='Y') {

res=pop(stack,top);if(res==-1){ cout<<"\nUnderflow !!!!"; exit(1); }else{ cout<<"\nThe deleted Element is : "<<res<<endl; cout<<"\nThe resultant stack is : \n\n"; display(stack,top); } cout<<"\nWant to delete again ? : "; cin>>ch; }

break; case 3:

cout<<"\nThe resultant stack is : "; display(stack,top); break;

case 4: exit(0);

break; default: cout<<"\nPlease enter desired keyword : "; } cout<<"\n\nChoose from the menu again ? : "; cin>>m;

37

Page 38: computer practicals

}while(m=='y'||m=='Y');getch();

} // end of main()

int push(int stack[],int &top,int el){

if(top==size-1) return -1; else { top++; stack[top]=el; return 0;

}}

int pop(int stack[],int &top){ int ret; if(top==-1) return -1; else {

ret=stack[top]; top--; return ret; }}

void display(int stack[],int top){ cout<<stack[top]<<"<--"; for(int i=top-1;i>=0;i--) cout<<stack[i]<<"<--"; }

Output:Choose from the following : 1. Push

38

Page 39: computer practicals

2. Pop 3. Display 4. ExitEnter your choice : 1Enter the element : 1The stack formed is :1 Want to enter again ?: yEnter the element : 2The stack formed is :2 1 Want to enter again ?: yEnter the element : 3The stack formed is :3 2 1 Want to enter again ?: yEnter the element : 4The stack formed is :4 3 2 1 Want to enter again ?: NChoose from the menu again ? : yChoose from the following : 1. Push 2. Pop 3. Display 4. ExitEnter your choice : 2The deleted Element is : 4The resultant stack is :3 2 1 Want to delete again ? : yThe deleted Element is : 3The resultant stack is :2 1 Want to delete again ? : nChoose from the menu again ? : yChoose from the following :

39

Page 40: computer practicals

1. Push 2. Pop 3. Display 4. ExitEnter your choice : 3The resultant stack is : 2 1 Choose from the menu again ? : n

/* A menu drive program to implement stack as a linked list */

#include<iostream.h>

40

Page 41: computer practicals

#include<conio.h>#include<process.h>

struct node { int roll; node* next;}*top,*save,*ptr,*newptr,*np;

node *create(int a){ ptr=new node; ptr->roll=a; ptr->next=NULL; return ptr;}

void push(node *np){ if(top==NULL)

top=np; else

{ save=top; top=np; np->next=save;}

}

void pop(){ if(top==NULL) cout<<"\n Underflow!!!!"; else { ptr=top; top=top->next; delete ptr; }}

void display(node *np){

41

Page 42: computer practicals

while(np!=NULL){

cout<<np->roll<<" -> ";np=np->next;

}}

void main(){ clrscr(); top=NULL; int n,m; char k,ch;

do { cout<<"\nChoose from the menu :\n"

cout<<"\n 1. Push."cout<<"\n 2. Pop."cout<<"\n 3. Display."cout<<"\n 4. Quit."cout<<"\n\nEnter your choice : ";cin>>n;

switch(n) { case 1:

k='y'; while(k=='y'||k=='Y') {

cout<<"\n Enter element to be inserted .";cin>>m;newptr=create(m);if(newptr==NULL)

cout<<"\n Cannot create !!!!";push(newptr);cout<<"\n The Stack formed is : ";display(top);cout<<"\n\n Want to enter again ?: ";cin>>k;

} break;

case 2: k='y';

while(k=='y'||k=='Y')42

Page 43: computer practicals

{pop();cout<<"\n The Stack formed is : \n\n";display(top);cout<<"\n\n Want to delete again ?: ";cin>>k;

} break;

case 3: cout<<"\n The Stack formed is : "; display(top); break;

case 4: exit(0); break;

default: cout<<"\n Please enter desired keyword : "; } cout<<"\n Do you want to continue..? : "; cin>>ch;}

while(ch=='y'||ch=='Y');getch();}

Output:Choose from the menu :

43

Page 44: computer practicals

1. Push. 2. Pop. 3. Display. 4. Quit.Enter your choice : 1Enter element to be inserted : 1The Stack formed is : 1 ->Want to enter again ?: yEnter element to be inserted : 2The Stack formed is : 2 -> 1 ->Want to enter again ?: yEnter element to be inserted : 3The Stack formed is : 3 -> 2 -> 1 ->Want to enter again ?: nDo you want to continue..? : yChoose from the menu : 1. Push. 2. Pop. 3. Display. 4. Quit.Enter your choice : 2The Stack formed is : 2 -> 1 ->Want to delete again ?: yThe Stack formed is :1 ->Want to delete again ?: NDo you want to continue..? : yChoose from the menu : 1. Push. 2. Pop. 3. Display. 4. Quit.Enter your choice : 3

44

Page 45: computer practicals

The Stack formed is : 1 ->Do you want to continue..? : yChoose from the menu : 1. Push. 2. Pop. 3. Display. 4. Quit.Enter your choice : 4

/* A Menu drive program to implement queue as an array */

45

Page 46: computer practicals

#include<iostream.h>#include<conio.h>#include<process.h>

const int size=50;

int q[size],rear=-1,front=-1;

int insert(int q[],int ele){ if(rear==size-1) return(-1); else if(rear==-1) { front=rear=0; q[rear]=ele; } else {

rear++; q[rear]=ele; } return(0);}

int delet(int q[]){ int r ; if(front==-1) return(-1); else {

r=q[front]; if(rear==front) front=rear=-1; else front++; } return r;}

46

Page 47: computer practicals

void display(int q[],int front,int rear){

if(front==-1) return; else { for(int i=front;i<=rear;i++) cout<<q[i]<<" "; }}

void main(){ clrscr(); int n,u,k,m; char ch,ans; do {

cout<<"\nChoose from the menu :\n"<<"\n 1. Insert"<<"\n 2. Delete"<<"\n 3. Display"<<"\n\n Enter your choice : "; cin>>n;

switch(n) { case 1: ans='y';

while(ans=='y'||ans=='Y') {

cout<<"\n Enter element to be inserted :"; cin>>m;

k=insert(q,m);

if(k==-1)cout<<"\n Overflow !!!!";

cout<<"\n The resultant Queue is : ";

display(q,front,rear);

cout<<"\n\n Want to enter again ?: ";

cin>>ans; 47

Page 48: computer practicals

} break;

case 2: ans='y'; while(ans=='y'||ans=='Y') {

u=delet(q);if(u==-1)

{ cout<<"\n Underflow !!!!";

break;}

else{cout<<"\n The deleted element is "<<u<<"\n";cout<<"\n The resultant Queue is : \n\n";display(q,front,rear); }

cout<<"\n\n Want to delete again ?: ";cin>>ans;

}

break;

case 3: cout<<"\n The Queue is : \n\n"; display(q,front,rear); break;

default: cout<<"\n Please enter desired keyword : "; }

cout<<"\n Choose from the menu again ? : ";cin>>ch;

}while(ch=='y'||ch=='Y'); getch();}

Output:Choose from the menu :

48

Page 49: computer practicals

1. Insert 2. Delete 3. Display

Enter your choice : 1 Enter element to be inserted :1 The resultant Queue is : 1-->Want to enter again ?: yEnter element to be inserted :2The resultant Queue is : 1-->2-->Want to enter again ?: yEnter element to be inserted :3The resultant Queue is :1-->2-->3-->Want to enter again ?: nChoose from the menu again ? : yChoose from the menu :1. Insert2. Delete3. DisplayEnter your choice : 2The deleted element is 1The resultant Queue is :2-->3-->Want to delete again ?: yThe deleted element is 2The resultant Queue is :3-->Want to delete again ?: nChoose from the menu again ? : yChoose from the menu : 1. Insert 2. Delete 3. Display Enter your choice : 3 The Queue is : 3-->Choose from the menu again ? :N

/* Menu drive program to implement Queue as a linked list */

49

Page 50: computer practicals

#include<iostream.h>#include<conio.h>

struct node {

int roll;

node* next;

}*front,*rear,*ptr,*newptr,*np;

node *create(int a)

{

ptr=new node;

ptr->roll=a;

ptr->next=NULL;

return ptr;

}

void insert(node *np)

{

if(front==NULL)

front=rear=np;

else

{

rear->next=np;

rear=np;

}

}

void delet()

{

if(front==NULL)

cout<<"\n Underflow!!!!";

else

50

Page 51: computer practicals

{

ptr=front;

front=front->next;

delete ptr;

}

}

void display(node *np)

{

while(np!=NULL)

{

cout<<np->roll<<"-> ";

np=np->next;

}

}

void main()

{

clrscr();

front=rear=NULL;

int n,m;

char ans,ch;

do

{

cout<<"\nChoose from the menu : "

<<"\n 1) Insert."

<<"\n 2) Delete

<<"\n 3) Display."

<<"\n\n Enter your choice : ";

cin>>n;

51

Page 52: computer practicals

switch(n)

{

case 1: ans='y';

while(ans=='y'||ans=='Y')

{

cout<<"\n Enter element to be inserted .";

cin>>m;

newptr=create(m);

if(newptr==NULL)

cout<<"\n Cannot create !!!!";

insert(newptr);

cout<<"\n The Queue formed is : ";

display(front);

cout<<"\n Want to enter more nodes ?: ";

cin>>ans;

}

break;

case 2: ans='y';

while(ans=='y'||ans=='Y')

{

delet();

cout<<"\n Queue : ";52

Page 53: computer practicals

display(front);

cout<<"\n Want to delete more ?: ";

cin>>ans;

}

break;

case 3: cout<<"\n Queue : ";

display(front);

break;

default: cout<<"\n You entered wrong choice...";

}

cout<<"\n Want to return to main menu ? : ";

cin>>ch;

}

while(ch=='y'||ch=='Y');

getch();

}

OUTPUTChoose from the menu :

1. Insert

2. Delete

3. Display

Enter your choice : 1

Enter element to be inserted : 1

The Queue formed is :

1-->

Want to enter more nodes ?: y

Enter element to be inserted .2

The Queue formed is :53

Page 54: computer practicals

1-> 2->

Want to enter more nodes ?: y

Enter element to be inserted .3

The Queue formed is :

1-> 2-> 3->

Want to enter more nodes ?: n

Want to return to main menu ? : y

Choose from the menu :

1. Insert

2. Delete

3. Display

Enter your choice : 2

Queue :

2-> 3->

Want to delete more ?: n

Want to return to main menu ? : y

Choose from the menu : n

1. Insert

2. Delete

3. Display

Enter your choice : 3

Queue :

2-> 3->

Want to return to main menu ? : n

/* Program to create a text file(.txt) and display number of words, alphabets, vowels and consonants and number of lowercase and uppercase letters using the concept of DATA FILE HANDLING. */

54

Page 55: computer practicals

#include<fstream.h>#include<string.h>#include<ctype.h>#include<conio.h>void main(){clrscr();char a[80];

int words=0, upper_letters=0, lower_letters=0, alpha=0, vowels=0,consonants=0;ofstream string("str.txt");cout<<"\n\nEnter the string : ";cin.getline(a,79);string<<"The string is : "<<a<<"\n\n";for(int i=0; i<strlen(a); i++){

if(a[i]==' ') while(a[i]==' ')

{i++;

}

if(isalnum(a[i])) { while(a[i]!=' ')

{ i++; }

words++; }

}string<<"\n\nThe number of words in string are : "<<words;string<<"\n\n\nAlphabets in string are : \n\n";for(i=0; i<strlen(a); i++){ if(isalpha(a[i])) { alpha++; string<<a[i]<<" "; }}string<<"\n\nTotal number of alphabets in string => "<<alpha;string<<"\n\n\nUppercase letters in string are : \n\n";

55

Page 56: computer practicals

for(i=0; i<strlen(a); i++){ if(isupper(a[i])) { upper_letters++; string<<a[i]<<" "; }}string<<"\n\nTotal number of uppercase letters in string => "<<upper_letters;string<<"\n\n\nLowercase letters in string are : \n\n";for(i=0; i<strlen(a); i++){ if(islower(a[i])) { lower_letters++; string<<a[i]<<" "; }}string<<"\n\nTotal number of Lowercase letters in string => "<<lower_letters;string<<"\n\n\nVowels in string are : \n\n";for(i=0; i<strlen(a); i++){ if(isalpha(a[i])) {

if(a[i]=='a'||a[i]=='A'||a[i]=='e'||a[i]=='E'||a[i]=='i'||a[i]=='I'||a[i]=='o'||a[i]=='O'||a[i]=='u'||a[i]=='U')

{ vowels++; string<<a[i]<<" "; } }}string<<"\n\nTotal number of vowels in string => "<<vowels;

string<<"\n\n\nConsonants in string are : \n\n";for(i=0; i<strlen(a); i++){ if(isalpha(a[i])) { if(a[i]!='a'&&a[i]!='A'&&(a[i]!='e'&&a[i]!='E'&&a[i]!='i'&&a[i]!='I'&&(a[i]!='o'&&a[i]!='O')&&a[i]!='u'&&a[i]!='U') { consonants++; string<<a[i]<<" ";

56

Page 57: computer practicals

} }}string<<"\n\nTotal number of vowels in string => "<<consonants;getch();}

Output : Enter the string : Rahul Verma

Output of created text file: The string is : Rahul Verma

The number of words in string are : 2

Alphabets in string are :

R a h u l V e r m a

Total number of alphabets in string => 10

Uppercase letters in string are :

R V

Total number of uppercase letters in string => 2

Lowercase letters in string are :

a h u l e r m a

Total number of Lowercase letters in string => 8

Vowels in string are :

a u e a

Total number of vowels in string => 4

Consonants in string are :

R h l V r m

Total number of vowels in string => 6

/* Program to read a text file and create a duplicate file by toggling the characters using the concept of data file handling. */

#include<fstream.h>

57

Page 58: computer practicals

#include<ctype.h>#include<string.h>#include<conio.h>void main(){clrscr();

char a[80][80],choice;int i=0;

cout<<"Enter the strings. Enter ## to terminate entering: ";l1: label 1cin.getline(a[i],80);if(a[i][0]=='#'&&a[i][1]=='#') goto l2; else

{ i++; goto l1;}

l2: // label 2 fstream str;

str.open("str_n.txt",ios::out);str<<"The strings are : \n\n";for(int j=0; j<i; j++)str<<a[j]<<"\n\n";str.close();char b[80][80];str.open("str_n.txt",ios::in);for( j=0; j<=(i*2)+1; j++)str.getline(b[j],80);str.close();for(j=0; j<=(i*2)+1; j++)for(int k=0; k<strlen(b[j]); k++){if(isalpha(b[j][k]))

{

58

Page 59: computer practicals

if(islower(b[j][k])) {

b[j][k]=toupper(b[j][k]); }else if(isupper(b[j][k])) {

b[j][k]=tolower(b[j][k]); }

}}str.open("string_2.txt",ios::out);str<<"Toggled strings are : \n\n";for(j=0; j<=(i*2)+1; j++)str<<b[j]<<"\n\n";str.close();getch();}

Output:Enter the strings. Enter ## to terminate entering: Eatanapplea day.Keeps the doctor away.##

// Under MS-DOS shellD:\TURBOC3>type str_n.txt The strings are :Eatanapplea day.Keeps the doctor away.

59

Page 60: computer practicals

D:\TURBOC3>type string_2.txtToggled strings are :tHE STRINGS ARE :eATaNaPPLEA dAY.kEEPS THE DOCTOR AWAY.

/* Program to create a text file to input roll no. and marks of ten students and display them on screen after reading from the text file, using data file handling. */

60

Page 61: computer practicals

#include<fstream.h>

#include<conio.h>

void main()

{

clrscr();

int a[5]; float b[5];

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

{

cout<<"\n\nEnter the roll no. of student "<<i+1<<" : "; cin>>a[i];

cout<<"\n\tEnter the marks of student "<<i+1<<" => "; cin>>b[i];

}

ofstream student;

student.open("stud.txt");

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

{

student<<"\n\nMarks of roll no. "<<a[i]<<" => "<<b[i];

}

student.close();

i=0;

cout<<”\n\nStudent details are :\n\n”;

char str[80][80];

ifstream stude;

stude.open("stud.txt",ios::in);

stude.seekg(0);

while(!stude.eof())

{

stude.getline(str[i],80);

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

i++;61

Page 62: computer practicals

}

getch();

}

Output:Enter the roll no. of student 1 : 2

Enter the marks of student 1 => 95

Enter the roll no. of student 2 : 4

Enter the marks of student 2 => 96

Enter the roll no. of student 3 : 8

Enter the marks of student 3 => 99

Enter the roll no. of student 4 : 10

Enter the marks of student 4 => 91

Enter the roll no. of student 5 : 12

Enter the marks of student 5 => 89

Student Details are :

Marks of roll no. 2 => 95

Marks of roll no. 4 => 96

Marks of roll no. 8 => 99

Marks of roll no. 10 => 91

Marks of roll no. 12 => 89

// Under MS-DOS shellD:\TURBOC3>type stud.txt

Marks of roll no. 2 => 95

Marks of roll no. 4 => 96

Marks of roll no. 8 => 99

Marks of roll no. 10 => 91

Marks of roll no. 12 => 89

62

Page 63: computer practicals

/* Program to read file “sports.dat” and copy only those records where event name is ATHLETIC using the concept of Data file handling */

#include<fstream.h>63

Page 64: computer practicals

#include<string.h>

#include<conio.h>

#include<stdio.h>

#include<stdlib.h>

struct sports

{

char event[20];

char participants[10][30];

int no_of_participants;

} s[20], s2[20];

void copy(fstream &ob);

int i=0;

void main()

{

clrscr();

char choice;

fstream ob("sports.dat",ios::binary|ios::in|ios::out);

do

{

if(i>0)

cin.get();

cout<<"\n\nEnter the name of Event : ";

cin.getline(s[i].event,20);

cout<<"\n\nEnter the total number of participants in Event"<<s[i].event<<" : ";

cin>>s[i].no_of_participants;

cout<<"\n\nEnter the name of Praticipants : \n";

cin.get();64

Page 65: computer practicals

for(int j=0; j<s[i].no_of_participants; j++)

cin.getline(s[i].participants[j],30);

ob.write((char*)&s[i], sizeof(sports));

cout<<"\n\n\nWant to Enter Details of Another Event (Y/N) : ";

cin>>choice;

i++;

}

while(choice=='y'||choice=='Y');

cout<<"\n\n\n\n\n*****************************************************\n\n";

copy(ob);

cout<<"\n\n*********************************************************\n\n\n";

getch();

}

void copy(fstream &o)

{

sports s[20];

o.seekg(0);

ofstream file;

file.open("athletic.dat",ios::binary);

file.seekp(0);

int j,n=0;

int c=0;

while(o)

{

o.read((char*)&s[c], sizeof(sports));

if(strcmp("athletic",s[c].event)==0)

{

file.write((char*)&s[c], sizeof(sports));

n=1;65

Page 66: computer practicals

break;

}

c++;

}

if(n==0)

{

cout<<"\n\nUnsuccessful Search.";

getch();

exit(0);

}

o.close();

file.close();

sports sp;

ifstream oo;

oo.open("athletic.dat",ios::binary);

while(oo)

{

oo.read((char*)&sp, sizeof(sports));

}

cout<<"\n\nThe Records of file are : \n\n";

cout<<"\n\nEvent = "<<sp.event;

cout<<"\n\n\n\nThe Participants are : \n\n";

for(int i=0; i<sp.no_of_participants; i++)

{

cout<<sp.participants[i]<<"\n\n";

}

oo.close();66

Page 67: computer practicals

remove("athletic.dat");

remove("sports.dat");

}

Output : -Enter the name of Event : Cricket

Enter the total number of participants in Event Cricket : 3

Enter the name of Praticipants :

Rahul verma

Shivam

Siddharth

Want to Enter Details of Another Event (Y/N) : y

Enter the name of Event : athleltic

Enter the total number of participants in Event atlhletic : 2

Enter the name of Praticipants :

Mohak

Vikas

Want to Enter Details of Another Event (Y/N) : y

Enter the name of Event : Football

Enter the total number of participants in Event Football : 2

Enter the name of Praticipants :

Arsh

Ashu

Want to Enter Details of Another Event (Y/N) : n

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

The Records of file are :

Event = athletic

The Participants are :67

Page 68: computer practicals

Mohak

Vikas

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

/* Program to print and find the sum of Fibonacci series using recursion. */

#include<iostream.h>

#include<conio.h>68

Page 69: computer practicals

int fibonacci(int n);

void main()

{

clrscr();

int n;

cout<<"\n\n Enter the number of terms upto which you want the sum of

fibonnaci series : ";

cin>>n;

cout<<"\n\nThe fibonacci series generated is : \n\n";

cout<<"1 1 ";

int s=fibonacci(n);

cout<<"\n\nThe sum of fibonacci series for "<<n<<" terms = "<<s;

getch();

}

int first=1;

int second=1;

int third;

int i=2;

int sum=0;

int fibonacci(int n)

{

if(n==1)

sum=1;

else if(n==2)

sum=2; // n = 1 2 3 4 5

else if(n>1 && i!=n) // num = 1 1 2 3 5

{

third=first+second;69

Page 70: computer practicals

cout<<third<<” “;

if(i==2)

sum+=first+second+third;

else

sum+=third;

first=second;

second=third;

++i;

fibonacci(n);

}

return sum;

}

Output : Enter the number of terms upto which you want the sum of Fibonacci series : 5

The Fibonacci series generated is :

1 1 2 3 5

The sum of Fibonacci series for 5 terms = 12

/* Program to print and find the sum of even /odd numbers using recursion.*/

#include<iostream.h>

#include<conio.h>70

Page 71: computer practicals

int sum_even(int );

int sum_odd(int );

int sum=0;

int num=0;

void main()

{

clrscr();

int n;

int s_e, s_o;

cout<<"\n\nEnter the number upto which you want the sum of even/odd

numbers : ";

cin>>n;

cout<<"\n\n The list of integers is : \n\n";

for(int l=1; l<=n; l++)

cout<<l<<"\n";

s_e=sum_even(n);

s_o=sum_odd(n);

cout<<"\n\nThe sum of even numbers upto "<<n<<" = "<<s_e;

cout<<"\n\nThe sum of odd numbers upto "<<n<<" = "<<s_o;

getch();

}

int sum_even(int n)

{

if(num%2==0)

sum=sum+num;

if(num!=n && num<n)

{71

Page 72: computer practicals

++num;

sum_even(n);

}

return sum;

}

int sum_2=0;

int num_2=0;

int sum_odd(int n)

{

if(num_2%2!=0)

sum_2=sum_2+num_2;

if(num_2!=n)

{

num_2++;

sum_odd(n);

}

return sum_2;

}

Output:Enter the number upto which you want the sum of even/odd numbers : 10

72

Page 73: computer practicals

The list of integers is :12345678910The sum of even numbers upto 10 = 30The sum of odd numbers upto 10 = 25

/*Program to find the smallest and the largest element in a dynamically created array using pointers.*/

#include<iostream.h>

73

Page 74: computer practicals

#include<conio.h>

void main(){

clrscr();int *array, smallest, largest, n;cout<<"\n\nEnter the number of elements : ";cin>>n;array=new[n];cout<<"\n\nEnter the elements : \n\n";for(int i=0; i<n; i++)cin>>array[i];i=0;cout<<"\n\nThe array formed is : \n\n";

while(i!=n){

cout<<array[i]<<" ";i++;

}smallest=array[0];for(i=0; i<n; i++){if(array[i]<=smallest)

smallest=array[i];}

largest=array[0];for(i=0; i<n; i++){

if(array[i]>=largest)largest=array[i];

}cout<<"\n\nThe smallest element is : "<<smallest<<"\n\nThe largest element is : "<<largest;getch();}

Output :

Enter the number of elements : 5

Enter the elements :

74

Page 75: computer practicals

1

9

2

8

6

The array formed is :

1 9 2 8 6

The smallest element is : 1

The largest element is : 9

/* Program to swap two integers using pointers. */

#include<iostream.h>

#include<conio.h>

75

Page 76: computer practicals

void swap_using_pointers(int *, int *);

void main()

{

clrscr();

int a,b;

cout<<"\n\nEnter first integer : "; cin>>a;

cout<<"\n\nEnter second integer : "; cin>>b;

swap_using_pointers(&a,&b);

cout<<"\n\nNow value of first integer = "<<a;

cout<<"\n\nNow value of second integer = "<<b;

getch();

}

void swap_using_pointers(int *a,int *b)

{

int temp;

temp=*a;

*a=*b;

*b=temp;

}

Output:

Enter first integer : 1

Enter second integer : 100

Now value of first integer = 100

Now value of second integer = 1

76

Page 77: computer practicals

/*Program to find the length of a string and print the reversed string using pointers. */

#include<iostream.h>

77

Page 78: computer practicals

#include<string.h>

#include<conio.h>

void main()

{

clrscr();

char *str= new char[256];

cout<<"\n\nEnter the string : ";

cin.getline(str,256);

cout<<"\n\nThe string length => "<<strlen(str);

cout<<"\n\nThe reverse string is : ";

char intermediate;

for(int j=strlen(str)-1,i=0; i<=strlen(str)/2; j--,i++)

{

intermediate = *(str+j);

*(str+j)=*(str+i);

*(str+i)=intermediate;

}

cout<<str;

getch();

}

Output :

Enter the string : [email protected]

The string length => 26

78

Page 79: computer practicals

The reverse string is : moc.liamffider@inoscryajiv

79