Find us On Facebook

Thursday, April 7, 2011

write a class to represent a vector (a series of float values). Include member functions to perform the following tasks:

1)To Create the Vector
2)To modify the value of a given element
3)To multiply by a scalar value.
4)To display the vector in the form (10,20,30....)
Write a program to test your class*/

#include <iostream.h>
#include <conio.h>

int const size=50;

class vector
{
float d[size];
int s;
public:
void create(void);
void modify(void);
void multiply(void);
void display(void);
};

void vector :: create(void)
{
cout<<"\n\nEnter of Array you want to create:-";
cin>>s;
cout<<"Enter "<<s<<" Real Numbers\n";
for(int i=0;i<s;i++)
       cin>>d[i];
}

void vector :: modify(void)
{
int mfy_value;
float with;
cout<<"\nEnter Value is to be modified:-";
cin>>mfy_value;
cout<<"Enter Value with which you want to Replace:-";
cin>>with;
int search=0;
for(int i=0;search<s;i++)
{
    if(mfy_value==d[i])
       {
      d[i]=with;
      }
   search=i+1;
}
}

void vector :: multiply(void)
{
int mul;
cout<<"\nEnter value with which you want to multiply:-";
cin>>mul;
for(int i=0;i<s;i++)
    d[i]=d[i]*mul;
}

void vector :: display(void)
{
cout<<"\n\nDisplay of Array\n";
cout<<"(";
for(int i=0;i<s;i++)
{
    cout<<d[i];
   if(i!=s-1)
       cout<<",";
}
cout<<")";
}

void main()
{
clrscr();
vector o1;
int choice;
do
{
cout<<"\n\nChoice List\n";
cout<<"1)    To Create Vector Array\n";
cout<<"2)    To Modify Array\n";
cout<<"3)    To Multiply with Scalar value\n";
cout<<"4)    To Display\n";
cout<<"5)    EXIT\n";
cout<<"Enter your choice:-";
cin>>choice;
switch(choice)
{
case 1:    o1.create();
    break;
case 2:    o1.modify();
    break;
case 3: o1.multiply();
    break;
case 4: o1.display();
    break;
case 5:goto end;
}
}while(1);
end:
}

Unary Operator minus

#include <iostream.h>
#include <conio.h>

class minus
{
  int d1,d2,d3;
  public:
  minus()
  {d1=d2=d3=0;}
  minus(int a,int b,int c)
  {
   d1=a;
   d2=b;
   d3=c;
  }
  void display(void)
  {
    cout<<d1<<"  "<<d2<<"  "<<d3;
  }
  void operator - ()
  {
    d1=-d1;
    d2=-d2;
    d3=-d3;
  }
};

void main()
{
  clrscr();
  minus o1,o2(2,4,6);

  -o1;
  cout<<"\nData of Object 1 : ";
  o1.display();

  -o2;
  cout<<"\n\nData of Object 2 : ";
  o2.display();

getch();
}

Triangle

#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
void main()
{
int i,j,num,c;
clrscr();
cout<<"Enter number:-";
cin>>num;
cout<<setw(num+2)<<"*\n";
for(c=2,i=num;i>0;i--)
{
    cout<<setw(i)<<"*";

    for(j=1;j<=1;j++)
   {
    cout<<setw(c)<<"*";
   }
 c=c+2;
cout<<"\n";
}
getch();
}

DYNAMIC MEMORY ALLOCATION

#include <iostream.h>
#include <conio.h>

class matrix
{
  int ***p,d1,d2,d3;
  public:
  matrix(){}
  matrix(int a,int b,int c);
  void getdata();
  void display();
};

matrix :: matrix(int a,int b,int c)
{
  d1=a;
  d2=b;
  d3=c;
  p=new int **[d1];
  for(int i=0;i<d1;i++)
  {
    p[i]=new int *[d2];
    for(int j=0;j<d2;j++)
    p[i][j]=new int [d3];
  }
}

void matrix :: getdata(void)
{
  cout<<"\n\nEnter data "<<d1<<"*"<<d2<<"*"<<d3<<" data\n";
  for(int i=0;i<d1;i++)
  {
    for(int j=0;j<d2;j++)
    {
     for(int k=0;k<d3;k++)
    cin>>p[i][j][k];
     }
  }
}

void matrix :: display(void)
{
   cout<<"\n\n\nDisplay function\n";
   for(int i=0;i<d1;i++)
  {
    for(int j=0;j<d2;j++)
    {
     for(int k=0;k<d3;k++)
    cout<<p[i][j][k];
     }
     cout<<endl;
  }
}

void main()
{
clrscr();
matrix o1;
o1.getdata();
o1.display();
getch();
}

Class Template for vector operations.

#include <iostream.h>
#include <conio.h>

template <class T>
class vector
{
    T *arr;
    int size;

    public:

    vector() {
       arr=NULL;
       size=0;
    }

    vector(int m);
    vector(T *a,int n);
    void modify(T value,int index);
    void multiply(int scalarvalue);
    void display();
};

template <class T>
vector<T> :: vector(int m)
{
   size=m;
   arr = new T[size];
   for(int i=0;i<size;i++)
       arr[i]=0;
}

template <class T>
vector<T> :: vector(T *a,int n)
{
   size=n;
   arr = new T[size];
   for(int i=0;i<size;i++)
       arr[i]=a[i];
}


template <class T>
void vector<T> :: modify(T value,int index)
{
    arr[index]=value;
}


template <class T>
void vector<T> :: multiply(int scalarvalue)
{
    for(int i=0;i<size;i++)
       arr[i] = arr[i] * scalarvalue;
}

template <class T>
void vector<T> :: display()
{
    cout<<"(";
    for(int i=0;i<size;i++)
    {
        cout<<arr[i];
        if(i!=size-1)
           cout<<", ";
    }
    cout<<")";
}


void main()
{
    clrscr();


    //Creating Integer Vector.
    int iarr[]={1,2,3,4,5};
    vector <int> v1(iarr,5); //Integer array with 5 elements.
    cout<<"\nInteger Vector : ";
    v1.display();
    cout<<"\n\nModify index 3 with value 15\n";
    v1.modify(15,3); //modifying index 3 with value 15.
    cout<<"After Modification : ";
    v1.display();
    cout<<"\n\nMultiply with scalar value : 10\n";
    v1.multiply(10); //Multiply with scalar value 10.
    cout<<"After Multiplying : ";
    v1.display();
    cout<<"\n\n";


    //Creating double Vector.
    double darr[]={1.1,2.2,3.3,4.4,5.5};
    vector <double> v2(darr,5); //Double array with 5 elements.
    cout<<"\nDouble Vector : ";
    v2.display();
    cout<<"\n\nModify index 0 with value 9.9 \n";
    v2.modify(9.9,0); //modifying index 0 with value 9.9.
    cout<<"After Modification : ";
    v2.display();
    cout<<"\n\nMultiply with scalar value : 10\n";
    v2.multiply(10); //Multiply with scalar value 10.
    cout<<"After Multiplying : ";
    v2.display();
    cout<<"\n\n";

    getch();

}

Template for finding Minimum value in an array

#include <iostream.h>
#include <conio.h>

template <class T>
T findMin(T arr[],int n)
{
    int i;
    T min;
    min=arr[0];
    for(i=0;i<n;i++)
    {
         if(min > arr[i])
        min=arr[i];
    }
    return(min);
}


void main()
{
    clrscr();
    int iarr[]={5,4,3,2,1};
    char carr[]={'z','y','c','b','a'};
    double darr[]={3.3,5.5,2.2,1.1,4.4};

    //calling Generic function...to find minimum value.
    cout<<"Generic Function to find Minimum from Array\n\n";
    cout<<"Integer Minimum is : "<<findMin(iarr,5)<<"\n";
    cout<<"Character Minimum is : "<<findMin(carr,5)<<"\n";
    cout<<"Double Minimum is : "<<findMin(darr,5)<<"\n";

    getch();
}

sum of 10 integers

#include <iostream.h>
#include <conio.h>

class addition
{
int data;
public:
addition()     //constructor
{data=0;}

void getdata(addition o1)
{cout<<"\nEnter Your Data :-";
    int sum;
    cin>>sum;
   data=data+sum;
}

void display(void)
{    cout<<"\nSum of Data is :-"<<data;}
};


void main()
{

String concatenation using + operator //String Comparision using <= operator

#include <iostream.h>
#include <string.h>
#include <conio.h>

class string
{
  char *p;
  int len;
  public:
   string()
   {}

   string(char *a)
   {
    len=strlen(a);
    p=new char[len+1];
    strcpy(p,a);
   }

   string operator + (string o2)
   {
      string temp;
      temp.len=len+o2.len;
      temp.p = new char[temp.len + 1];
     strcpy(temp.p,p);
     strcat(temp.p,o2.p);
     return temp;
   }

   int operator <= (string o2)
    {
       if(len <= o2.len)
          return(1);
       else
          return(0);
    }

   void display(void)
   {
     cout<<p;
   }
};


void main()
{
  clrscr();
  string o1("Vivek"),o2("Patel"),o3;

  o1.display();
  cout<<endl;
  o2.display();
  cout<<endl<<endl;

  o3=o1+o2;

  cout<<"After String Concatenation : ";
  o3.display();
  cout<<endl<<endl<<endl;

  string s1("New Delhi"),s2("New York");
  if(s1<=s2)
  {
    s1.display();
    cout<<" is smaller than ";
    s2.display();
  }
  else
  {
    s2.display();
    cout<<" is smaller than ";
    s1.display();
  }

  getch();
}

Three steps for Polar cordinates

//1)convert points to rectangular cordinates
//2)Addition of Points
//3)Converting Back to Polar cordinates

#include <iostream.h>
#include <math.h>
#include <conio.h>

class polar
{
  double radius;
  double angle;

  double getx()
    {return radius*cos(angle);} //These two function
  double gety()                 //convert this polar objects
    {return radius*sin(angle);} //into x and y rectangular coords

 public:
   polar()
   {radius=0.0;angle=0.0;}

   polar(float r,float a)
    {
      radius=r;
      angle=a;
    }

   void display()
    {
      cout<<"("<<radius<<", "<<angle<<")";
    }

  polar operator + (polar o2)
  {
    double x=getx()+o2.getx();
    double y=gety()+o2.gety();
    double r=sqrt(x*x + y*y);   //converts x and y to
    double a=atan(y/x);         //Polar co-ordinate.
    return polar(r,a);
  }
};

void main()
{
 clrscr();
 polar o1(10,2),o2(10,5),o3;

 o3=o1+o2;

 cout<<"\no1 =";
 o1.display();
 cout<<"\no2 =";
 o2.display();
 cout<<"\no3 =";
 o3.display();

 getch();
}

Operator

#include <iostream.h>
#include <conio.h>

class distance
{
   int feet;
   float inches;
   public:

    distance()          //constructor1
    {feet=0;inches=0;}
    distance(int ft,float inch)       //constructor2
    {feet=ft;inches=inch;}

    void getdata()
    {   cout<<"Enter Feet and inches respectively: ";
    cin>>feet>>inches;
    }

    void display()
    { cout<<"Feet : "<<feet<<endl<<"Inches :"<<inches;}

To Enter Few Records and to sort according to the choice of the data member. (USING NESTED STRUCTURE)

#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

//GLOBAL DECLARATION OF STRUCTURE
struct date
{
int year,month,day;
};

struct person
{
int recno;
char name[20];
date dob;          //using date structure as datatype
int salary;
};

void line(void);    //Global Declaration of function as it is called in another function other than main.


void main()
{
void starting(void);
void sorting(struct person[],int);
void display(struct person[]);
void end(void);
person rec[5];            //Declaration of person datatype

starting();

textcolor(111);
textbackground(196);
clrscr();
 cout<<"\n\n"<<setw(48)<<"RECORD INFORMATION";
//for inserting records
cout<<"\n\n\n\n\tEnter 5 Records\n\n\n\n";
for(int i=0,c=1;i<5;i++)
{

    cout<<"Information for Record No. "<<c<<endl;
       c++;
    line();
    cout<<"\n\nEnter Record Number:-";
   cin>>rec[i].recno;
    cout<<"Enter Name :-";
   cin>>rec[i].name;
   cout<<"\nFOR Birth_Date\n";
   cout<<"--------------\n";
   cout<<"Enter Year:-";
   cin>>rec[i].dob.year;
   cout<<"Enter Month:-";
   cin>>rec[i].dob.month;
   cout<<"Enter Day:-";
   cin>>rec[i].dob.day;
   cout<<"\nEnter salary:-";
   cin>>rec[i].salary;
   line();
   cout<<endl<<endl;
}

clrscr();
display(rec);
clrscr();
cout<<endl<<endl;
int choice;
do
{
cout<<endl<<endl;
cout<<"\n\nWhat Operation Would you Like to Perform\n";
cout<<"1)    sort by Record Number\n";
cout<<"2)    sort by Name\n";
cout<<"3)    sort by Date\n";
cout<<"4)    sort by Salary\n";
cout<<"5)    DISPLAYING\n";
cout<<"6)    QUIT\n";
cout<<"Enter Your Choice:-";
cin>>choice;
    switch(choice)
    {
    case     1:
    case     2:
    case     3:
   case    4:sorting(rec,choice);
                 break;
    case     5:display(rec);
               break;
   case    6:goto out;
    default:cout<<"Entered Choice is Invalid, Try Again";
    }
   getch();
   clrscr();
}while(1);
out:
}


//sorting  function
void sorting(struct person rec[],int choice)
{
 struct person tempq;

         //record Number wise Sorting
      if(choice==1)
      {
          for(int i=0;i<5;i++)
         {
             for(int j=i+1;j<5;j++)
            {
                if(rec[i].recno > rec[j].recno)
               {
                tempq        =    rec[i];
               rec[i]    =    rec[j];
               rec[j]    =    tempq;
               }
            }
         }
      }


   //namewise sorting

    if(choice==2)
   {
       for(int i=0;i<5;i++)
      {
          for(int j=i+1;j<5;j++)
         {
             if(strcmp(rec[i].name,rec[j].name)>0)
                {
               tempq                  =  rec[i];
               rec[i]                =  rec[j];
               rec[j]              =  tempq;
               }
         }
      }
    }

    //datewise
   if(choice==3)
   {
    for(int i=0;i<5;i++)
   {
       for(int j=i+1;j<5;j++)
      {
            if(rec[i].dob.year > rec[j].dob.year)
             {
                tempq                  =  rec[i];
               rec[i]                =  rec[j];
               rec[j]              =  tempq;

               if(rec[i].dob.month > rec[j].dob.month)
                   {
                  tempq                =   rec[i];
                  rec[i]            =     rec[j];
                  rec[j]            =   tempq;

                      if(rec[i].dob.day > rec[j].dob.day)
                      {
                      tempq                    =    rec[i];
                      rec[i]                =  rec[j];
                      rec[j]                =  tempq;
                      }

                  }
               }
         }
      }
     }


     //for salary wise

     if(choice==4)
     {
        for(int i=0;i<5;i++)
       {
           for(int j=i+1;j<5;j++)
          {
          if(rec[i].salary > rec[j].salary)
         {
                  tempq            =   rec[i];
                 rec[i]           =     rec[j];
                rec[j]        =   tempq;
         }
      }
   }
  }

}



//display function
void display(struct person rec[])
{
textcolor(106);
textbackground(104);
clrscr();
cout<<endl<<endl<<endl;
line();
cout<<setw(10)<<"Record No."<<setw(25)<<"NAME OF PERON"<<setw(20)<<"DATE OF BIRTH"<<setw(23)<<"SALARY AT PRESENT"<<endl;
line();
for(int i=0;i<5;i++)
{
cout<<endl;
cout<<setw(6)<<rec[i].recno;
cout<<setw(27)<<rec[i].name;
cout<<setw(15)<<rec[i].dob.day<<"\\"<<rec[i].dob.month<<"\\"<<rec[i].dob.year;
cout<<setw(15)<<rec[i].salary;
cout<<endl;
}
getch();
}


//Line function
void line(void)
{
    for(int i=0;i<40;i++)
    {
        cout<<"--";
    }
   cout<<endl;
}


//Starting function
void starting(void)
{
 //START OF programer details\\
 textcolor(105);
 textbackground(104);
clrscr();
cout<<endl;
cout<<"|";
for(int i=0;i<39;i++)
{
        cout<<"--";
}
cout<<"|";

cout<<" ";
for(int i=0;i<34;i++)
    cout<<"=";

cout<<"R GABA";

for(int i=0;i<33;i++)
    cout<<"=";

cout<<endl;

cout<<"|";
for(int i=0;i<39;i++)
{
        cout<<"--";
}
cout<<"|";
cout<<endl<<endl<<endl;
line();
cout<<setw(49)<<"WELLCOME VIEWERS\n";
line();
cout<<setw(55)<<"SAMPLE DATABASE APPLICATION\n\n";
line();
line();
cout<<setw(79)<<"Created By R GABA\n\n";
line();
line();
cout<<setw(55)<<"EMAIL US ";
getch();
}

MAKING FILE PROGRAM WITH USING ALL MODES

#include <iostream.h>
#include <fstream.h>
#include <conio.h>

static int totrec=0;    //totrec variable keep track for total variable entered
            //Initially Record scanned are Zero

void main()
{
int choice;
while(1)
{
clrscr();
cout<<"Choose your choice\nNOTE : one choice for one record(except viewing data)\n";
cout<<"1) Scanning intial records\n";
cout<<"2) Appending records\n";
cout<<"3) Modifying or append records\n";
cout<<"4) Viewing records\n";
cout<<"5) Exit\n";
cout<<"Enter your choice : ";
cin>>choice;
switch (choice)
{
  case 1 :   {
         ofstream outfile;
         outfile.open("emp",ios::out);
         cout<<"\n\nPlease enter the details as per demanded\n";
         cout<<"\nEnter the name : ";
         char name[20];
         cin>>name;
         outfile<<name<<endl;
         cout<<"Enter Age : ";
         int age;
         cin>>age;
         outfile<<age<<endl;
         cout<<"Enter programming language known by him\her : ";
         char lang[25];
         cin>>lang;
         outfile<<lang<<endl;
         totrec= totrec + 1;
         outfile.close();
         }
         break;
  case 2 :   {
         ofstream outfile;
         outfile.open("emp",ios::app);
         cout<<"\n\nPlease enter the details as per demanded\n";
         cout<<"\nEnter the name : ";
         char name[20];
         cin>>name;
         outfile<<name<<endl;
         cout<<"Enter Age : ";
         int age;
         cin>>age;
         outfile<<age<<endl;
         cout<<"Enter programming language known by him : ";
         char lang[25];
         cin>>lang;
         outfile<<lang<<endl;
         totrec = totrec + 1;
         outfile.close();
         }
         break;
  case 3 :   {
         ofstream outfile;
         outfile.open("emp",ios::ate);
         cout<<"Are you interested in adding record\nenter y or n\n";
         char ans;
         cin>>ans;
         if(ans=='y' || ans=='Y')
         {
         cout<<"\n\nPlease enter the details as per demanded\n";
         cout<<"\nEnter the name : ";
         char name[20];
         cin>>name;
         outfile<<name<<endl;
         cout<<"Enter Age : ";
         int age;
         cin>>age;
         outfile<<age<<endl;
         cout<<"Enter programming language known by him : ";
         char lang[25];
         cin>>lang;
         outfile<<lang<<endl;
         totrec = totrec + 1;
         }
         outfile.close();
         }
         break;
  case 4 :   {
         ifstream infile;
         infile.open("emp",ios::in);
         const int size=80;
         char line[size];
         int counter=totrec;
         while(counter > 0)
         {
         infile.getline(line,size);
         cout<<"\n\nNAME     : "<<line<<endl;
         infile.getline(line,size);
         cout<<"AGE      : "<<line<<endl;
         infile.getline(line,size);
         cout<<"LANGUAGE : "<<line<<endl;
         counter--;
         }
         infile.close();
         }
         getch();
         break;
  case 5  :  goto out;
  default :  cout<<"\nInvalid Choice\nTRY AGAIN\n";
  }
}
out:
}

MATRIX ADDITION Pointers implementation through pointers

#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

class matrix
{
int **p,row,col;
public:
void getdata(void);
friend void matrixadd(matrix &,matrix &);
void display(void);
};

void matrix :: getdata(void)
{
clrscr();
cout<<"Enter Size of Row:-";
cin>>row;
p=new int *[row];
cout<<"Enter size of Coulumn:-";
cin>>col;
cout<<"\nEnter Data for Matrix of size "<<row<<"*"<<col<<endl;
 for(int i=0;i<row;i++)
 {
   p[i]=new int [col];
 }
//scaning value
for(int a=0;a<row;a++)
{
  for(int b=0;b<col;b++)
  {
     cin>>p[a][b];
  }
}
}


void matrix :: display(void)
{
cout<<"\n\n\n\n";
cout<<"Display Function\n\n";
for(int i=0;i<row;i++)
{
   for(int j=0;j<col;j++)
   {
     cout<<setw(5)<<p[i][j];
   }
cout<<endl;
}
}

void matrixadd(matrix &a,matrix &b)
{
int result[10][10];
if(a.row==b.row && a.col==b.col)
{
  for(int i=0;i<a.row;i++)
  {
    for(int j=0;j<a.col;j++)
    {
         result[i][j]=a.p[i][j]+b.p[i][j];
    }
  }
//displaying
  for(int x=0;x<a.row;x++)
  {
    for(int y=0;y<a.col;y++)
    {
      cout<<setw(5)<<result[x][y];
    }
    cout<<endl;
  }
}
else
  cout<<"Invalid Matrix Addition Occurs as size differs\n";
}

void main()
{
matrix o1,o2;
o1.getdata();
o2.getdata();
clrscr();
o1.display();
o2.display();
getch();
clrscr();
cout<<"\n\nAfter Adition Has been Performed\n\n";
matrixadd(o1,o2);
getch();
}

Matrix Addition by Overloading + operator

#include <iostream.h>
#include <conio.h>

class matadd
{
  int **a;
  int row,col;
  public:
   matadd(){}
   matadd(int r,int c);
   void getdata(void);
   void display(void);
   matadd operator + (matadd o2);
};

matadd :: matadd(int r,int c)
{
  row=r;
  col=c;
  a=new int *[row];
   for(int i=0;i<row;i++)
     a[i]=new int[col];
}

void matadd :: getdata(void)
{
   cout<<"\n\nEnter Data for "<<row<<" rows and "<<col
       <<" cols\n\n";
   for(int i=0;i<row;i++)
   {
     for(int j=0;j<col;j++)
        cin>>a[i][j];
   }
}

void matadd :: display(void)
{
  cout<<"\n\n\nMatrix Display\n\n";
  for(int i=0;i<row;i++)
  {
    for(int j=0;j<col;j++)
      cout<<a[i][j]<<" ";
    cout<<endl;
  }
}

matadd matadd :: operator + (matadd o2)
{
   matadd temp(row,col);
    for(int i=0;i<row;i++)
     {
     for(int j=0;j<col;j++)
      {
       temp.a[i][j]=a[i][j] + o2.a[i][j];
      }
     }
return temp;
}

void main()
{
  clrscr();
  matadd o1(3,2),o2(3,2),o3;

  o1.getdata();
  o2.getdata();

  o3=o1+o2;

  o3.display();

  getch();
}

Logic

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,num,space,k;
clrscr();
printf("Enter the number:-");
scanf("%d",&num);
k=num;
for(i=num;i>=0;i--)
{

    for(space=k;space>0;space--)
   {
       printf(" ");
   }

   for(j=num -(i-1);j>=1;j--)
   {
  


       printf("%d ",i);
   }
printf("\n");
k--;
}

getch();
}

Library Database 2

#include <iostream.h>
#include <string.h>
#include <iomanip.h>
#include <conio.h>

class books
{
char **author,**title,**publisher;
int *price;
static int count;
public:

 books(void);   //constructor
 void getdata(void);
 void display_stock(void);
 void search(void);
};

int books :: count=0;

books :: books(void)
{
  author=new char*[15];
  title=new char*[15];
  publisher=new char*[15];
  price=new int[15];
     for(int i=0;i<15;i++)
       {
         author[i]=new char;
         title[i]=new char;
         publisher[i]=new char;
       }
 }

void books :: getdata(void)
{
  cout<<"\n\n\nEnter Book name:-";
  cin>>title[count];
  cout<<"Enter Author name:-";
  cin>>author[count];
  cout<<"Enter Publisher of book:-";
  cin>>publisher[count];
  cout<<"Enter Price of book:-";
  cin>>price[count];
  count++;
}

void books :: display_stock(void)
{
clrscr();
cout<<"\n\n\n";
cout<<setw(15)<<"Book Name"<<setw(20)<<"Author Name"<<setw(20)<<"Publisher"<<setw(10)<<"Price"<<endl<<endl;
for(int i=0;i<count;i++)
{
cout<<setw(15)<<title[i]<<setw(20)<<author[i]<<setw(20)<<publisher[i]<<setw(10)<<price[i]<<endl;
}
cout<<"\n\nTotal Number of Books are "<<count;
}

void books :: search(void)
{
clrscr();
 char  name[20],writer[20];
 cout<<"\n\n\nEnter Book name:-";
 cin>>name;
 cout<<"Enter Author of Book:-";
 cin>>writer;
 for(int i=0;i<count;i++)
 {
   if((strcmp(title[i],name)==0) && (strcmp(author[i],writer)==0))
     {
     cout<<"\n\nEntered Information Book Available\n";
     cout<<"Price of that book is "<<price[i];
     goto skip;
     }
 }
 cout<<"\n\nBook is not Available in stock\n";
skip:
}

void main()
{
clrscr();
books o1;
int choice;
while(1)
{
cout<<"\n\nChoose your choice\n";
cout<<"1) Entering Information for book\n";
cout<<"2) To See Actual stock\n";
cout<<"3) To Search for a Particular book\n";
cout<<"4) Exit\n";
cout<<"Enter your choice:-";
cin>>choice;
 switch(choice)
 {
   case 1 : o1.getdata();
             break;
   case 2 : o1.display_stock();
             break;
   case 3 : o1.search();
             break;
   case 4 : goto out;
   default: cout<<"\n\nEnter choice is invalid\nTry again\n";
 }
}
out:
}

Library Database

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>

struct library
{
char author[20],title[20],pub[20];
int price;
library *next;
};


int sum=0;

void main()
{
clrscr();
library *head=NULL;
library *initial(void);
library *purchase(library *);
//library *sale(library *);
void display(library *);
void stock(library *);
void search(library *);

int choice;
while(1)
{
cout<<"Choose your Choice\n";
cout<<"1)    Initial Data Entry\n";
cout<<"2)    Purchase of Book\n";
cout<<"3)    Sales of Book\n";
cout<<"4)    Stock of Book\n";
cout<<"5)    Search of Book\n";
cout<<"6)    Display Books\n";
cout<<"7)    Exit\n";
cout<<"Enter Your Choice:-";
cin>>choice;
    switch(choice)
   {
   case 1 : head=initial();
               getch();
               break;
   case 2 : head=purchase(head);
               getch();
               break;
  // case 3 : head=sale(head);
  //             break;
   case 4 : stock(head);
               getch();
               break;
   case 5 : search(head);
               getch();
               break;
   case 6 : display(head);
               getch();
            break;
   case 7 : goto out;
   default: cout<<"\nInvalid Choice\nTRY AGAIN\n";
   }
clrscr();
}
out:
}

library *initial(void)
{
clrscr();
library *newl=NULL,*start=NULL,*end=newl;
char ch;
    while(1)
   {
   cout<<"\n\nType y or Y for yes\n";
   cout<<"Are you Interested in Entering Entry:-";
   cin>>ch;
   if(ch=='y' || ch=='Y')
   {
         newl=new library;
      cout<<"\n\nEnter Author of Book:-";
      cin>>newl->author;
      cout<<"Enter Title of Book:-";
      cin>>newl->title;
      cout<<"Enter Publication of Book:-";
      cin>>newl->pub;
      cout<<"Enter Price of Book:-";
      cin>>newl->price;
        sum=sum+newl->price;
      if(start==NULL)
          start=newl;
      else
          end->next=newl;
      end=newl;
      end->next=NULL;
   }
   else
       break;
    }
return(start);
}

library *purchase(library *start)
{
clrscr();
int pos,count=1,choice;
library *newl,*cnt=start,*head=start;
if(start==NULL)
    cout<<"\n\nLIST IS EMPTY\n";

cout<<"\n\nChoose your Choice\n";
cout<<"1)    Inserting At FIRST POSITION\n";
cout<<"2)    Inserting In BETWEEN\n";
cout<<"3)    Inserting At LAST POSITION \n";
cout<<"4)    Exit\n";
cout<<"Enter your choice:-";
cin>>choice;

if(choice >=1 && choice <=3)
{
    newl=new library;
   cout<<"Enter Author Name :-";
   cin>>newl->author;
   cout<<"Enter Book Title :-";
   cin>>newl->title;
   cout<<"Enter Publication :-";
   cin>>newl->pub;
   cout<<"Enter Price of Book:-";
   cin>>newl->price;
   sum=sum+newl->price;
}

switch(choice)
{
    case 1 :          //for First position
                       newl->next=head;
                          head=newl;
                          break;

   case 2 :          //for Middle position
                       read:
                       cout<<"\n\nAt which position you want to insert Record:-";
                        cin>>pos;
                        while(cnt!=NULL)
                        {
                        count++;                   //cnt for counting variable of type node
                        cnt=cnt->next;
                        }
                        if(pos<1 || pos>count+1)
                       {
                        cout<<"\n\nEntered position is Invalid\nTRY AGAIN\n";
                        goto read;
                  }

                  {                    //Extra Braces are used as case bypasses intialization of a local variable
                        int c=1;
                        while(c<pos-1)
                        {
                            c++;
                            start=start->next;
                        }
                  }
                        newl->next=start->next;
                        start->next=newl;
                       break;

   case 3 :        //for Last position
                        while(start->next!=NULL)
                            start=start->next;

                       start->next=newl;
                       newl->next=NULL;
                  break;

   case 4 :         goto out;

   default:       cout<<"\nEntered Choice is Invalid Try again\n";
                       break;

}
out:
return(head);
}


void stock(library *start)
{
clrscr();
int count=0;
    while(start!=NULL)
   {
        count++;
      start=start->next;
   }
cout<<"\n\n\n\tTotal Number of Books in Stock is  "<<count<<endl;
cout<<"\tPurchase Price of Total Stock is  "<<sum;
}


void search(library *start)
{
clrscr();
char author[20],title[20];
cout<<"Enter Book title and its Author name respectively to Search in stock\n";
cin>>title>>author;
    while(start!=NULL)
   {
        if(title==start->title)
      {
          if(author==start->author)
         {
             cout<<"\n\nBook is In Stock\n";
            cout<<"It Cost Rs"<<start->price;
            return;
         }
      }
    }
cout<<"\n\nSEARCH IS NOT IN STOCK\n";
}


void display(library *start)
{
clrscr();
cout<<setw(10)<<"Book Title"<<setw(25)<<"Author of Book"<<setw(25)<<"Publication"<<setw(20)<<"Price"<<endl<<endl;
for(int i=0;i<40;i++)
    cout<<"=*";
cout<<endl;
while(start!=NULL)
{
cout<<setw(10)<<start->title<<setw(25)<<start->author<<setw(25)<<start->pub<<setw(20)<<start->price<<endl;
start=start->next;
}
}

Unary Operator ++

#include <iostream.h>
#include <conio.h>

class counter
{
  int count;
  public:
  counter(){count=0;}
  counter(int a)
  {count=a;}
  void operator ++()                   //for prefix
  {count++;}
  counter operator ++(int)             //for eg:- c1 = c2++
  {                                                //and postfix expression
     count++;
     counter temp;
     temp.count=count;
     return temp;
  }
  void getdata(void)
  {
    cout<<"\n\nEnter Value for Count :-";
    cin>>count;
  }
  void display(void)
  {
    cout<<"\nValue of count is "<<count<<endl;
  }
};

void main()
{
 clrscr();
 counter o1(9),o2,o3;
 o1++;
 o1.display();

 o2.getdata();
 o2++;
 ++o2;
 o2.display();

 o3=o2++;
 o3.display();
 getch();
}

Conversion from CLASS TO CLASS

#include <iostream.h>
#include <conio.h>

class invent1
{
 int code;
 int items;
 float price;

  public:

  invent1()
  {}

  invent1(int a,int b,int c)
  {
    code=a;
    items=b;
    price=c;
  }

  void display()
  {
    cout<<"\nCode  : "<<code;
    cout<<"\nItems : "<<items;
    cout<<"\nPrice : "<<price;
  }

  int getcode()
  {return code;}

  int getitem()
  {return items;}

  int getprice()
  {return price;}

};


class invent2
{
  int code;
  float value;

   public:

    invent2()
    {
      code=0;
      value=0;
    }

    invent2(int x,float y)
    {
      code=x;
      value=y;
    }

    void display()
    {
     cout<<"Code  : "<<code<<endl;
     cout<<"Value : "<<value<<endl;
    }

    invent2(invent1 p)
    {
     code=p.getcode();
     value=p.getitem()*p.getprice();
    }
};


void main()
{
 clrscr();
 invent1 s1(100,5,140);
 invent2 d1;

 d1=s1;  //Invoke Constructor in Invent2 for conversion

 cout<<"\nProduct details - Invent1 type";
 s1.display();

 cout<<"\n\n\nProduct details - Invent2 type\n";
 d1.display();
 getch();
}

File in c++

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<process.h>
#include<string.h>

void main()
{
    fstream fin,fout;
    char ch;
    int flag = 1;
    clrscr();
    fin.open("data.txt",ios :: out | ios :: in);
    fout.open("data1.txt",ios :: out | ios :: in);
    fin.seekg(0);
    if(fin==NULL)
       cout<<"Unable to open file";
    while(fin)
    {
        fin.get(ch);
        if(ch == ' ')
        {
             flag = 0;
        }
        else
        {
            if(flag == 0)
            {
                fout.put(' ');
                cout<< ' ';
                flag = 1;
            }
            fout.put(ch);
            cout<<ch;
        }
    }
}

Inventory and Stock

#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#define MAX_REC 10

class Inventory{
    char itemName[15];
    int code;
    double cost;
    public:
    void getdata();
    void showdata();
};

void Inventory :: getdata(){
    cout<<"\nEnter Item Name : ";
    cin>>itemName;
    cout<<"Enter Code : ";
    cin>>code;
    cout<<"Enter Cost : ";
    cin>>cost;
}

void Inventory :: showdata(){
    cout<<endl;
    cout.width(15);
    cout.setf(ios::left, ios::adjustfield);
    cout<<itemName;

    cout.width(8);
    cout<<code;

    cout.width(15);
    cout.setf(ios::right, ios::adjustfield);
    cout.setf(ios::showpoint);
    cout.setf(ios::fixed,ios::floatfield);
    cout.precision(2);
    cout<<cost;
}

void main(){
    Inventory record[MAX_REC];
    int i,n;
    clrscr();

    cout<<"\n=====Inventory Management=====\n";
    cout<<"\nHow many Records to be created : ";
    cin>>n;

    cout<<"Enter "<<n<<" Records\n";
    for(i=0;i<n;i++)
        record[i].getdata();

    cout<<"\n\n---Stock Information---\n";
    cout<<"\n"<<setw(8)<<"Item Name"
        <<setw(10)<<"Code"
        <<setw(19)<<"Cost"<<endl;
    cout<<"-------------------------------------------";

    for(i=0;i<n;i++)
        record[i].showdata();

    getch();
}

Binary Operator + to add two object

#include <iostream.h>
#include <conio.h>

class distance
{
 int feet;
 float inches;
 public:
   distance()
   { feet=0;inches=0.0;}
   distance(int ft,float in)
   { feet=ft; inches=in;}
   void getdist()
   {
     cout<<"\nEnter feet   : ";
     cin>>feet;
     cout<<"\nEnter Inches : ";
     cin>>inches;
   }
   void showdist()
   {
     cout<<feet<<"\'-"<<inches<<'\"';
   }

   distance operator + (distance);
 };

 distance distance :: operator + (distance d2)
 {
   int   f = feet + d2.feet;
   float i = inches + d2.inches;
   if(i >= 12.0)
     {
       i = i - 12.0;
       f++;
     }
   return distance(f,i);
 }

Conversion BASIC TO CLASS TIME CONVERSION

#include <iostream.h>
#include <conio.h>

class time
{
  int hrs;
  int mins;
  public:
    time()
    {}
    time(int t)
    {
      hrs=t/60;
      mins=t%60;
    }
    void display()
    {
     cout<<"\nHours   = "<<hrs;
     cout<<"\nMinutes = "<<mins;
    }
};

void main()
{
 clrscr();
 time o1;
 int  duration;
 cout<<"Enter Duration : ";
 cin>>duration;

 o1=duration;        //Invoke the constructor

 o1.display();
 getch();
}

Conversion BASIC TO CLASS TYPE string to class object

#include <iostream.h>
#include <conio.h>
#include <string.h>

class string
{
  char *p;
  int len;
  public:
    string::string()
    {}
    string::string(char *a)
    {
      len=strlen(a);
      p=new char[len+1];
      strcpy(p,a);
    }
    void display()
    {
      cout<<p;
    }
};

void main()
{
 clrscr();
 string s1,s2;
 char *name1="vivek";
 char *name2="patel";
 s1=string(name1);       //Invoke constructor
 s2=name2;               //Invoke constructor

 s1.display();
 cout<<endl;
 s2.display();
 getch();
 }

write a class to represent a vector (a series of float values). Include member functions to perform the following tasks:

1)To Create the Vector
2)To modify the value of a given element
3)To multiply by a scalar value.
4)To display the vector in the form (10,20,30....)
Write a program to test your class*/

#include <iostream.h>
#include <conio.h>

int const size=50;

class vector
{
float d[size];
int s;
public:
void create(void);
void modify(void);
void multiply(void);
void display(void);
};

void vector :: create(void)
{
cout<<"\n\nEnter of Array you want to create:-";
cin>>s;
cout<<"Enter "<<s<<" Real Numbers\n";
for(int i=0;i<s;i++)
       cin>>d[i];
}

void vector :: modify(void)
{
int mfy_value;
float with;
cout<<"\nEnter Location of array at which value is to be modified:-";
cin>>mfy_value;
cout<<"Enter Value with which you want to Replace:-";
cin>>with;
d[mfy_value]=with;
}

void vector :: multiply(void)
{
int mul;
cout<<"\nEnter value with which you want to multiply:-";
cin>>mul;
for(int i=0;i<s;i++)
    d[i]=d[i]*mul;
}

void vector :: display(void)
{
cout<<"\n\nDisplay of Array\n";
cout<<"(";
for(int i=0;i<s;i++)
{
    cout<<d[i];
 if(i!=s-1)
     cout<<",";
}
cout<<")";
}

void main()
{
clrscr();
vector o1;
int choice;
do
{
cout<<"\n\nChoice List\n";
cout<<"1)    To Create Vector Array\n";
cout<<"2)    To Modify Array\n";
cout<<"3)    To Multiply with Scalar value\n";
cout<<"4)    To Display\n";
cout<<"5)    EXIT\n";
cout<<"Enter your choice:-";
cin>>choice;
switch(choice)
{
case 1:    o1.create();
    break;
case 2:    o1.modify();
    break;
case 3: o1.multiply();
    break;
case 4: o1.display();
    break;
case 5:goto end;
}
}while(1);
end:
}

