Pages

Wednesday 22 September 2021

One & Two Dimensional Array in c++

// Program to illustrate one dimensional array
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class onedarray
{
 public:
int a[33],i,n;
 public:
void getdata();
void putdata();
};

void onedarray::getdata()
{
 cout<<"Enter the size"<<endl;
 cin>>n;
 for(i=0;i<n;i++)
  {
  cout<<"Element a["<<i<<"] :"<<ends;
  cin>>a[i];
  }
}

void onedarray::putdata()
{
   /* for(i=0;i<n;i++)
     {
      cout<<a[i]<<" ";
     }*/ Comment is to display with out Address Array Data Location 
 
 for(i=0;i<n;i++)
 {
  cout<<"\nAddress:"<<&a[i]<<setw(15)<<"Array:"<<"a["<<i<<"]"<<setw(15)<<"Data:"<<a[i]<<setw(15)<<"Location:"<<i+1;
  cout<<"\n";
 }
}

void main()
{
 onedarray od;
 clrscr();
 od.getdata();
 od.putdata();
 getch();
}

Output:


 
//Program to illustrate Two dimensional array
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class twodarray
{
 public:
 int a[11][22],i,j,m,n;
 public:
 void getdata();
 void putdata();
};

void twodarray::getdata()
{
 cout<<"Enter the row size"<<endl;
 cin>>m;
 cout<<"Enter the column size"<<endl;
 cin>>n;
 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
   {
    cout<<"Element a["<<i<<"]"<<"["<<j<<"]"<<ends;
    cin>>a[i][j];
   }
 }
}

void twodarray::putdata()
{
 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
  {
   cout<<"\nAddress:"<<&a[i][j]<<setw(15)<<"Array:"<<"a["<<i<<"]"<<"["<<j<<"]"<<setw(15)<<"Data:"<<a[i][j];
   cout<<"\n";
  }
 }
}
void main()
{
 twodarray td;
 clrscr();
 td.getdata();
 td.putdata();
 getch();
}

Output:







Monday 20 September 2021

Insertion sort in c++

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class insertionsort
{
 public:
 int a[33],i,j,n,temp;
 public:
 void getdata();
 void sort();
 void display();
};

void insertionsort::getdata()
{
 cout<<"Enter the size :"<<ends;
 cin>>n;
 for(i=0;i<n;i++)
  {
  cout<<"Enter the Element a["<<i<<"] :"<<ends;
  cin>>a[i];
  }
}

void insertionsort::sort()
{
  temp=0;
  for(j=0;j<n;j++)
    {
     temp=a[j];
     for(i=j-1;i>=0 && temp<a[i];i--)
       {
a[i+1]=a[i];
       }
      a[i+1]=temp;
      cout<<"\n pass"<<ends<<j+1<<ends<<"element inserted in proper place"<<ends<<temp<<endl;
      for(i=0;i<n;i++)
      {
      cout<<a[i]<<"  ";
      }
    }
}

void insertionsort::display()
 {
    cout<<"\n Sorted Array elements :";
    for(i=0;i<n;i++)
    {
     cout<<a[i]<<" ";
    }
    for(i=0;i<n;i++)
    {
     cout<<"\nAddress:"<< &a[i] <<"\tArray:"<<"a["<<i<<"]"<<"\tData:"<<a[i]<<"\tPosition or Location:"<<i+1;
    }
 }

void main()
{
 insertionsort is;
 clrscr();
 is.getdata();
 is.sort();
 is.display();
 getch();
}

output:


Insertion sort animation video link:
https://www.youtube.com/watch?v=JU767SDMDvA

Selection sort in c++


#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class selectionsort
{
 public:
int a[33],i,j,n,min,temp;
 public:
void getdata();
void sort();
void display();
};
void selectionsort::getdata()
{
 cout<<"Enter the size :"<<ends;
 cin>>n;
 for(i=0;i<n;i++)
  {
  cout<<"Enter the Element a["<<i<<"] :"<<ends;
  cin>>a[i];
  }
}
void selectionsort::sort()
{
  temp=0;
  for(i=0;i<n;i++)
    {
     min=i;
     for(j=i+1;j<n;j++)
      {
if(a[j]<a[min])
{
min=j;
}
      }
      temp=a[i];
      a[i]=a[min];
      a[min]=temp;
     cout<<"\n Pass"<<ends<<i+1<<ends<<"value that is to swap"<<ends<<temp<<ends<<"with minimum value"<<setw(5)<<a[i];
    }
}

