| | |
two dimensional matrix array problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2008
Posts: 51
Reputation:
Solved Threads: 0
hello, dear all. below is my program code. the problem is an half of my output is zero for entries of matrix for n greater than 4.
its cant read the following code that i set it up
C++ Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <iostream> #define MAX 100 int num[MAX][MAX]; int n; int nrows = 0; using namespace std; void print(int i ) { if (i > 0) { for (int j = 1; j <=n; j++) //cout << num[i][j]; cout << "num["<<i<<"]["<<j<<"]=" <<num[i][j] << endl; cout <<"\n"; } } void rightRotate ( int n) {int tmp; for(int k = 1; k<n; ++k) for(int l = 1; l<=n; ++l) { //cout << "l:"<< l << "\n"; tmp = num[l][k]; num[l][k]= num[l][k+1]; num[l][k+1] = tmp;} } void matrixPermute (int n) { int i, temp; if (n ==3 ) {nrows ++; print(nrows); return; } temp = n-1 ; cout << "temp:"<< temp<<"\n"; for (i = 1; i <=temp ; ++i) { //cout << "i:" << i << "\n"; matrixPermute (temp); rightRotate (temp); } } void initiate(int n) { for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { num[i][j] = j; } } int main () { cout << "number must be between 3 and 10 \n"; cin >> n; cout << endl; initiate(n); matrixPermute (n); cout << "nrows=" <<nrows << "\n"; }
its cant read the following code that i set it up
C++ Syntax (Toggle Plain Text)
void matrixPermute (int n) { int i, temp; if (n ==3 ) {nrows ++; print(nrows); return; }
What are you trying to accomplish?
Can you be more descriptive?
Can you be more descriptive?
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
•
•
Join Date: Aug 2008
Posts: 51
Reputation:
Solved Threads: 0
let me show the output
number must be between 3 and 10:
5
....
......
num[5][1]=1
num[5][2]=2
num[5][3]=3
num[5][4]=4
num[5][5]=5
num[6][1]=0
num[6][2]=0
num[6][3]=0
num[6][4]=0
num[6][5]=0
temp:3
num[7][1]=0
num[7][2]=0
num[7][3]=0
num[7][4]=0
num[7][5]=0
num[8][1]=0
num[8][2]=0
num[8][3]=0
num[8][4]=0
num[8][5]=0
num[9][1]=0
num[9][2]=0
num[9][3]=0
num[9][4]=0
num[9][5]=0
temp:3
num[10][1]=0
num[10][2]=0
num[10][3]=0
num[10][4]=0
num[10][5]=0
num[11][1]=0
num[11][2]=0
num[11][3]=0
num[11][4]=0
num[11][5]=0
num[12][1]=0
num[12][2]=0
num[12][3]=0
num[12][4]=0
num[12][5]=0
nrows=12
Press any key to continue . . .
the problem is when nrows = 6 until 12, the related elements is zero. something is not corresponds to the nrows.
number must be between 3 and 10:
5
....
......
num[5][1]=1
num[5][2]=2
num[5][3]=3
num[5][4]=4
num[5][5]=5
num[6][1]=0
num[6][2]=0
num[6][3]=0
num[6][4]=0
num[6][5]=0
temp:3
num[7][1]=0
num[7][2]=0
num[7][3]=0
num[7][4]=0
num[7][5]=0
num[8][1]=0
num[8][2]=0
num[8][3]=0
num[8][4]=0
num[8][5]=0
num[9][1]=0
num[9][2]=0
num[9][3]=0
num[9][4]=0
num[9][5]=0
temp:3
num[10][1]=0
num[10][2]=0
num[10][3]=0
num[10][4]=0
num[10][5]=0
num[11][1]=0
num[11][2]=0
num[11][3]=0
num[11][4]=0
num[11][5]=0
num[12][1]=0
num[12][2]=0
num[12][3]=0
num[12][4]=0
num[12][5]=0
nrows=12
Press any key to continue . . .
the problem is when nrows = 6 until 12, the related elements is zero. something is not corresponds to the nrows.
•
•
Join Date: Aug 2008
Posts: 51
Reputation:
Solved Threads: 0
when i remove try did not use nrows such as follows:
and
this is the output
number must be between 3 and 10
5
temp:4
temp:3
12345
23145
31245
temp:3
23415
34215
42315
temp:3
34125
41325
13425
temp:3
41235
12435
24135
nrows=12
Press any key to continue . . .
however i need to represent the output in matrix form. i couldnt make it. i need the index 'i' will follows nrows index.
C++ Syntax (Toggle Plain Text)
void print() { if (num != 0) { int i=1; for (int j = 1; j <=n; j++) cout << num[i][j]; // cout << "num["<<i<<"]["<<j<<"]=" <<num[i][j] << endl; cout <<"\n"; } }
C++ Syntax (Toggle Plain Text)
void matrixPermute (int n) { int i, temp; if (n ==3 ) { nrows++; print(); return; }
number must be between 3 and 10
5
temp:4
temp:3
12345
23145
31245
temp:3
23415
34215
42315
temp:3
34125
41325
13425
temp:3
41235
12435
24135
nrows=12
Press any key to continue . . .
however i need to represent the output in matrix form. i couldnt make it. i need the index 'i' will follows nrows index.
line 50 and 51: array numbers always begin with 0, not 1.
line 44: >>matrixPermute (temp);
This is a recursive function call, yet you always are passing the same value of temp which never changes in that loop. It looks like that function will be an infinite recursion, which will eventually crash and burn the program.
C++ Syntax (Toggle Plain Text)
for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { num[i][j] = j+1; } }
line 44: >>matrixPermute (temp);
This is a recursive function call, yet you always are passing the same value of temp which never changes in that loop. It looks like that function will be an infinite recursion, which will eventually crash and burn the program.
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: Aug 2008
Posts: 51
Reputation:
Solved Threads: 0
thanks....
yup that's recursive function. the index 'temp', we can change it where i use code as follows:
or we can set up
regarding to my last previous post is i have to represent the output in matrix form correspond to nrows index.
when n = 4, there is no problem. however problem arises when n>4..
is it possible or i have to rewrite the program without 'temp'?
yup that's recursive function. the index 'temp', we can change it where i use code as follows:
C++ Syntax (Toggle Plain Text)
if (n ==2 ) { nrows++; print(); return; }
C++ Syntax (Toggle Plain Text)
if (n ==1 ) { nrows++; print(); return; }
when n = 4, there is no problem. however problem arises when n>4..
is it possible or i have to rewrite the program without 'temp'?
•
•
Join Date: Aug 2008
Posts: 51
Reputation:
Solved Threads: 0
ok. i start index with '0' not '1'.
its also obtained the same output. i understand because it related to 'temp'.
the meaning of the temp is follows:
n = 4
when temp =3,
it will right rotate the first three element such as follows:
1234
2314
3124..........
for n = 5:
when temp 4, it will rotate the first 4 element:
12345 ( the list is not appear).
23415
34125
41235
the continue temp = 3: ( that's the last output)
12345
23145
31245
12345
C++ Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <iostream> #define MAX 100 int num[MAX][MAX]; int n; int nrows = 0; using namespace std; void print(int i) { if (num != 0) { for (int j = 0; j <n; j++) // cout << num[0][j]; cout << "num["<<i<<"]["<<j<<"]=" <<num[i][j] << endl; cout <<"\n"; } } void rightRotate ( int n) {int tmp; for(int k = 0; k<n-1; ++k) for(int l = 0; l<n; ++l) { //cout << "l:"<< l << "\n"; tmp = num[l][k]; num[l][k]= num[l][k+1]; num[l][k+1] = tmp;} } void matrixPermute (int n) { int i, temp; if (n ==3 ) { nrows++; print(nrows); return; } temp = n-1 ; cout << "temp:"<< temp<<"\n"; for (i = 0; i <temp ; ++i) { //cout << "i:" << i << "\n"; matrixPermute (temp); rightRotate (temp); } } void initiate(int n) { for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) { num[i][j] = j+1; } } int main () { cout << "number must be between 3 and 10 \n"; cin >> n; cout << endl; initiate(n); matrixPermute (n); cout << "nrows=" <<nrows << "\n"; }
the meaning of the temp is follows:
n = 4
when temp =3,
it will right rotate the first three element such as follows:
1234
2314
3124..........
for n = 5:
when temp 4, it will rotate the first 4 element:
12345 ( the list is not appear).
23415
34125
41235
the continue temp = 3: ( that's the last output)
12345
23145
31245
12345
![]() |
Similar Threads
- return 2d array to caller from public class function (C++)
- problem in constructing two dimensional array in c++ using classes (C++)
- reading in from vector to a two dimensional array (C)
- Array Distances (Python)
- Please helpwith how to return two dimensional array (C++)
- A Challenging Debugging Problem (C)
- Conditionally initializing arrays HELP (C++)
- Reading CSV in VC++ (C++)
- Sorting 2D Dynamic array of integers , Snake Style (C)
Other Threads in the C++ Forum
- Previous Thread: String/Integer C++ Question
- Next Thread: Read Binary File help...
| Thread Tools | Search this Thread |
Tag cloud for C++
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 homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






