Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Converting 2 dimemsional array to 3 dimensional array and vice versa in c++ (http://www.go4expert.com/articles/converting-2-dimemsional-array-3-t9341/)

bashamsc 14Mar2008 12:19

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

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: Cpp

#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;
    }

}


shabbir 4Apr2008 18:44

Re: Converting 2 dimemsional array to 3 dimensional array and vice versa in c++
 
Nomination for article of the month of March

shabbir 17Apr2008 15:52

Re: Converting 2 dimemsional array to 3 dimensional array and vice versa in c++
 
Vote here - Voting for article of the month for Mar 2008


All times are GMT +5.5. The time now is 18:21.