Tuesday, October 4, 2016

LOWER TRIANGLE MATRIX PATTERN :)

Program to Display Lower Triangular Green Matrix Pattern with Random Numbers.

#include <iostream>
#include<stdlib.h>
using namespace std;
int main( )
{
    system("COLOR A");   //Function to make Textcolor GREEN
start:
    int i,j;
    short int x;
    x=rand();
    for(j=1; j<=i; j++)
    {
        for(i=1; i<=10; i++)
        {
            if(i<=j)
                cout<<x;
            continue;
        }
        cout<<"\n";
    }
    cout<<"\n";
    goto start;
}


Constructors And Destructors In Multilevel Inheritance:)

If we create Constructors in All Levels in Multilevel Inheritance then the order of execution of Constructors is Same As the level
That is First The Base then Base 1 and Then Derived Class Constructor is Called

Destructors Follows the reverse order of inheritace


Program to Illustrate The Calling Of Constructors And Destructors During Multilevel Inheritance:


#include<iostream>
using namespace std;
class base
{
    int a;
    public:
        base(int b)
        {
            a=b;
            cout<<"\nBase Constructor Called";
            cout<<"\nA= "<<a;
        }
        ~base()
        {
            cout<<"\nBase Destructor Called";
        }
};
class base1:public base
{
    int b;
    public:
        base1(int a,int b):base(b) // the base1 is derived from base
        {
            b=a;
            cout<<"\nBase 1 Constructor Called";
            cout<<"\nB= "<<b;
        }
        ~base1()
        {
            cout<<"\nBase1 Destructor Called";
        }
};
class derived:public base1
{
    int x;
    public:
    derived(int y,int a,int b):base1(a,y)//one parameter for base class and other for base 1
    {
        x=y;
        cout<<"\nDerived Construtor Called";
        cout<<"\nX= "<<x;    }
~derived()
{
    cout<<"\nDerived Destructor Called";
}
};
main()
{
    derived d(10,20,21);
}

Output:















Else in all other inheritances The Base Class Constructor called First Then The Derived Class Constructor.

EMPLOYEE DATABASE~(Solution to E Balagurusamy Problem 8.3) :-

#include <iostream>
#include <stdlib.h>
using namespace std;
class staff
{
protected:
    char name[20],id[5];
    int age;
};

class teacher:public staff
{
protected:
    char subject[10];
public:
    void getinfo()
    {
        cout<<"\nEnter Name: ";
        cin>>name;
        cout<<"Enter Age: ";
        cin>>age;
        cout<<"Enter Employee ID: ";
        cin>>id;
        cout<<"Enter Subject of Publication: ";
        cin>>subject;
    }
    void putinfo()
    {
        cout<<"Name: "<<name;
        cout<<"\nAge: "<<age;
        cout<<"\nEmployee ID: "<<id;
        cout<<"\nSubject: "<<subject;
    }
};

class typist:public staff
{
protected:
    int speed;;
public:
    void typ()
    {
        cout<<"Enter Typing Speed(Out of 5): ";
        cin>>speed;
    }
};

class regular:public typist
{
protected:
    int salary;
public:
    void inp()
    {
        typ();
        cout<<"Enter Monthly Salary: ";
        cin>>salary;
    }
    void outp()
    {
        cout<<"\nTyping Speed:" <<speed;
        cout<<"\nAnnual Salary: "<<salary*12;
    }
};

class casual:public typist
{
protected:
    int days,sal;
public:
    void getdata()
    {
        typ();
        cout<<"Enter Number of Working Days: ";
        cin>>days;
        cout<<"\nEnter Salary per Day: ";
        cin>>sal;
    }
    void showdata()
    {
        cout<<"\nTyping Speed:" <<speed;
        cout<<"\nTotal Salary:"<<sal*days;
    }
};

class officer:public staff
{
protected:
    char grade;
public:
    void getd()
    {
        cout<<"Enter Grade: ";
        cin>>grade;
    }
    void display()
    {
        cout<<"\nGrade: "<<grade;
    }
};

