Program of List Using Array
Program of List Using Array
int arr[2]; int n; /*Total number of elements in the list */ void input(int); int search(int item); void insert(); void del(); void display(); // del(); //display(); int main() { int choice,item,pos; while(1) { cout<<"1.Input list\n"; cout<<"2.Insert\n"; cout<<"3.Search\n"; cout<<"4.Delete\n"; cout<<"5.Display\n";
switch(choice) { case 1: cout<<"Enter the number of elements to be entered : "; cin>>n; input(n); break; case 2: insert(); break; case 3: cout<<"Enter the element to be searched : "; cin>>item; pos = search(item); if(pos >= 1) cout<<" found at position \n"<<item<<pos; else cout<<"Element not found\n"; break; case 4: del();
break; case 5: display(); break; case 6: return 0; break; default: cout<<"Wrong choice\n"; } /*End of switch */ }/*End of while */ }/*End of main() */
void input(int n) { int i; for(i = 0; i< n ; i++) { cout<<"Input value for element : "<<i+1; cin>>arr[i]; } }/*End of input()*/
int i; for(i=0; i < n; i++) { if(item == arr[i]) return(i+1); } return(0); /* If element not found */ }/*End of search()*/
void insert() { int temp,item,position; if(n == MAX) { cout<<"List overflow\n"; return; } cout<<"Enter position for insertion : "; cin>>position; cout<<"Enter the value : "; cin>>item; if(position > n+1 ) { cout<<"Enter position less than or equal to \n"<<n+1; return;
} if( position == n+1 ) /*Insertion at the end */ { arr[n] = item; n = n+1; return; } /* Insertion in between */ temp=n-1; while( temp >= position-1) { arr[temp+1] = arr[temp]; /* shifting right */ temp --; } arr[position-1] = item; n = n +1 ; }/*End of insert()*/
} cout<<"Enter the element to be deleted : "; cin>>item; if(item==arr[n-1]) /*Deletion at the end*/ { n = n-1; return; } position=search(item); if(position==0) { cout<<"Element not present in array\n"; return; } /*Deletion in between */ temp=position-1; while(temp <= n-1) { arr[temp] = arr[temp+1]; /* Shifting left */ temp ++; } n=n-1; }/*End of del()*/
void display()
{ int i; if(n==0) { cout<<"List is empty\n"; return; } for(i = 0; i< n; i++) cout<<"Value at position \n"<<i+1<<arr[i]; }/*End of display()*/
cout<<"How many elements you want to enter in the array "; cin>>n;
for(i=0; i < n;i++) { cout<<"Enter element "; cin>>arr[i]; } cout<<"Enter the element to be searched : "; cin>>item; start=0; end=n-1; middle=(start+end)/2; while(item != arr[middle] && start <= end) { if(item > arr[middle]) start=middle+1; else
end=middle-1; middle=(start+end)/2; } if(item==arr[middle]) printf("%d found at position %d\n",item,middle+1); if(start>end) printf("%d not found in array\n",item); }/*End of main()*/
} main() { int x,y; clrscr(); cout<<"Enter a number:";//8 cin>>x; cout<<"Enter a number:";//8 cin>>y; swap(x,y); cout<<endl<<" x and y="<<x<<"\t"<<y; getch(); }
#include <stdio.h> #include <conio.h> #include <iostream.h> class subtraction { private: int liNumber1; int liNumber2; public: void getData(); //Declaration of function void Sub();
};//end of the class //Defination of function void subtraction::getData() { cout<<"Enter first number:"; cin>>liNumber1; cout<<"Enter second number:"; cin>>liNumber2; } void subtraction::Sub() {
cout<<endl<<"Subtraction="<<liNumber1-liNumber2; } void main() { subtraction obj; // similar int i; clrscr(); obj.getData(); obj.Sub(); getch();
#include <stdio.h> #include <conio.h> #include <iostream.h> class abc { static int count;//here we just declare it public: void display() { count++; cout<<endl<<"Display called "<<count<<" items"; }
//Define a class to calculate the factorial of the number given by the user.Intialize the factorial to 1 at the creation of each object. If the number is zero or less default factorial value needs to displayed.
#include <stdio.h> #include <iostream.h> class Factorial { int f; public: Factorial()//default constructor { cout<<"default constructor:"; f=1; } int factorial(int no) { //4 if(no<=0) cout<<endl<<"Factorial="<<f; else { while(no>1) { f=f*no; no--; //cout<<endl<<Factorial<<f; }
return l*b; }
};
main() {
rectangle r1(3,2); rectangle r2=r1;//initialize one object from another cout<<"area of r1:" <<r1.area(); cout<<endl<<"area of r2:" <<r2.area(); getch(); }
//example of Destructor
#include <stdio.h> #include <conio.h> #include <iostream.h> class rectangle { int l,b; public: rectangle() { l=0; b=0;
};
main() {
rectangle r1;
getch(); }
{ rectangle tmp; tmp.l=r1.l+r2.l; tmp.b=r1.b+r2.b; return tmp; } void main() { rectangle r1(3,3),r2(2,2); rectangle r3; r3=sum(r1,r2); cout<<endl<<"Area of r3:"<<r3.area(); }
public: index() { count=0; } index(int i) { count=i; } index operator++() //prefix { return index(++count); } index operator++(int) //postfix { return index(count++);
//program of inheritance
#include <stdio.h> #include <conio.h> #include <iostream.h> class Radio { public: Radio() { cout<<endl<<"Radio created"; } }; class Mobile : public Radio { public: Mobile() { cout<<endl<<"Mobile created"; }
void draw() { cout<<endl<<"Draw Rectangle"; } }; class circle:public shape { public: void draw() { cout<<endl<<"Draw Circle"; }
shape * ptr; int opt; cout<<endl<<"1.Rectangle"; cout<<endl<<"2.Circle"; cout<<endl<<"3.Square"; cout<<endl<<"Enter you choice:"; cin>>opt; switch(opt) { case 1: ptr=new rectangle(); break;
} ptr->draw(); }
Write a function to find max out of three basic type using function template.
T greater(T a,T b,T c) { if (a>b && a>c) { return a; } else if(b>a && b>c) { return b; } else { return c; } }
Write a function to swap two variables of basic type or user defines type using function template.
#include <stdio.h> #include <conio.h> #include <iostream.h> template <class T> T swap(T &a,T &b) { T s; s=a; a=b; b=s; } main() { int a=6,b=66; swap(a,b); cout<<endl<<"a : "<<a; cout<<endl<<"b : "<<b; }