Define a class to represent a bank account. Include the following members:

/*Data Members
1    Name of the depositor
2    Account Number
3    Type of account
4    Balance amount in the account

Member function
1    To assign initial values
2    To deposit an amount
3    To withdraw an amount after checking the balance
4    To display name and Balance

write a main program to test the program.   */

#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

class    bank
{
char name[20];
int acno;
char actype[20];
int bal;
public :
void opbal(void);
void deposit(void);
void withdraw(void);
void display(void);
};

void bank :: opbal(void)
{
cout<<endl<<endl;
cout<<"Enter Name :-";
cin>>name;
cout<<"Enter A/c no. :-";
cin>>acno;
cout<<"Enter A/c Type :-";
cin>>actype;
cout<<"Enter Opening Balance:-";
cin>>bal;
}

void bank :: deposit(void)
{
cout<<"Enter Deposit amount :-";
int deposit=0;
cin>>deposit;
deposit=deposit+bal;
cout<<"\nDeposit Balance = "<<deposit;
bal=deposit;
}

void bank :: withdraw(void)
{
int withdraw;
cout<<"\nBalance Amount = "<<bal;
cout<<"\nEnter Withdraw Amount :-";
cin>>withdraw;
bal=bal-withdraw;
cout<<"After Withdraw Balance is "<<bal;
}