void selectionsort::display()
{
    cout<<"\n Sorted Array elements :";
    for(i=0;i<n;i++)
     {
     cout<<a[i]<<" ";
     }
    for(i=0;i<n;i++)
    {
     cout<<"\nAddress:"<< &a[i] <<"\tArray:"<<"a["<<i<<"]"<<"\tData:"<<a[i]<<"\tLocation or Position:"<<i+1;
    }
}


void main()
{
selectionsort ss;
clrscr();
ss.getdata();
ss.sort();
ss.display();
getch();
}

Output:

Selection sort animation video link:
https://www.youtube.com/watch?v=g-PGLbMth_g

Program to illustrate Arrays of Objects in c++

An array of a class type is also known as an array of objects. An array of objects is declared in the same way as an array of any built-in data type.

//Program to illustrate one dimensional static array
#include<iostream.h>
#include<conio.h>
class student
 {
  int Id;
  char Name[25];
  public:
  void GetData()           //Statement 1 : Defining GetData()
  {
   cout<<"\nEnter student Id : ";
   cin>>Id;
   cout<<"\nEnter student Name : ";
   cin>>Name;
  }
  void PutData()           //Statement 2 : Defining PutData()
  {
   cout<<Id<<"\t\t"<<Name;
  }
};
void main()
{
  int i;
  clrscr();
  student s[3];
  for(i=0;i<3;i++)
  {
   cout<<"\nEnter details of "<<i+1<<" students";
   s[i].GetData();
  }
  cout<<"Student Details"<<endl;
  cout<<"Student Id"<<"\t"<<"Student Name"<<endl;
  for(i=0;i<3;i++)
  {
  s[i].PutData();
  cout<<endl;
  }
getch();
}

//Program to illustrate one dimensional dynamic array
#include<iostream.h>
#include<conio.h>
class std
 {
  int id;
  char name[25];
  public:
  void getdata();           //Statement 1 : Defining GetData()
  void putdata();
 };
void std::getdata()
  {
   cout<<"\nEnter student Id : ";
   cin>>id;
   cout<<"\nEnter student Name : ";
   cin>>name;
  }
void std::putdata()           //Statement 2 : Defining PutData()
  {
   cout<<id<<"\t\t"<<name;
  }

void main()
{
  int i,n;
  std s[160];
  clrscr();
  cout<<"Enter the size of the Array";
  cin>>n;
  for(i=0;i<n;i++)
  {
   cout<<"\nEnter details of "<<i+1<<" students";
   s[i].getdata();
  }
  cout<<"Student Details"<<endl;
  cout<<"Student Id"<<"\t"<<"Student Name"<<endl;
  for(i=0;i<n;i++)
  {
  s[i].putdata();
  cout<<endl;
  }
getch();
}

Sunday 19 September 2021

new and delete operators in c++

new operator:
The new operator requests for the memory allocation in heap. If the sufficient memory is available, it initializes the memory to the pointer variable and returns its address.

Here is the syntax of new operator in C++ language,
pointer_variable = new datatype;

Here is the syntax to initialize the memory,
pointer_variable = new datatype(value);

Here is the syntax to allocate a block of memory,
pointer_variable = new datatype[size];

#include<iostream.h>
#include<conio.h>
void main()
{
 int *x;
 float *y;
 char *z;
 clrscr();
 x = new int(10);
 cout<<&x<<ends<<*x<<endl;
 y = new float(1.5);
 cout<<&y<<ends<<*y<<endl;
 z = new char('k');
 cout<<&z<<ends<<*z<<endl;
getch();
}

output:

//Program to illustrate one dimensional array using new and delete operators
#include<iostream.h>
#include<conio.h>
void main()
{
 int n;
 clrscr();
 cout<<"enter array size:";
 cin>>n;
 int *p = new int[n];
 for(int i=0;i<n;i++)
  {
    cin>>p[i];// cin=[p+i]
  } 
 cout<<"elements are:";
 for(i=0;i<n;i++)
  {
  cout<<ends<<p[i];  //cout=*[p+i]
  }
delete p;
getch();
}