int main()
{
    system("COLOR D");
    char ch;
    cout<<"\t******EMPLOYEE DATABASE******\n";
    teacher t;
    t.getinfo();
    cout<<"Enter Typist Choice:(r/c)\n";
    cout<<"R:Regular Typist  C:Casual Typist\n";
    cin>>ch;
    if(ch=='r'||ch=='R')
    {
        regular s;
        s.inp();
    }
    else
    {
        casual s;
        s.getdata();
    }
    officer x;
    x.getd();
    cout<<"\n\n\t****Employee Information****\n";
    t.putinfo();
    if(ch=='r'||ch=='R')
    {
        regular s;
        s.outp();
    }
    else
    {
        casual s;
        s.showdata();
    }
    x.display();
    return 0;
}


Monday, October 3, 2016

STATIC AND NON-STATIC VARIABLES:

Program to Show Difference between static and non-static member variables.

#include <iostream>
using namespace std;
class sample
{
    static int c; //static variable
    int x; //non-static variable

public:
    void reset()
    {
        x=0;
    }

    void count()
    {
        ++c;
        ++x;
        cout<<"\n\nStatic Variable:\n";
        cout<<"Value of c="<<c;
        cout<<"\nAddress of c="<<(unsigned)&c; //address of c
        cout<<"\n\nNon-Static Variable:\n";
        cout<<"Value of x="<<x;
        cout<<"\nAddress of x="<<(unsigned)&x; //address of x
    }
};
int sample::c=0; //initialising static variable

int main()
{
    sample s1,s2,s3;
    s1.reset();
    s2.reset();
    s3.reset();
    s1.count();
    s2.count();
    s3.count();
    return 0;
}



Bank Systems :) (Solution To E Balagurusamy Problem 8.1:)

#include<iostream>
#include<math.h>
using namespace std;
class account
{
    char name[20];
    long ac_no;
    protected:
        int bal;
        public:
            char type[20];
            void getdetails()
            {
                cout<<"Name of customer";
                cin>>name;
                cout<<"Account Number";
                cin>>ac_no;
                cout<<"Balance";
                cin>>bal;
            }
            void putdetails()
            {
            cout<<"Name: "<<name<<"\nAccount No: "<<ac_no<<"\nBalance: "<<bal;
            }
            void deposit()
            {
                int x;
                cout<<"Enter Amount To Deposit ";
                cin>>x;
                bal=bal+x;
            }
            void withdrawl(){
                int x;
                cout<<"Enter Amount To Withdraw  ";
                cin>>x;
                if(bal>=x)
                {
                bal=bal-x;
            }
            else
            {
                cout<<"\nBalance is Not Sufficient";
            }
        }
};
class savings:public account
{
    int interest;
    public:
        void interestt(float r=0.075,int t=1)
        {
            bal=bal*pow((1+r),t);
        }
};
class current:public account
{

    public:
        void min()
        {
            if(bal<1000)
            {
                bal=bal-100;
            }
        }
        void cheque()
        {
            int x;
            cout<<"Enter The Money To Be Debited ";
            cin>>x;
            bal=bal-x;
        }
};
main()
{ savings s;
     current c;
     int ch;
     cout<<"Enter account type";
     cout<<"\nPress 1 for Saving";
     cout<<"\nPress 2 for Current\n";
     cin>>ch;
     if(ch==1)
     {s.getdetails();}
     else
     {c.getdetails();}
     cout<<"\n\n\nWHAT DO U WANT TO DO\n\n\n";
     cout<<"************************************************************";
     int c1=1;
     while(c1!=7)
        {
         cout<<"\n1. TO WITHDRAWL  ";
         cout<<"\n2. TO DEPOSIT  ";
         cout<<"\n3. TO DISPLAY  ";
         cout<<"\n4. Cheque Withdrawl  ";
         cout<<"\n5. Interest ";
         cout<<"\n6. Minimum Balance ";
         cout<<"\n7. To Exit\n ";
         cin>>c1;
         switch(c1)
            {
             case 1:
                if(ch==1)
                s.withdrawl();
                else
                c.withdrawl();
                break;
             case 2:
                if(ch==1)
                s.deposit();
                else
                c.deposit();
                break;
             case 3:
                if(ch==1)
                s.putdetails();
                else
                c.putdetails();
                break;

             case 4:
                if(ch==1)
                cout<<"NO Chequebook Facility";
                else
                c.cheque();
                break;
             case 5:
                 if(ch==1)
                s.interestt();
                else
                cout<<"Curret Accounts Get No Compound Interest";
                break;
             case 6:
                 if(ch==1)
                cout<<"No Penality";
                else
                c.min();
                break;
            case 7:
                break;
             }
         }
    cout<<"\n\n\nTHANKS FOR USING OUR SERVICE";
}

