Skip to main content
Rotating the contents of the columns in a 2D array in C++

  


The values are inputted to the 1st column of the 2D array. As shown in the second image, the content is rotated and displayed.

The C++ program for this is as below.

#include <iostream>
void displayArray(int arr[][3],int row,int col);
using namespace std;
int main ()
{
        int data[4][3];
        cout<<"Please input the number"<<endl;

        for(int i=0;i<4;i++)
        {
               cin>>data[i][0];//Values are inserted to the 1st column of the array
        }
        for(int j=0;j<4;j++)
        {
            for(int k=1;k<3;k++)
            {
                   data[j][k]=0;
            }
               }
        
        cout<<endl;
        displayArray(data,4,3);

        return 0;
}

void displayArray(int arr[][3],int row,int col)
{
        int data=0;

        for(int j=0;j<row;j++)
        {
                data=arr[j][0];//Data in the 1st column is temporarily copied to a variable
                arr[j][0]=arr[j][2];
                arr[j][2]=arr[j][1];
                arr[j][1]=data;

                cout<<arr[j][0]<<"\t"<<arr[j][1]<<"\t"<<arr[j][2]<<endl;
        }

}

Comments

  1. ගොඩාක් හොද වැඩක්, සුභ පතනවා දිගටම කරන්න..

    ReplyDelete

Post a Comment