









































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Computer Science Practical practice
Typology: Exercises
1 / 49
This page cannot be seen from the preview
Don't miss anything!










































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()