void  bank :: display(void)
{
cout<<endl<<endl<<endl;
cout<<setw(50)<<"DETAILS"<<endl;
cout<<setw(50)<<"name      "<<name<<endl;
cout<<setw(50)<<"A/c. No.     "<<acno<<endl;
cout<<setw(50)<<"A/c Type      "<<actype<<endl;
cout<<setw(50)<<"Balance     "<<bal<<endl;
}

void main()
{
clrscr();
bank o1;
int choice;
    do
    {
           cout<<"\n\nChoice List\n\n";
           cout<<"1)  To assign Initial Value\n";
         cout<<"2)  To Deposit\n";
         cout<<"3)  To Withdraw\n";
         cout<<"4)  To Display All Details\n";
         cout<<"5)  EXIT\n";
         cout<<"Enter your choice :-";
         cin>>choice;
         switch(choice)
         {
         case 1: o1.opbal();
                     break;
         case 2: o1.deposit();
                     break;
           case 3: o1.withdraw();
                     break;
         case 4: o1.display();
                     break;
            case 5: goto end;
         }
   }while(1);
end:
}

Write a function power() to raise a number m to a power n. The function takes a double value for m and int value for n and returns the result correctly. Use a default value of 2 for n to make the function to calculate squares when this argument is omitted. Write a main that gets the values of m and n from the user to test the function.

