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

Popular posts from this blog

Fixing 'java RMI - ConnectException: Operation timed out' in WSO2 Enterprise Integrator 6.4

If you ever come across the below exception when running WSO2 Enterprise Integrator 6.4, here is the fix. This error occurs when you have multiple IP addresses from different networks configured in your etc/hosts as below. 10.xxx.x.xxx localhost 192.xxx.x.xxx localhost So simply, removing the unnecessary one and leaving the one of the network that you are currently connected to should resolve this issue. 10.xxx.x.xxx localhost

Student Information System - Java (SLIIT - ST2 PROJECT)

Student Information System (Github Project) This system is developed in Java and mySQL as a group project by me and 3 other members during a period of 1 month. The system allows the administrator to,  enroll students to the system  update enroll information  add/update course and degree program details  generate reports  create exams and edit relevant information  calculate gpa of the relevant exam  assign lecturers to courses  add lecturers/update details Lecturers to,  assign course grades  view their feedback  generate reports  view student / course / degree program details Students to,  view their profile  view their grading information  give feedback to lecturers   view lecturer / course / degree program details and other features. Below are some interfaces of the project. (Splash Screen) (Login) (Admin View) (Student Re...

SIMPLE BLACKJACK GAME IN JAVA (CONSOLE)

import java.util.Scanner; class BlackJack{     public static void main(String[] args)      {         int player_random1 = 100;         int player_random2 = 100;         while(player_random1 >= 12 || player_random2 >= 12  || player_random1 < 3 || player_random2 <3)         {             player_random1 = (int)(Math.random()*100);             player_random2 = (int)(Math.random()*100);         }                  int player_total = player_random1 + player_random2;                  System.out.println("You get a "+player_random1+" and a "+player_random2);         System.out.println("Your total is "+player_total); if(player_total==21)  ...