1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Converting 2 dimemsional array to 3 dimensional array and vice versa in c++

Discussion in 'C++' started by bashamsc, Mar 14, 2008.

  1. bashamsc

    bashamsc New Member

    Introduction



    This article discusses about the Two Dimensional To Three Dimensional conversion and vice versa in c++

    The following program converts two dimensional array to three dimensional array using TwoDimToThree() and converts three dimensional array to two dimensional array ussing ThreeDimToTwo() .


    Code:
    #include<iostream>
    
    using namespace std;
    
    void TwoDimToThree();
    
    void ThreeDimToTwo();
    
    main()
    {
    
    	cout<<"If u want to convert 2d array to 3d array enter 1 "<<endl;
    
    	cout<<endl<<"If u want to convert 3d array to 2d array enter 2 "<<endl;
    
    	int n;
    
    	cin>>n;
    
    	if(n==1)
    		TwoDimToThree();
    
    	else
    		ThreeDimToTwo();
    
    }
    void TwoDimToThree()
    {
    
    	cout<<"Enter the no. of rows and column for 2d array "<<endl;
    
    	int Row,Col;
    
    	cin>>Row>>Col;
    
    	cout<<"Enter "<<Row*Col<<" elements for the 2d array"<<endl;
    
    	int i,j,k,a[Row][Col],b[Row][Row][Col];
    
    	for(i=0;i<Row;i++)
    		for(j=0;j<Col;j++)
    			cin>>a[i][j];
    
    	cout<<"The elements of 2d array are "<<endl;
    	for(i=0;i<Row;i++)
    		for(j=0;j<Col;j++)
    			cout<<"a["<<i<<"]["<<j<<"] = "<<a[i][j]<<endl;
    
    	cout<<"The elements of 3d array are "<<endl;
    	for(i=0;i<Row;i++)
    	{
    		for(j=0;j<Row;j++)
    			for(j=0;j<Row;j++)
    			{
    				for(k=0;k<Col;k++)
    				{
    					b[i][j][k]=a[j][k];
    					cout<<"b["<<i<<"]["<<j<<"]["<<k<<"] = "<<b[i][j][k]<<endl;
    				}
    			}
    	}
    
    }
    
    void ThreeDimToTwo()
    {
    
    	cout<<"Enter the no. of rows , column , length of 3d array "<<endl;
    
    	int Row,Col,Len;
    
    	cin>>Row>>Col>>Len;
    
    	cout<<"Enter "<<(Row*Col)*Len<<" elements"<<endl;
    
    	int a[Row][Col][Len],b[Row][Row*Col];
    
    	int i,j,k;
    	for(i=0;i<Row;i++)
    		for(j=0;j<Col;j++)
    			for(k=0;k<Len;k++)
    				cin>>a[i][j][k];
    
    	int l=0,m=0,p;
    
    	cout<<"Elements of the three dimensional array are "<<endl;
    	for(i=0;i<Row;i++)
    	{
    		for(j=0;j<Col;j++)
    		{
    			for(k=0;k<Len;k++)
    			{
    				b[l][m]=a[i][j][k];
    				cout<<"a["<<i<<"]["<<j<<"]["<<k<<"] = "<<a[i][j][k]<<endl;
    				m++;
    				p=m;
    			}
    		}
    		m=0,l++;
    	}
    
    	cout<<endl<<"Elements of two dimensional array are "<<endl;
    
    	for(i=0;i<l;i++)
    	{
    		for(j=0;j<p;j++)
    			cout<<"b["<<i<<"]["<<j<<"] = "<<b[i][j]<<endl;
    	}
    
    }
    
     
  2. shabbir

    shabbir Administrator Staff Member

  3. shabbir

    shabbir Administrator Staff Member

Share This Page