943,928 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1461
  • C++ RSS
Feb 22nd, 2009
0

two dimensional matrix array problem

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #define MAX 100
  5. int num[MAX][MAX];
  6. int n;
  7. int nrows = 0;
  8. using namespace std;
  9.  
  10. void print(int i )
  11. {
  12. if (i > 0) {
  13.  
  14. for (int j = 1; j <=n; j++)
  15. //cout << num[i][j];
  16. cout << "num["<<i<<"]["<<j<<"]=" <<num[i][j] << endl;
  17. cout <<"\n";
  18. }
  19. }
  20. void rightRotate ( int n)
  21. {int tmp;
  22. for(int k = 1; k<n; ++k)
  23. for(int l = 1; l<=n; ++l)
  24. {
  25. //cout << "l:"<< l << "\n";
  26. tmp = num[l][k];
  27. num[l][k]= num[l][k+1];
  28. num[l][k+1] = tmp;}
  29. }
  30.  
  31. void matrixPermute (int n)
  32. {
  33. int i, temp;
  34. if (n ==3 )
  35. {nrows ++;
  36. print(nrows);
  37. return;
  38. }
  39. temp = n-1 ;
  40. cout << "temp:"<< temp<<"\n";
  41. for (i = 1; i <=temp ; ++i)
  42. {
  43. //cout << "i:" << i << "\n";
  44. matrixPermute (temp);
  45. rightRotate (temp);
  46. }
  47. }
  48. void initiate(int n)
  49. {
  50. for (int i = 1; i <= n; ++i)
  51. for (int j = 1; j <= n; ++j)
  52. {
  53. num[i][j] = j;
  54. }
  55. }
  56.  
  57. int main ()
  58. {
  59. cout << "number must be between 3 and 10 \n";
  60. cin >> n;
  61. cout << endl;
  62. initiate(n);
  63. matrixPermute (n);
  64. cout << "nrows=" <<nrows << "\n";
  65. }

its cant read the following code that i set it up
C++ Syntax (Toggle Plain Text)
  1. void matrixPermute (int n)
  2. {
  3. int i, temp;
  4. if (n ==3 )
  5. {nrows ++;
  6. print(nrows);
  7. return;
  8. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shamila08 is offline Offline
51 posts
since Aug 2008
Feb 22nd, 2009
0

Re: two dimensional matrix array problem

can u be a bit more specific
Reputation Points: 10
Solved Threads: 5
Light Poster
arghasen is offline Offline
35 posts
since Nov 2008
Feb 22nd, 2009
0

Re: two dimensional matrix array problem

What are you trying to accomplish?
Can you be more descriptive?
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Feb 22nd, 2009
0

Re: two dimensional matrix array problem

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shamila08 is offline Offline
51 posts
since Aug 2008
Feb 22nd, 2009
0

Re: two dimensional matrix array problem

when i remove try did not use nrows such as follows:
C++ Syntax (Toggle Plain Text)
  1. void print()
  2. {
  3. if (num != 0) {
  4. int i=1;
  5. for (int j = 1; j <=n; j++)
  6. cout << num[i][j];
  7. // cout << "num["<<i<<"]["<<j<<"]=" <<num[i][j] << endl;
  8. cout <<"\n";
  9. }
  10. }
and
C++ Syntax (Toggle Plain Text)
  1. void matrixPermute (int n)
  2. {
  3. int i, temp;
  4. if (n ==3 )
  5. { nrows++;
  6. print();
  7. return;
  8. }
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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shamila08 is offline Offline
51 posts
since Aug 2008
Feb 22nd, 2009
0

Re: two dimensional matrix array problem

line 50 and 51: array numbers always begin with 0, not 1.
C++ Syntax (Toggle Plain Text)
  1. for(int i = 0; i < n; i++)
  2. {
  3. for(int j = 0; j < n; j++)
  4. {
  5. num[i][j] = j+1;
  6. }
  7. }


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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Feb 22nd, 2009
0

Re: two dimensional matrix array problem

thanks....
yup that's recursive function. the index 'temp', we can change it where i use code as follows:
C++ Syntax (Toggle Plain Text)
  1. if (n ==2 )
  2. { nrows++;
  3. print();
  4. return;
  5. }
or we can set up
C++ Syntax (Toggle Plain Text)
  1. if (n ==1 )
  2. { nrows++;
  3. print();
  4. return;
  5. }
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'?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shamila08 is offline Offline
51 posts
since Aug 2008
Feb 22nd, 2009
0

Re: two dimensional matrix array problem

ok. i start index with '0' not '1'.
C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #define MAX 100
  5. int num[MAX][MAX];
  6. int n;
  7. int nrows = 0;
  8. using namespace std;
  9.  
  10. void print(int i)
  11. {
  12. if (num != 0) {
  13. for (int j = 0; j <n; j++)
  14. // cout << num[0][j];
  15. cout << "num["<<i<<"]["<<j<<"]=" <<num[i][j] << endl;
  16. cout <<"\n";
  17. }
  18. }
  19. void rightRotate ( int n)
  20. {int tmp;
  21. for(int k = 0; k<n-1; ++k)
  22. for(int l = 0; l<n; ++l)
  23. {
  24. //cout << "l:"<< l << "\n";
  25. tmp = num[l][k];
  26. num[l][k]= num[l][k+1];
  27. num[l][k+1] = tmp;}
  28. }
  29.  
  30. void matrixPermute (int n)
  31. {
  32. int i, temp;
  33. if (n ==3 )
  34. { nrows++;
  35. print(nrows);
  36. return;
  37. }
  38. temp = n-1 ;
  39. cout << "temp:"<< temp<<"\n";
  40. for (i = 0; i <temp ; ++i)
  41. {
  42. //cout << "i:" << i << "\n";
  43. matrixPermute (temp);
  44. rightRotate (temp);
  45. }
  46. }
  47. void initiate(int n)
  48. {
  49. for (int i = 0; i < n; ++i)
  50. for (int j = 0; j < n; ++j)
  51. {
  52. num[i][j] = j+1;
  53. }
  54. }
  55.  
  56. int main ()
  57. {
  58. cout << "number must be between 3 and 10 \n";
  59. cin >> n;
  60. cout << endl;
  61. initiate(n);
  62. matrixPermute (n);
  63. cout << "nrows=" <<nrows << "\n";
  64. }
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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shamila08 is offline Offline
51 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: String/Integer C++ Question
Next Thread in C++ Forum Timeline: Read Binary File help...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC