I am very new to C++ and one of my homework assignments involvemulti dimensional arrays.

I was able to initialize and input and display the array (4x4) but the last step is to swap the data of the row to that of the column. Tried different ways and unable to figure out the code. Can you help?

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    const int NO_OF_ROWS = 4;
    const int NO_OF_COLUMNS =4;
    int matrix[NO_OF_ROWS][NO_OF_COLUMNS];
int copyArray(int row,int col,int arraysize);
     
    int row;
    int col;

    //initialize 
    
    for (col = 0; col < NO_OF_COLUMNS; col++)
         matrix[NO_OF_ROWS][NO_OF_COLUMNS] = 0;
    for (row = 0; row < NO_OF_ROWS; row++)
         matrix[NO_OF_ROWS][NO_OF_COLUMNS] = 0;
         
    // print
    
      //Input
    cout << "Enter the 16 integers for data. " << endl;
    for (col = 0; col < NO_OF_COLUMNS; col++)
        
    for (row = 0; row < NO_OF_ROWS; row++)
       
        cin >> matrix[row][col];
        

        //print
        for (row = 0; row < NO_OF_ROWS; row++)
        {
            for (col = 0; col < NO_OF_COLUMNS; col++)
                cout << setw(5) << matrix[row][col] << " ";
                cout << endl;
        }

// swap contents of row to the column

    for(row = 0; row < NO_OF_ROWS-1; row--)
    

    
 
  // print
 int copyArray(int row,int col,int arraysize);
     
        

    
        {
            for (col = 0; col < NO_OF_COLUMNS; col++)
                cout << setw(5) << matrix[row][col] << " ";
                cout << endl;
        }
        //interchange rows and columns
    
    return 0;
}

The code in lines 16-20 is all wrong. you need nested loops to do that, and array indices are the values of the loop counters, like this:

array[row][col] = 0;

Or even easier -- you don't have to do the above at all if you initialize the array when it is declared

int matrix[NO_OF_ROWS][NO_OF_COLUMNS] = {0};

The compiler builds it and the output is ok. My problem is swapping the data from the column to the rows and displaying the new output. I've been working on this and still could not get it right.

>>The compiler builds it and the output is ok
Yes the compiler will build it, but its not ok. What you have is called buffer overrun. Look at what you posted and THINK about it for a couple minutes. Why do I say buffer overrun -- because array element at indices matrix[NO_OF_ROWS][NO_OF_COLUMNS] does not exist. Your program is scribbling all over memory outside he boundries of the array.

I managed to get it!! Anyway, here's the code just in case someone comes along with the same question.

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    const int NO_OF_ROWS = 4;
    const int NO_OF_COLUMNS =4;
    int matrix[NO_OF_ROWS][NO_OF_COLUMNS];
int copyArray(int row,int col,int arraysize);
const int END_OF_ROW = 4;
const int END_OF_COL =4;
    int row;
    int col;

    //initialize 

    for (col = 0; col < NO_OF_COLUMNS; col++)
         matrix[NO_OF_ROWS][NO_OF_COLUMNS] = 0;
    for (row = 0; row < NO_OF_ROWS; row++)
         matrix[NO_OF_ROWS][NO_OF_COLUMNS] = 0;

    // print

      //Input
    cout << "Enter the 16 integers for data. " << endl;
    for (col = 0; col < NO_OF_COLUMNS; col++)

    for (row = 0; row < NO_OF_ROWS; row++)

        cin >> matrix[row][col];


        //print
        for (row = 0; row < NO_OF_ROWS; row++)
        {
            for (col = 0; col < NO_OF_COLUMNS; col++)
                cout << setw(5) << matrix[row][col] << " ";
                cout << endl;
        }
    for (col = 0; col < NO_OF_COLUMNS  ; col++)
    {
      for (row = 0; row < NO_OF_ROWS ; row++)

    ;}
    cout << endl;

      for (row = 0; row < NO_OF_ROWS; row++)
        {
            for (col = 0; col < NO_OF_COLUMNS; col++)
                cout << setw(5) << matrix[col][row] << " ";
                cout << endl;
    }



  // print

return 0;





    }

Thanks for your help Ancient Dragon. I managed to solve it myself though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.