Thursday 16 September 2021

Programs on Access specifier (public,private,protected) in c++

 Program on Access specifier 

(public,private,protected) 

 

WITHIN

CLASS

OUTSIDE

CLASS

DERIVED

CLASS

PUBLIC

YES

YES

YES

PRIVATE

YES

NO

NO

PROTECTED

YES

NO

YES


Program on Access specifier (public)
#include<iostream.h>
#include<conio.h>
class accspe
{
 public:
 int x;
 private:
 int y;
 protected:
 int z;
};

void main()
{
 clrscr();
 accspe as;
 cout<<"enter x public value:"<<endl;
 cin>>as.x;
 cout<<"x public value:"<<as.x<<endl;
 getch();
}

Program on Access specifier (private)
#include<iostream.h>
#include<conio.h>
class accspe
{
 public:
 int x;
 private:
 int y;
 protected:
 int z;
 public:
 void display()
 {
   y=20;
   cout<< "y value can be seen if 
               it is inside the class private:"<<y<<endl;
 }
};

void main()
{
 clrscr();
 accspe as;
 cout<<"enter x public value:"<<endl;
 cin>>as.x;
 cout<<"x public value:"<<as.x<<endl;
/* cout<<"enter y private value:"<<endl;
 cin>>as.y;
 cout<<"y private value:"<<as.y<<endl;
 */         we will get error if we remove comment
 as.display();
 getch();
}

Program on Access specifier (protected)
#include<iostream.h>
#include<conio.h>
class accspe
{
 public:
 int x;
 private:
 int y;
 protected:
 int z;
 public:
 void display()
 {
   y=20;
   cout<< "y value can be seen if it is inside the class private:"<<y<<endl;
 }
};

class derived : public accspe
{
  public:
  void show()
  {
  z=30;
  cout<< "z value Potected is "<<z<<endl;
  }
};

void main()
{
 clrscr();
 accspe as;
 cout<<"enter x public value:"<<endl;
 cin>>as.x;
 cout<<"x public value:"<<as.x<<endl;
/* cout<<"enter y private value:"<<endl;
 cin>>as.y;
 cout<<"y private value:"<<as.y<<endl;
 */
 as.display();
 derived d;
 d.show();
 getch();
}


Monday 6 September 2021

Exception Handling in C++

C++ Exceptions
When executing C++ code, different errors can occur: 
  • coding errors made by the programmer
  • errors due to wrong input.
  • The technical term for this is: C++ will throw an exception (throw an error).
C++ try throw and catch
It consist of three keywords: try, throw and catch:

The try statement allows you to define a block of code to be tested for errors while it is being executed.

The throw keyword throws an exception when a problem is detected, 

The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

The try, throw and catch keywords can not run in Turboc++ it can run on other software's available such as Dev c++

#include<iostream>
using namespace std;
int main()
 {
   int a,b;
   try
    {
     cout<<"enter a and b value:";
     cin>>a>>b;
     if (b == 0)
        throw (0);
     else
cout << "division is :"<<a/b;
    }
   catch (int x)
    {
    cout << "Division not possible ";
    }
  return 0;
}

Saturday 4 September 2021

Functions in c++

Functions are used for Placing or Storing the Code which is to be Repeated Several Times. For Example, if we need Same Code, then we must have to Write that Code Again and Again So that for Removing this Task, we uses functions

Functions Provides us the Following Features

• Reusability of Code : Means Once a Code has Developed then we can use that Code any Time.

• Remove Redundancy: Means a user doesn’t need to Write Code Again and Again.

• Decrease Complexity: Means a Large program will be Stored in the Two or More Functions. So that this will makes easy for a user to understand that Code.

There are Two Types of Functions 

• Built in Functions

All the functions those are provided by the C++ Language are Pre defined and Stored in the Form of header Files and they are refered to the Built in Functions For example. cin and cout, getch , Clrscr 

• User Defined functions

But in the other hand the Functions those are developed by the user for their Programs are known as User Defined Programs. When a user wants to make his Own Function, then he must have to follow the Following Operations.

• Function Declaration or Prototyping

• Function Defining

• Calling a Function 

1) Function Declaration or Prototyping: The Function declaration contains the Name of Function, Return type of the Function and also the Number of Arguments that a User will takes for performing the Operation.

The Function Prototyping Contains 

1) Return Type of a function: If a Function is declared with the void Keyword or if a Function Contains a void then that’s means a Function Never Returns a value. If a Function Contain any other data type means if a Function Contains int or Float then the Function must return a value to the user.

2) Name of Function : The Name of Function must be valid and the name of function must be Start from any Alphabet and the Name of Function doesn’t Contains any Spaces and Special Character For Example Space , * sign etc.

3) Argument List: A Function may have zero or More Arguments.The Arguments those are used by the Function Calling are known as the Actual Arguments and the Arguments those are used in the Function declaration are Known as the Formal Arguments, When we call any Function then the Actual Arguments will Match the Formal Arguments and if a proper Match is Found, then this will Executes the Statements of the Function otherwise this will gives you an error Message.

There are Two Ways for Calling a Function 
1) Call by Value:-when we call a Function then we will just pass the variables or the Arguments and we doesn’t Pass the Address of Variables ,So that the function will never effects on the Values or on the variables. So Call by value is just the Concept in which you must have to Remember that the values those are Passed to the Functions will never effect the Actual Values those are Stored into the variables.

2) Call By Reference :-When we pass the Address of variables to the Arguments then a Function may effect on the Variables. Means When a Function will Change the Values then the values of Variables gets Automatically Changed. And When a Function performs Some Operation on the Passed values, then this will also effect on the Actual Values.

Program on Call by value in C++
In call by value, original value is not modified.
#include<iostream.h>
#include<conio.h>
void cbv(int,int); // function declaration or prototype

void cbv(int p,int q)  // called function
 {
  int temp;
  temp=p;
  p=q;
  q=temp;
  cout<<"\n Value of p (inside function) = "<<p;
  cout<<"\n Value of q (inside function) = "<<q;
 }

void main()
{
 clrscr();
 int a,b;
 cout<<"\n Enter a value :";
 cin>>a;
 cout<<"\n Enter b value :";
 cin>>b;
 cout<<"\n Initial values of a= " <<a << " and b is:"<<b;
 cout<<"\n Calling the function ";
 cbv(a,b);   // calling function
 cout<<"\n Final values of a= " <<a << " and b is :"<< b;
 getch();
}

Output:
Enter a value :11
Enter b value :22
Initial values of a=11 and b is: 22
Calling the function 
Value of p (inside function) = 22
Value of q (inside function) = 11
Final values of a=11 and b is: 22

Example 2:
#include<iostream.h>  
#include<conio.h>  
void change(int data);  
void main()  
{  
int data = 3;  
change(data);  
cout << "Value of the data is: " << data<< endl;  
getch();  
}  
void change(int data)  
{  
data = 5;  
}  

Output:
Value of the data is: 3


Program on Call by reference in C++
In call by reference, original value is modified because we pass reference (address).

#include<iostream.h>
#include<conio.h>
void cbr(int *,int *); // function declaration or prototype

void cbr(int *p,int *q)  // called function
 {
  int temp;
  temp=*p;
  *p=*q;
  *q=temp;
  cout<<"\n Value of p (inside function) = "<<*p;
  cout<<"\n Value of q (inside function) = "<<*q;
 }

void main()
{
 int a,b;
 cout<<"\n Enter a value :";
 cin>>a;
 cout<<"\n Enter b value :";
 cin>>b;
 cout<<"\n Initial values of a= "<<a <<" and b is :" <<b;
 cout<<"\n Calling the function ";
 cbr(&a,&b);   // calling function
 cout<<"\n Final values of a= "<<a <<" and b is :" <<b;
 getch();
}

Output:
Enter a value :11
Enter b value :22
Initial values of a=11 and b is: 22
Calling the function 
Value of p (inside function) = 22
Value of q (inside function) = 11
Final values of a=22 and b is:11

Friday 3 September 2021

Input/Output Statements in C++

In C++, input and output (I/O) operators are used to take input and display output. 
The operator used for taking the input is known as the extraction or get from operator (>>), 
while the operator used for displaying the output is known as the insertion or put to operator (<<).