#include <iostream.h>
#include <conio.h>
void main()
{
double power(double m,int n=2);
clrscr();
cout<<endl<<endl<<endl;
int choice,m,n;
double result;
do
{
cout<<"\n\n\nCHOICES\n\n";
cout<<"1)    Only Value of M\n";
cout<<"2)    Value for both M and N\n";
cout<<"3)    QUIT\n";
cout<<"ENTER YOUR CHOICE:-";
cin>>choice;
clrscr();
cout<<endl<<endl<<endl;
switch(choice)
{
    case 1 : cout<<"Enter Value for M:-";
               cin>>m;
            result=power(m);
            cout<<"Power function when default argument is used ="<<result;
            break;
   case 2 : cout<<"Enter Value for M and N:-";
               cin>>m>>n;
            result=power(m,n);
            cout<<"Power function when actual argument is use ="<<result;
            break;
   case 3 : goto out;
   default: cout<<"Entered Value is Invalid, Try again";
}
}while(choice!=3);
out:
}


double power(double m,int n)
{
double pow=1,k=0;
for(int i=0;i<n;i++)
{
pow=pow*m;
}
return(pow);
}

Write a macro that obtains the largest of three numbers.

#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int largest(int,int,int);
cout<<"Enter 3 Integer Numbers\n";
int a,b,c;
cin>>a>>b>>c;
int result;
result=largest(a,b,c);
cout<<"\n\nLargest Value of Inputed is "<<result;
getch();
}

