954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ Multi dimensional arrays

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;
}
Linda1
Newbie Poster
17 posts since Oct 2007
Reputation Points: 4
Solved Threads: 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};
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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.

Linda1
Newbie Poster
17 posts since Oct 2007
Reputation Points: 4
Solved Threads: 0
 

>>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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I managed to get it!! Anyway, here's the code just in case someone comes along with the same question.
#include
#include
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;



}

Linda1
Newbie Poster
17 posts since Oct 2007
Reputation Points: 4
Solved Threads: 0
 

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

Linda1
Newbie Poster
17 posts since Oct 2007
Reputation Points: 4
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You