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:







No comments:

Post a Comment

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...