Computer Practical Practice, Exercises of Computer Science

Computer Science Practical practice

Typology: Exercises

2021/2022

Available from 08/31/2022

ali-ahmed-durrani
ali-ahmed-durrani 🇵🇰

47 documents

1 / 49

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PROGRAMS
1.
Binary Search:
#include<conio.h>
#include<iostream.h>
int Binary_Search(int a[], int N, int data)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31

Partial preview of the text

Download Computer Practical Practice and more Exercises Computer Science in PDF only on Docsity!

PROGRAMS

Binary Search:

#include<conio.h>

#include<iostream.h>

int Binary_Search(int a[], int N, int data)

{ int first,last,mid; int found=0;

first=0; last=N-1;

while(first<=last && found==0)

mid=int((first+last)/2);

if(a[mid]==data)

found=1; if(a[mid]<data)

first=mid+1;

if(a[mid]>data)

last=mid-1;

return(found);

int main()

{ clrscr(); int x[10],data, i; cout<<"\n Enter 10

numbers in ascending order:"; for(i=0;i<10;i++)

cin>>x[i];

cout<<"\n Enter No. to be searched:";

cin>>data; int

res=Binary_Search(x,10,data);

if(res==1) cout<<"\n found"; else

found

Linear Search #include<conio.h> #include<iostream.h> int Linear_Search (int x[],int N,int data) { int i, found=0; for(i=0;i<N;i++) { if(x[i]==data) found=1; } return(found); } int main() { clrscr(); int x[10],data, i; cout<<"\n Enter 10 numbers:"; for(i=0;i<10;i++) { cin>>x[i]; }

cout<<"\n Enter No. to be searched:"; cin>>data; int res=Linear_Search(x,10,data); if(res==1) cout<<"\n found"; else cout<<"\n not found"; getch(); return 0; } /* Enter 10 numbers: 2 3 4 5 6 7 8 9 10 Enter No. to be searched: found */

// Output Phase cout<<"\n Sorted Array"; for(i=0;i<10;i+ +) { cout<<setw(6)<<x[i]; } getch(); return 0; } /* Enter 1 value 3 Enter 2 value 45 Enter 3 value 67 Enter 4 value 88 Enter 5 value 12 Enter 6 value 90 Enter 7 value 34 Enter 8 value 76 Enter 9 value 29 Enter 10 value 12 Sorted Array 3 12 12 29 34 45 67 76 88 90 */

4.Selection Sort #include<conio.h> #include<iostream.h> #include<iomanip.h> void main() { clrscr(); int x[10],i,j,temp,low,pos; // input cout<<"\n Enter 10 numbers to be sort:"; for(i=0;i<10;i++) cin>>x[i]; // Processing for(i=0;i<9;i++) { low=x[i]; pos=i; for(j=i+1;j<10;j++) { if(low>x[j]) { low=x[j]; pos=j; } } temp=x[i]; x[i]=x[pos]; x[pos]=temp; } // end of i for loop

#include<iomanip.h> void main() { clrscr(); int x[10],i,j,temp; // input cout<<"Enter 10 numbers to be sort:"; for(i=0;i<10;i++) cin>>x[i]; // Processing for(i=1;i<10;i++) { temp=x[i]; j=i-1; while(temp<x[j] && j>=0) { x[j+1]=x[j]; j=j- 1; } x[j+1]=temp; } // output cout<<"\n sorted Array:"; for(i=0;i<10;i++) cout<<setw(4)<<x[i]; getch(); return; }

Enter 10 numbers to be sort: 23 65 87 66 12 16 62 94 74 90 sorted Array: 12 16 23 62 65 66 74 87 90 94 */ 6.Copy Constructor #include<conio.h> #include<iostream.h> #include<string.h> class student { int roll; char name[30]; public: student()

student s; s.show_data(); cout<<"\n"; student A(s); A.show_data(); getch(); return 0; } /* Constructor: Roll no: Name:Rahul Copy constructor: Roll no: Name:Rahul */

7.Parametrised Constructor #include<conio.h> #include<iostream.h> class read_constructor { int x; public: read_constructor(int a) { x=a ; } void read_data() { cin>>x; } void show_data() { cout<<"Value of x:"<<x;

8.Lower Matrix #include<conio.h> #include<iostream.h> #include<iomanip.h> int main() { clrscr(); int x[4][4]= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; int i,j; // output phase cout<<"Lower matrix:"; for(i=0;i<4;i++) { cout<<endl; for(j=0;j<4;j++) if(i>=j) cout<<setw(6)<<x[i][j]; else cout<<setw(6)<<" "; }

getch(); return 0; } /* Lower matrix: 1 5 6 9 10 11 13 14 15 16 */

Sum of elements of matrix : The matrix is: 10 9 8 8 3 4 */ 10.Nesting #include<conio.h> #include<iostream.h>

class area { int b, h; double ar; public: void input_data() { cout<<"\n Enter base:"; cin>>b; cout<<"\n Enter height:"; cin>>h; } void calculate() { input_data(); ar=0.5bh; } void output() { calculate(); cout<<"\n Area of triangle:"<<ar; } }; int main()