Input Operator
The input operator, commonly known as the extraction operator (>>), is used with the standard input stream, cin. 

Program to demonstrate the working of an input operator.
#include<iostream.h>
#include<conio.h>
voiid main ()
{
  int a;  
  cin>>a;
  a = a+1;
  getch();
}
In this example, the statement cin>> a takes an input from the user and stores it in the variable a.

Output Operator
The output operator, commonly known as the insertion operator (<<), is used. The standard output stream cout Like cin, cout also treats data as a stream of characters. 

Program to demonstrate the working of an output operator.
#include<iostream.h>
#include<conio.h>
void main ()
{
  int a;
  cin>>a;
  a=a+1;
  cout<<a;
  getch();
}

Cascading of Input/Output Operators
The cascading of the input and output operators refers to the consecutive occurrence of input or output operators in a single statement.

Program without cascading of the input/output operator.
#include<iostream.h>
#include<conio.h>
void main ()
 {
   int a, b;
   cin>>a;
   cin>>b;
   cout<<"The value of a is";
   cout<<a;
   cout<<"The value of b is";
   cout<<b;
   getch();
}

In this example, all cin and cout statements use separate input and output operators respectively However, these statements can be combined by cascading the input and output operators accordingly as shown in this example.

Program with cascading of the input/output operator

#include<iostream.h>
#include<conio.h>
int main ()
{
  int a, b;
  cin>>a>>b;
  cout<<"The value of b is : "<<b;
  cout<<"The value of a is "<<a;
  return 0;
}

The Un-Buffered Standard Error Stream (cerr)
The object cerr is an instance of ostream class and it is tied to the standard error device, which display error message on the screen. Object cerr is an un-buffered so each stream inserted displayed immediately on output device. 

#include <iostream>
using namespace std;
int main() 
{
   cerr<<"Error occurred!"<<endl;
   return 0;
}
Output:
Error occurred!

The Standard Log Stream (clog) – Buffered error stream
The object clog is an instance of ostream class and it is tied to the standard error device, which display error message on the screen but the object clog is buffered. This means that each insertion to clog will be held in a buffer until the buffer is filled or until the buffer is flushed.

#include <iostream>
using namespace std;
int main()
 {
   clog<<"Error Occurred!"<<endl;
   return 0;
 }
Output:
Error Occurred!

Note:If you look at above three examples of cout, cerr, clog there is no significant difference between them. 

Some important manipulators in <iomanip.h> are of 2 types 
1.Manipulator operator  
eg: endl - end the line
      ends- give a single space between two objects
      cout<<"ab"<<ends<<"cd";

2.Manipulator functions
eg: setw(), setbase(),setfill(), setprecision(), setonflag(), 
setw (val): It is used to set the field width in output operations.
setbase(val): It is used to set the numeric base value for numeric values.
setfill (c): It is used to fill the character ‘c’ on output stream.
setprecision (val): It sets val as the new value for the precision of floating-point values.
setiosflags(flag): It is used to set the format flags specified by parameter mask.
resetiosflags(m): It is used to reset the format flags specified by parameter mask.

Program to illustrate manipulators 

#include <iostream.h>
#include<conio.h>
#include <iomanip.h>
void main()
 {
    clrscr();
    cout<<"ab"<<ends<<"cd"<<endl;
    cout << setprecision(2) << 1023.26556 << endl;
    cout << setbase(8)<<62<<endl;
    cout << setbase(16)<<62<<endl;
    cout << setbase(10)<<62<<endl;

    cout << setw(10)<<setiosflags(ios::left)<<"Hello"<<endl;
    cout << setw(10)<<setiosflags(ios::right)<<"Hello"<<endl;
    cout << setw(10)<<setiosflags(ios::oct)<<65;
    cout << setw(10)<<setiosflags(ios::hex)<<65;
    cout << setw(10)<<setiosflags(ios::dec)<<65;

    cout << setfill('*')<<endl;
    cout <<setw(6)<<"rama"<<endl;

    getch();
 } 

output:


Constructors & Destructors in c++

  Constructors :  A Constructor is a special member function, which is used to initialize the objects of its class. The Constructor is invok...