| | |
C++ Multi dimensional arrays
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2007
Posts: 17
Reputation:
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.
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
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
int matrix[NO_OF_ROWS][NO_OF_COLUMNS] = {0};
Last edited by Ancient Dragon; Oct 12th, 2007 at 11:59 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Oct 2007
Posts: 17
Reputation:
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;
}
![]() |
Similar Threads
- (reformatted) How to return Multi-Dimensional Arrays (C++)
- Addition with Two 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++
Views: 7506 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