inline largest(int a,int b,int c)
{
int z;
z=(a>b)?((a>c)?a:c):((b>c)?b:c);
return(z);
}

The Effect of a default argument can be alternatively achieved by overloading. Discuss with an example.

#include <iostream.h>
#include <conio.h>
void main()
{
int sum(int,int);         //function with 2 argument
int sum(int,int,int);     //function with 3 argument
int sum(int,int,int,int); //function with 4 argument
int sum(int[],int);       //function with n argument
clrscr();
int a,b,c,d,result;

cout<<"\n\nfor 2 argument\n";
cout<<"Enter 2 Integers\n";
cin>>a>>b;
result=sum(a,b);
cout<<"Addition ="<<result;


cout<<"\n\nfor 3 argument\n";
cout<<"Enter 3 Integers\n";
cin>>a>>b>>c;
result=sum(a,b,c);
cout<<"Addition ="<<result;



cout<<"\n\nfor 4 argument\n";
cout<<"Enter 4 Integers\n";
cin>>a>>b>>c>>d;
result=sum(a,b,c,d);
cout<<"Addition ="<<result;


cout<<"\n\nHow many Argument You want to enter:-";
int no;
cin>>no;
int num[50];
cout<<"Enter "<<no<<" Integers\n";
for(int i=0;i<no;i++)
    cin>>num[i];
result=sum(num,no);
cout<<"Addition ="<<result;

getch();
}


//function with 2 argument
int sum(int a,int b)
{
return(a+b);
}


//function with 3 argument
int sum(int a,int b,int c)
{
return(a+b+c);
}

//function with 4 argument
int sum(int a,int b,int c,int d)
{
return(a+b+c+d);
}

//function with n argument
int sum(int a[],int n)
{
int sum=0;
    for(int i=0;i<n;i++)
   {
      sum=sum+a[i];
   }
return(sum);
}

Matrix of DEFAULT VALUE

#define row 50
#define col 50
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
void main()
{
void data_entry_matrix(int cols,int rows=3);
clrscr();
cout<<"\n\n\tusing rows as default value";


cout<<"\n\n\tEnter Number of Column You want to enter:-";
int cols;
cin>>cols;

data_entry_matrix(cols);

getch();
}



void data_entry_matrix(int c, int r)
{
clrscr();
int matrix[row][col];
cout<<"\n\n\t\tEnter Your Data for Matrix "<<r<<"x"<<c<<"\n\n\n\t";
//for input
for(int i=0;i<r;i++)
{
    for(int j=0;j<c;j++)
   {
       cin>>matrix[i][j];
   }
}

cout<<endl<<endl<<endl;
//for display
cout<<"\n";
 for(int i=0;i<r;i++)
{
    for(int j=0;j<c;j++)
   {
       cout<<setw(5)<<matrix[i][j];
   }
   cout<<endl;
}

}

Write a function to read a matrix of size m x n from the keyoard.

#define row 50
#define col 50
#include <iostream.h>
#include <conio.h>
void main()
{
void data_entry_matrix(int,int);
clrscr();
cout<<"\n\n\tEnter Number of rows you want to enter:-";
int rows;
cin>>rows;

cout<<"\n\n\tEnter Number of Column You want to enter:-";
int cols;
cin>>cols;

data_entry_matrix(rows,cols);

getch();
}


Electricity Bill

/*An electricity board charges the following rates to domestic
users to discourage large consumption of energy:
    For the first 100 units - 40p per unit
   For next 200 units        - 50p per unit
   Beyond 300 units            - 60p per unit
All users are charged a minimum of Rs.500.  If the total cost is
more than Rs.250.00 then an additional surcharge of 15% is added.
    Write a program to read the names of users and number of units
consumed and print out the charges with names.*/

#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
cout<<"\n\n\n\tElectricity Board Charges\n";
cout<<"\n\tTo Discourage Large Consumption of energy\n\n";



char name[20];
cout<<"\n\nEnter USER name :-";
cin>>name;

cout<<"\n\nEnter Number of Units Consumed:-";
float unit;
cin>>unit;

//MANIPULATION
float tc;
if(unit<=100)
    tc=unit*0.40;
else if(unit<=300)
    tc=unit*0.50;
else
    tc=unit*0.60;


float surchase=0;
if(tc>250)
    surchase=tc*0.15;

float total_cost;
total_cost = 500 + surchase + tc;

cout<<"\n\nYOUR BILL AMOUNT IS "<<total_cost;

getch();
}


write a program to print a table of values of the function

