two dimensional matrix array problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 51
Reputation: shamila08 is an unknown quantity at this point 
Solved Threads: 0
shamila08 shamila08 is offline Offline
Junior Poster in Training

two dimensional matrix array problem

 
0
  #1
Feb 22nd, 2009
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.
  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
  1. void matrixPermute (int n)
  2. {
  3. int i, temp;
  4. if (n ==3 )
  5. {nrows ++;
  6. print(nrows);
  7. return;
  8. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 29
Reputation: arghasen is an unknown quantity at this point 
Solved Threads: 4
arghasen arghasen is offline Offline
Light Poster

Re: two dimensional matrix array problem

 
0
  #2
Feb 22nd, 2009
can u be a bit more specific
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: two dimensional matrix array problem

 
0
  #3
Feb 22nd, 2009
What are you trying to accomplish?
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 51
Reputation: shamila08 is an unknown quantity at this point 
Solved Threads: 0
shamila08 shamila08 is offline Offline
Junior Poster in Training

Re: two dimensional matrix array problem

 
0
  #4
Feb 22nd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 51
Reputation: shamila08 is an unknown quantity at this point 
Solved Threads: 0
shamila08 shamila08 is offline Offline
Junior Poster in Training

Re: two dimensional matrix array problem

 
0
  #5
Feb 22nd, 2009
when i remove try did not use nrows such as follows:
  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
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,618
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: two dimensional matrix array problem

 
0
  #6
Feb 22nd, 2009
line 50 and 51: array numbers always begin with 0, not 1.
  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.
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 51
Reputation: shamila08 is an unknown quantity at this point 
Solved Threads: 0
shamila08 shamila08 is offline Offline
Junior Poster in Training

Re: two dimensional matrix array problem

 
0
  #7
Feb 22nd, 2009
thanks....
yup that's recursive function. the index 'temp', we can change it where i use code as follows:
  1. if (n ==2 )
  2. { nrows++;
  3. print();
  4. return;
  5. }
or we can set up
  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'?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 51
Reputation: shamila08 is an unknown quantity at this point 
Solved Threads: 0
shamila08 shamila08 is offline Offline
Junior Poster in Training

Re: two dimensional matrix array problem

 
0
  #8
Feb 22nd, 2009
ok. i start index with '0' not '1'.
  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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC