यहाँ इस program में, class और switch statement का उपयोग करके program को बेहतर बनाने की कोशिश की गई है। साथ ही program में उपयुक्त comments दी गई हैं, ताकि program के implements को समझा जा सके।
class with multi dimensional array Program in C++
#include<iostream.h>
#include<conio.h>
#include<stdib.h> //exit function
class array // class name array
{
int x[2][3],y[2][3],i,j;
public:
void getdata()
{
cout<<"Enter the element in the 1st array\n";
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
cout<<"a["<<i<<"]["<<j<<"] = "; cin>>x[i][j]; // storing value into variable x[][]
}
}
cout<<endl;
cout<<"Enter the element in the 2nd array\n";
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
cout<<"a["<<i<<"]["<<j<<"] = ";
cin>>y[i][j]; // storing value into variable y[][]
}
}
}
void putdata()
{
cout<<"1st array:\n";
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
cout<<x[i][j]<<" "; // displaying value of variable x[][]
}
}
cout<<endl;
cout<<"2nd array:\n";
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
cout<<y[i][j]<<" "; //displaying value of variable y[][]
}
}
cout<<endl;
}
void sum()
{
cout<<"adition of element in the array\n";
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
cout<<x[i][j]+y[i][j]<<" "; //sum of array-variable
}
}
}
void substract()
{
cout<<"subtraction of element in the array\n";
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
cout<<x[i][j]-y[i][j]<<" "; //subtraction of array-variable
}
}
}
void multi()
{
cout<<"multiplicaton of element in the array\n";
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
cout<<x[i][j]*y[i][j]<<" "; //multiply of array-variable
}
}
}
void div()
{
cout<<"division of element in the array\n";
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
cout<<x[i][j]/y[i][j]<<" "; //devision of array-variable
}
}
}
}; // class terminate here
void main()
{
clrscr();
int ch,i=0,a;
array obj; // declared object of array class
obj.getdata(); // calling function using object obj
clrscr();
cout<<"Your entered list are\n";
obj.putdata();// calling function using object obj
// creating a menu wizard using do-while loop
do {
cout<<endl<<endl;
cout<<"***OPERATION MENU***\n";
cout<<"enter your choice\n";
cout<<"1.ADITION\n";
cout<<"2.SUBSTRACTION \n";
cout<<"3.MULITPLY\n";
cout<<"4.DIVISION\n";
cout<<"0.EXIT\n"; cin>>ch;
//calling each function using case
switch(ch)
{
case 1: {
clrscr();
obj.sum();
cout<<endl;
obj.putdata();
break;
}
case 2: {
clrscr();
obj.substract();
cout<<endl;
obj.putdata();
break;
}
case 3: {
clrscr();
obj.multi();
cout<<endl;
obj.putdata();
break;
}
case 4: {
clrscr();
obj.div();
cout<<endl;
obj.putdata();
break;
}
case 0: {
exit(0);
break;
}
default : cout<<"please choose valid option\n";
break;
}
} while(ch!=0);
getch();
}
OUTPUT
Enter the element in the 1st array
a[0][0] = 3
a[0][1] = 2
a[0][2] = 1
a[1][0] = 5
a[1][1] = 4
a[1][2] = 6
Enter the element in the 2nd array
a[0][0] = 7
a[0][1] = 6
a[0][2] = 4
a[1][0] = 3
a[1][1] = 2
a[1][2] = 5
Your entered list are
1st array:
3 2 1 5 4 6
2nd array:
7 6 4 3 2 5
***OPERATION MENU***
enter your choice
1.ADITION
2.SUBSTRACTION
3.MULITPLY
4.DIVISION
0.EXIT
Related exercise:
- Add Array Element Total in C++
- Passing Array to a Function in C++
- Array with function in C++
- Sorting Array Elements as Ascending and Descending Order in C++
- Searching Element in Array List in C++