Output:

Saturday, October 1, 2016

Operations On Sorted Linklist :)

//Program To Perform Operations On A Sorted Linklist

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>  //for malloc function
struct node
{
    int data;
    struct node *next;

}*head;
void insert(int data) //for insertion
{ struct node *temp=(struct node *)malloc(sizeof(struct node));
struct node *save=(struct node *)malloc(sizeof(struct node));
struct node *ptr=(struct node *)malloc(sizeof(struct node));
temp->data=data;
if(head==NULL) //if it is first element
{
    head=temp;
    temp->next=NULL;
}
if(temp->data<head->data)//if value less than first element
{
    temp->next=head;
    head=temp;
 }
else{

ptr=head;
while(ptr!=NULL)
{
    if(temp->data<ptr->data)
    {
        break;
    }
    else
    {   save=ptr;
        ptr=ptr->next;
    }
}
save->next=temp;
temp->next=ptr;
}
}
void print() //for display
{
struct node *temp=head;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
printf("\n");
}
}
 void delete(int data)
 {
  struct node *ptr=(struct node*)malloc(sizeof(struct node));
struct node *save=(struct node*)malloc(sizeof(struct node));
save=head;
ptr=head->next;
if(save->data==data)
{
 head=head->next;
 return;
 } 
 else{
  while(ptr!=NULL)
  {
   if(ptr->data==data)
   {
    save->next=ptr->next;
    return;
   }
   else
   {
    save=ptr;
    ptr=ptr->next;
   }}
   printf("ELEMENT NOT FOUND");
  
 }
 }
main()
{
int choice,num;
    head=NULL; //initially head is null
    while(1)  //for user interface
    { printf("\nOperations On Linklist\n");
         printf("Enter your choice\n");
              printf("1. INSERT \n");
              printf("2. DELETE \n");
              printf("3. DISPLAY \n");
              printf("4. EXIT \n");
              scanf("%d",&choice);
              switch(choice)
              {
                   case 1: //If user wishes to insert element in the queue
                   printf("Enter number to be inserted in the queue: ");
                   scanf("%d",&num); //getting the element to be inserted from the user
                   insert(num); //calling insert function
                   break; //jumping out of switch statement
                   case 2:
                   printf("Enter Element To Delete");
                   scanf("%d",&num);//If user wishes to delete element from the queue
                        delete(num); //calling delete function and saving the result in an int variable(num)
                        break;
                        case 3://If user wishes to display the queue
                             print(); //calling display function
                             break;
                             case 4: //If user wishes to exit(stop continuing the program)
                                  exit(1); //end of while loop
                                  default ://If user enters a number other than 1 to 4
                                          printf("INVALID CHOICE \n");
                                          }
                                          }
                                          }

Output:


Postfix Expression Evaluation For Multidigit :)

//Program to Evaluate Postfix Expression

#include<iostream>
#include<ctype.h>  //for isdigit function
using namespace std;
int stack[100];
int top=-1;
int value=0;    //Global Declarations
int pop()
{

    return (stack[top--]);
}
void push(int x)
{
    if(value==1)
    {
        int y;
        y=pop();
        stack[++top]=x+10*y;   //for more than one digit
    }
    else if(value==0)
    {
    stack[++top]=x;
    value=1;
}
}
main()
{
char c,postfix[100];
int i=0,y,x;
cout<<"\nEnter The Postfix Expression:";
cin.getline(postfix,100);
while((c=postfix[i++])!='\0')
{
    if(isdigit(c))
    push(c-'0');  //for converting datatype
    else if(c==',')
    {
        value=0;
    }
    else
    {
    x=pop();  //top element
    y=pop();  //next top element
    switch(c)
    {
        case'+':
            push(x+y);
         break;
         case '-':
             push(y-x);
             break;
        case '*':
        push(y*x);
        break;
        case '/':
        push(y/x);
        break;
        default:
        cout<<"\nInvalid Operator";    
    }
    }
    }
    cout<<"\n\nEntered Expression:"<<postfix;
cout<<"\n\nResult Of Evaluation:"<<stack[top];
}

Output: