Skip to main content

Posts

Showing posts from April, 2012

Stack operations using Linked List implementation

#include<iostream> using namespace std;  void push();  void pop();  void traverse();  struct node  { int num;    struct node *next;  }*top=NULL;  typedef struct node node;  typedef struct node* nptr;  void main()  { char choice;    while(1)    {    system("cls");    cout<<"-----STACK USING LINKED LIST-----\n\n";    cout<<" 1.PUSH\n";    cout<<" 2.POP\n";    cout<<" 3.TRAVERSE\n";    cout<<" 4.EXIT\n";    cout<<" Enter your choice: ";    cin>>choice;    switch(choice)    { case '1': push();       break;      case '2': pop();       break;      case '3': traverse();       break;      case '4': exit(0);      default: cout<<"\n\nInvalid choice\n\n";    }    system("pause");   }  }   void push()   { nptr p;     int item;     if((p=new node)!=NULL)     { cout<<&q

Create Simple Class

        Define a class to represent a bank account. Include the following members: Data Members: Name of the depositor Account Number Type of account Balance amount in the account. Member functions: To assign initial values To deposit an amount To withdraw an amount after checking the balance. To display name and balance. #include<iostream> #include<string> using namespace std; class bank { string name; int ac_no; string ac_type; int bal; public: bank() { //name[]="Default"; name="Default"; ac_no=0; //ac_type[]="Default"; ac_type="Default"; bal=0; } void get_data() { cout<<"Enter Name: "; cin>>name; cout<<"\nEnter Account No: "; cin>>ac_no; cout<<"\nEnter Account Type: "; cin>>ac_type; cout<<"\nEnter Initial Balance:"; cin>>bal; } void deposit() { int d_bal=0;

Copy the contents of one file to another

WAP to copy the contents of one file to another. Use command line arguments. #include <iostream> #include <fstream>                                                     #include <cstdlib> using namespace std;                                                   void print_error(const char*, const char* = " ");                     void main(int argc, char* argv[])                                       {      if (3 != argc)          print_error("usage: copy source file to destination file");      ifstream in( argv[1], ios::binary );                                  if (!in)           print_error( "can't open", argv[1] );      ofstream out( argv[2], ios::binary );                                  if (!out)           print_error( "can't open", argv[2] );      char ch;                                                              while ( in.get(ch) )                                                       o

Overload output (<<) and input (>>) operators

#include <iostream> using namespace std; class MyClass {   int x, y, z; public:   MyClass(int a, int b, int c) { x = a; y = b; z = c; }   friend ostream &operator << (ostream &stream, MyClass obj);   friend istream &operator >> (istream &stream, MyClass &obj); } ; ostream &operator << (ostream &stream, MyClass obj) { cout<<"\n Insertion ( << ) operator\n";   stream << obj.x << ", ";   stream << obj.y << ", ";   stream << obj.z << "\n";   return stream; // return the stream } istream &operator >> (istream &stream, MyClass &obj) {   cout << "\n\n Extraction ( >> ) operator\n Enter X,Y,Z values: ";   stream >> obj.x >> obj.y >> obj.z;   return stream; } void main() {   MyClass a(1, 2, 3);   cout << a;   cin >> a;   cout << a;   system("

Operator Overloading

    Create a class called Rational for performing arithmetic with rational nos. Use integer variables to represent numerator and denominator. Write a constructer method that enables an object of this class to be initialized when it is declared. Provide a no argument constructor with default values in case no initializes are provided. Provide a copy constructor also. Write methods for a.        Addition, Overload '+' operator b.       Subtraction, Overload '-' operator c.        Multiply, Overload ‘*’ operator d.       Division, Overload '/' operator e.        Increment, Overload ++ operator (both prefix & postfix) f.        Overload operator = =  (to check equality of two rational nos.) as a friend function g.       Overload Assignment operator h.       Printing in the form of a/b #include<iostream> using namespace std; class rational { int x,y; public: rational(int i=1, int j=1) { x=i; y=j; } rational operator + (rational ob)

Show address of each character in string

#include<iostream> #include<string> using namespace std; void main() { string s1,s2; string s3; cout<<"\n\n Enter first string: "; cin>>s1; cout<<"\n\n Enter second string: "; cin>>s2; //  Address of each character in the strings s1, s2; for(unsigned i=0,j=0;i<((s1.length()>s2.length())?s1.length():s2.length());i++,j++) { if(i<s1.length()) cout<<"\n Address of s1("<<s1[i]<<") = "<<(int)&s1[i]; if(i<s2.length()) cout<<"\t Address of s2("<<s2[j]<<") = "<<(int)&s2[j]; } system("pause"); }

To Convert Lowercase Characters to Uppercase

#include<iostream> #include<string> using namespace std; void main() { string s1;         cout<<"\n\n Enter first string: "; cin>>s1;         // Lowercase to Uppercase for(unsigned i=0;i<s1.length();i++) { if(s1[i]>='a' && s1[i]<='z') s1[i]-=32; }         cout<<"\n\n s1: "<<s1<<"\n\n";          // Uppercase to Lowercase for(unsigned i=0;i<s1.length();i++) { if(s1[i]>='A' && s1[i]<='Z') s1[i]+=32; }          cout<<"\n\n s1: "<<s1<<"\n\n"; system("pause"); }

Matrix Operations using Template

Create Matrix class. Write a menu-driven program to perform following Matrix operations (2-D array implementation): a) Sum b) Difference c) Product d) Transpose e) Use Templates #include<iostream> using namespace std; const int r=5,c=5; template<class T> class matrix { T m[r][c]; public: void get_value() { for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { cout<<"\n M["<<i<<"]["<<j<<"] = "; cin>>m[i][j]; } } } void operator +(matrix ob) { T p[r][c]; for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { p[i][j]=m[i][j]+ob.m[i][j]; cout<<" "<<p[i][j]<<" "; } cout<<"\n"; } } void operator -(matrix ob) { T p[r][c]; for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { p[i][j]=m[i][j]-ob.m[i][j]; cout<<" "<<

Calculate GCD / HCF of Two Numbers using Recursion

Write to calculate GCD / HCF of 2 number (i)                  with recursion (ii)                without recursion #include<iostream> using namespace std; int HCF(int x, int y) {     int tmp;     if(x<y)     {         tmp=x;         x=y;         y=tmp;//using tmp to swap x and y         }         while(x%y!=0)     {         tmp=x%y; //using tmp to store remainder         y=x;         x=tmp;         }     return(y); } int HCF_rec(int x, int y){     if(x%y==0)     {         return(y);     }     else     {         HCF(y,x%y);     } } void main() {     int a,b,k; cout<<"\n Enter Two Numbers: "; cin>>a>>b;     k=HCF(a,b);   cout<<"\n\n Without Recursion";     cout<<"\n\n HCF ("<<a<<","<<b<<") = "<<k<<"\n\n"; k=HCF_rec(a,b);   cout<<"\n\n Usi

Swap Two Numbers using Macro

Write a macro that Swaps two numbers. WAP to use it. #include<iostream> #define swap(x,y) { x=x+y; y=x-y; x=x-y; } using namespace std; void main() { int x,y; system("cls"); cout<<"\n Enter any two numbers : "; cin>>x>>y; cout<<"\n\n Before swapping\n x= "<<x<<"\n y= "<<y; swap(x,y); cout<<"\n\n After swapping\n x= "<<x<<"\n y= "<<y<<"\n\n"; system("pause"); }

Insertion sort, bubble sort or Selection sort Using templates

    Use generic class to sort a list of elements. Give user the option to perform sorting using Insertion sort, bubble sort or Selection sort. Use templates. #include<iostream> #include<limits> using namespace std; template <class T> class sorting { T a[10]; public: void get_item() { for(int i=0;i<10;i++) { cout<<"\n a["<<i<<"] = "; cin>>a[i]; } } void sel_sort() { T temp; for(int i=0;i<10;i++) { for(int j=i+1;j<10;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } } void bub_sort() { T temp; for(int i=0;i<10;i++) { for(int j=0;j<10-i-1;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } } void inst_sort() { T tmp; int j; for(int i=1;i<10;i++) { tmp=a[i];

Linear search & Binary search using Template

Write a program to search an element from a list. Give user the option to perform Linear or Binary search. Use Template functions. #include<iostream> using namespace std; template <class T> void Lsearch(T *a, T item, int n) { int z=0; for(int i=0;i<n;i++) { if(a[i]== item) { z=1; cout<<"\n Item found at position = "<<i+1<<"\n\n"; } else if(z!=1) { z=0; } } if(z==0) cout<<"\n Item not found in the list\n\n"; } template <class T> void Bsearch(T *a, T item, int n) { int beg=0,end=n-1; int mid=beg+end/2; while((a[mid]!=item) && (n>0)) { if(item>a[mid]) beg=mid; else end=mid; mid=(beg+end)/2; n--; } if(a[mid]==item) cout<<"\n Item found at position = "<<mid+1<<"\n\n"; else cout<<"\n Item not found in the list\n\n"; } void main() { int iarr[10] = {2,42,56,86,87,99,323,546,767,886};

Occurrences of each letter of alphabet in the text

Program to print a table indicating the no. of occurrences of each letter of alphabet in the text entered as command line arguments. #include<iostream> #include<string> using namespace std; int main(int argc, char *argv[]) { string str=""; static int alphabet[26]; int x; cout<<"\n\n Command-Line Argument\n"; for(int i=0;i<argc;i++) { cout<<"\n "<<argv[i]; str+=argv[i]; } for(int i=0;i<str.length();i++) { if(str[i]>='A' && str[i]<='Z') { x=((int)str[i])-65; alphabet[x]++; } else if(str[i]>='a' && str[i]<='z') { x=((int)str[i])-97; alphabet[x]++; } } //Displaying No. of occurrences of each alphabets in the command line argument cout<<"\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n Alphabet No. of Occurrences\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; for(int i=0;i<26;i++)