//y=e raise to -x
//for x varying from 0 to 10 in steps of 0.1. the table should appear as follows.
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#include <math.h>

class exponent
{
float i,j;
public:
exponent()
{i=0;j=0.0;}
void display(void);
};

void exponent :: display(void)
{
double sum;
    cout<<"\n\n\t\t\tTABLE FOR Y = EXP[-X]\n";
    for(int a=0;a<40;a++)
        cout<<"--";
    cout<<"\n";
    for(int c=0,b=0.0;c<10;b++,c++)
    {
        if(b==0.0)
        cout<<setw(c+5)<<"X";
        else
        cout<<setw(c+3)<<b;
    }
    cout<<endl;
    for(int d=0;d<40;d++)
         cout<<"==";
    cout<<endl;
c=0;
    while(i<10)
    {
        while(c<10)
        {
            sum=1/pow(i,j);
            if(c==0)
               cout<<setw(5)<<i;
            else
               cout<<setw(3)<<sum;
            if(c>10)
            break;
        }
    cout<<endl;
    }
}

void main()
{
clrscr();
exponent o1;
o1.display();
getch();
}

write programs to evaluate the following to 0.0001% accuracy.

 (B) SUM = 1+[(1/2)raise to 2]+[(1/3)raise to 3]+.....

#include<math.h>
#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int num;
cout<<"Enter the number:- ";
cin>>num;
float r,sum=0,p;
int k,i;
for(k=1,i=1;i<=num;i++,k++)
{
    r=1.0/k;
   p=pow(r,i);
   sum=sum+p;
}
cout<<"\n\nSUM = "<<sum;
getch();
}

OUTPUT PROGRAM

1
22
333
....

#include<iostream.h>
#include<conio.h>
void main()
{
int i,num,j;
clrscr();
cout<<"Enter number for corresponding output:- ";
cin>>num;
for(i=1;i<=num;i++)
{
    for(j=1;j<=i;j++)
   {
   cout<<i;
   }
cout<<"\n";
}
num--;
getch();
}

Write a function that creates a vector of user-given size M using new operator.

#define null 0
#include <iostream.h>
#include <conio.h>



void main()
{
void memory(int);
clrscr();
textcolor(16);
textbackground(1235);
clrscr();
cout<<"Enter Memory M you Want to create:-";
int size;
cin>>size;
memory(size);
getch();
}


//MEMORY function
void memory(int s)
{
int *m = new int[s];
if(m!=null)
    {
    cout<<"\nWe are Successfull";
   cout<<"\n\n\n\n\tNow You Want to Delete This Created Memory";
   cout<<"\n\n\tEnter Y or y for Deleting else anything:-";
   char ch;
   cin>>ch;
       if(ch=='y' || ch=='Y')
          {
             delete[]m;
            cout<<"\n\n\n\tCreated Memory is Delete";
         }
      else
          cout<<"\n\n\tOK, your Memory is Safe";
   }
else
    cout<<"\nWe are UN-Successfull";
}

write a function using refrence variable as argument to swap the value of a pair of integers.

#include <iostream.h>
#include <conio.h>

class swaping
{
int a;
public :
void getdata(void);
friend void swap(swaping &,swaping &);
void display(void);
};

void swaping :: getdata(void)
{
cout<<"\n\nEnter any Integer :-";
cin>>a;
}

void swap(swaping &o1,swaping &o2)
{
int temp;
temp    =    o1.a;
o1.a    =    o2.a;
o2.a    =    temp;
}

 void swaping :: display(void)
{
cout<<a<<endl;
}

void main()
{
clrscr();
swaping o1,o2;

cout<<"Before Swaping\n";
o1.getdata();
o2.getdata();

swap(o1,o2);

cout<<"\n\nAfter Swaping\n";
o1.display();
o2.display();

getch();
}

Write a program to read two numbers from the keyboard and display the larger value on the screen.

#include<conio.h>
#include<iostream.h>

class largest
{
int d;
public :
void getdata(void);
void display_large(largest,largest);
};

void largest :: getdata(void)
{
cout<<"\n\nEnter Value :-";
cin>>d;
}

void largest :: display_large(largest o1,largest o2)
{
    if(o1.d > o2.d)
       cout<<"\nObject 1 contain Largest Value "<<o1.d;
   else if(o2.d > o1.d)
       cout<<"\nObject 2 contain Largest Value "<<o2.d;
   else
       cout<<"\nBOTH ARE EQUAL";
}


void main()
{
largest o1,o2,o3;
clrscr();

o1.getdata();
o2.getdata();

o3.display_large(o1,o2);
getch();
}

Using a CLASS called TEMP and member functions.

#include<iostream.h>
#include<conio.h>

class TEMP
{
    float f,c;
   public:
      float  getdata();
      void display(void);
};

 float  TEMP :: getdata()
{
cout<<"Enter Value for farenheit degree to find for celsius:- ";
cin>>f;
c = (f-32)/1.8;
return(c);
}

void TEMP :: display(void)
{
float v;
 v=getdata();
cout<<"CELSIUS DEGREE = "<<v;
}

void  main()
{
TEMP c;
c.display();
}

Write a C++ program that will ask for a temperature in Fahrenheit and display it in Celsius.

#include<conio.h>
#include<iostream.h>
void main()
{
float f,c;
clrscr();
cout<<"Enter Fahrenheit degree to find temperature in celsius: ";
cin>>f;
c = (f-32)/1.8;
cout<<"\n\n\tCELSIUS DEGREE = "<<c;
getch();
}

Write a program to read the value of a,b and c and display

the value of x, where
                       X=A/B-C   
#include<conio.h>
#include<iostream.h>
void main()
{
float x,a,b,c,check;
clrscr();
cout<<"\nEnter value for a,b and c respectively\n";
cin>>a>>b>>c;
check=b-c;
if(check == 0)
cout<<"\n\nIMAGINARY NUMBERS\n";
else
{
x=a/(b-c);
cout<<"\n\nValue of X = "<<x;
}
getch();

Wednesday, April 6, 2011

W.A.P to use of pure virtual function


#include<iostream.h>
#include<conio.h>
            class cpolygon {
                        protected:
                                    int l,h;
                        public:
                                    void setval(int a, int b){
                                                l=a;
                                                h=b;
                                    }
                                    virtual int area(void)=0;
            };
            class crectangle : public cpolygon {
                        public:
                                    int area(void) {
                                                return(l*h);
                                    }
            };
            class ctriangle : public cpolygon {
                        public:
                                    int area(void) {
                                     return((l*h)/2);
                                     }
            };

void main() {
            clrscr();
            crectangle crect;
            ctriangle tangle;
            cpolygon *cpoly;
            cpolygon *poly1 = &crect;
            cpolygon *poly2 = &tangle;

            poly1->setval(4,5);
            poly2->setval(4,5);

            cout<<"Area : = "<<poly1->area();
            cout<<"\nArea : = "<<poly2->area();

            getch();
}



OUTPUT:-



Area : = 20
Area : = 10

Programms

C,C++,VB, PL/SQL EBOOK SEARCH ENGINE