•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,545 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,363 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 3238 | Replies: 5 | Solved
![]() |
•
•
Join Date: Oct 2007
Posts: 16
Reputation:
Rep Power: 2
Solved Threads: 0
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?
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?
CPP Syntax (Toggle Plain Text)
#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; //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]; 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--) 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; }
Last edited by WolfPack : Oct 12th, 2007 at 11:41 pm. Reason: Add Code Tags next time you post.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
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:
Or even easier -- you don't have to do the above at all if you initialize the array when it is declared
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}; Last edited by Ancient Dragon : Oct 12th, 2007 at 11:59 pm.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
>>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.
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.
•
•
Join Date: Oct 2007
Posts: 16
Reputation:
Rep Power: 2
Solved Threads: 0
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;
}
#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;
//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];
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;
}
return 0;
}
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Addition with Two Dimensional Arrays (C++)
- (reformatted) How to return Multi-Dimensional Arrays (C++)
- Creating a multi-dimensional Session variable (PHP)
- Multi-dimensional Arrays: (Python)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- How to implement multi-dimensional arrays in Python?? (Python)
- Arrays (PHP)
- Need help passing a multi-dimensional array (C++)
Other Threads in the C++ Forum
- Previous Thread: How to display Greek (non -ASCII) in C++ Console Apps
- Next Thread: Access violation error in borland c++



Linear Mode