problem with my c++ algorithm assginment

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

problem with my c++ algorithm assginment

 
0
  #1
Aug 29th, 2008
hello, dear all, i have a slightly problem with my assigment algorithm as follows:
  1. void write( int *x, int n)
  2. {
  3. if (x != 0) {
  4. for (int i = 0; i < n; i++) {
  5. printf("%4d", x[i] );
  6. }
  7. printf("\n");
  8. }
  9. } // print
  10.  
  11.  
  12. void swap(int *x, int i, int j)
  13. {
  14. int t;
  15. t = x[i];
  16. x[i] = x[j];
  17. x[j] = t;
  18. }
  19.  
  20.  
  21. void reverse(int *x, int start, int n)
  22. {
  23. int tmp = x[start];
  24. for (int i = start; i <n-1; ++i) {
  25. x[i] = x[n-i-1];
  26. x[n-i-1] = tmp;
  27. }
  28. }
  29.  
  30.  
  31. void starterequiv(int *x, int start, int n)
  32. {
  33. write(x, n);
  34. if (start < n) {
  35. int i, j;
  36. for (i = n-1; i > start; i--) {
  37. for (j = i + 1; j < n; j++) {
  38. swap(x, i, j);
  39. starterequiv(x, i, n);
  40. }
  41. reverse(x, i, n);
  42. }
  43. }
  44. }
  45.  
  46.  
  47. void initiate(int *x, int n)
  48. {
  49. for (int i = 0; i <n; i++) {
  50. x[i] = i+1;
  51. }
  52. } // init
  53.  
  54.  
  55. void main()
  56. {
  57. char buf[100];
  58. int n;
  59. printf("Enter the number of elements: ");
  60. fgets(buf, sizeof(buf), stdin );
  61. sscanf_s(buf, "%d", &n);
  62.  
  63. if (n > 0 && n <= 100) {
  64. int *x = new int[n];
  65. initiate(x, n);
  66. starterequiv(x, 0, n);
  67. }
  68. }

i need an output as follows:
1234
1432
1423
1324
1342
1243
. but i didnt get it. how to fix it?

many thanks.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 61
Reputation: AceofSpades19 is on a distinguished road 
Solved Threads: 10
AceofSpades19's Avatar
AceofSpades19 AceofSpades19 is offline Offline
Junior Poster in Training

Re: problem with my c++ algorithm assginment

 
0
  #2
Aug 29th, 2008
don't use void main(), use int main(). void main() doesn't compile on most compilers and is incorrect according to the standard
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: problem with my c++ algorithm assginment

 
0
  #3
Aug 29th, 2008
What output do YOU get?? At first glance the reverse function is looking odd. Are you sure that this function is working fine? And why don't you use std::vector and std::next_permutation if you are coding in C++???
Last edited by jencas; Aug 29th, 2008 at 4:14 am.
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
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: problem with my c++ algorithm assginment

 
0
  #4
Sep 1st, 2008
Originally Posted by jencas View Post
What output do YOU get?? At first glance the reverse function is looking odd. Are you sure that this function is working fine? And why don't you use std::vector and std::next_permutation if you are coding in C++???
.

the reverse function is used for reverse the array such as follows
1234 then 4321. i already test it. it's find.

i didn't use next_permutation because i have to build my own algorithm as my assignment.
do you any suggestion in spite of use next_permutation?
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: problem with my c++ algorithm assginment

 
0
  #5
Sep 1st, 2008
Originally Posted by jencas View Post
What output do YOU get?? At first glance the reverse function is looking odd. Are you sure that this function is working fine? And why don't you use std::vector and std::next_permutation if you are coding in C++???
.
Below its the output:
Enter the number of elements: 4
   1   2   3   4
   1   2   4   3
   1   2   4   3
   1   2   3   4
   1   4   2   3
   1   4   3   2
Press any key to continue . . .
The problem start after 1234, suppose it will be 1432 ( reverse except '1')
then its suppose to swap 1423, then reverse 1324 ( reverse except '1')
then its suppose to swap 1342, then its reverse 1243.
Last edited by cscgal; Sep 3rd, 2008 at 4:17 pm. Reason: Fixed broken code tag
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: problem with my c++ algorithm assginment

 
0
  #6
Sep 2nd, 2008
The loop in reverse( ) never stores a new value of temp after the initial value. How can this reverse the full array (or subset)?

And, this is C code, not C++. Please use the correct forum.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
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: problem with my c++ algorithm assginment

 
0
  #7
Sep 2nd, 2008
Originally Posted by vmanes View Post
The loop in reverse( ) never stores a new value of temp after the initial value. How can this reverse the full array (or subset)?

And, this is C code, not C++. Please use the correct forum.
thanks for the comment. ok i change from reverse function to recycle function. but the idea is still same. here my algorithm
  1. #include <stdio.h>
  2.  
  3. /**
  4.   Read a number, n, from standard input and print the
  5.   permutations.
  6.  */
  7.  
  8. void write( int *x, int n)
  9. {
  10. if (x != 0) {
  11. for (int i = 0; i < n; i++) {
  12. printf("%4d", x[i] );
  13. }
  14. printf("\n");
  15. }
  16. } // print
  17.  
  18.  
  19. void swap(int *x, int i, int j)
  20. {
  21. int t;
  22. t = x[i];
  23. x[i] = x[j];
  24. x[j] = t;
  25. }
  26.  
  27.  
  28. void recycle(int *x, int start, int n)
  29. {
  30. int tmp = x[start];
  31. for (int i = start; i <n-1; ++i) {
  32. x[i] = x[n-i-1];
  33. x[n-i-1] = tmp;
  34. }
  35. }
  36.  
  37.  
  38. void starterequiv(int *x, int start, int n)
  39. {
  40. write(x, n);
  41. if (start < n) {
  42. int i, j;
  43. for (i = n-1; i > start; i--) {
  44. for (j = i + 1; j < n; j++) {
  45. swap(x, i, j);
  46. starterequiv(x, i, n);
  47. }
  48. recycle(x, i, n);
  49. }
  50. }
  51. }
  52.  
  53.  
  54. void initiate(int *x, int n)
  55. {
  56. for (int i = 0; i <n; i++) {
  57. x[i] = i+1;
  58. }
  59. } // init
  60.  
  61.  
  62. void main()
  63. {
  64. char buf[100];
  65. int n;
  66. printf("Enter the number of elements: ");
  67. fgets(buf, sizeof(buf), stdin );
  68. sscanf_s(buf, "%d", &n);
  69.  
  70. if (n > 0 && n <= 100) {
  71. int *x = new int[n];
  72. initiate(x, n);
  73. starterequiv(x, 0, n);
  74. }
  75. }
my output as follows:
1234
1243
1423
1432
1234
1243

which is not the output i need. i know the problem is in loop of starterequiv function which is related to integer i and j. actually in my real intention that , i need to recycle first then swap.
Last edited by cscgal; Sep 2nd, 2008 at 11:22 pm. Reason: Fixed code tags
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: problem with my c++ algorithm assginment

 
0
  #8
Sep 2nd, 2008
Originally Posted by vmanes View Post
The loop in reverse( ) never stores a new value of temp after the initial value. How can this reverse the full array (or subset)?

And, this is C code, not C++. Please use the correct forum.
sorry. here my algorithm
  1. #include <stdio.h>
  2.  
  3. /**
  4.   Read a number, n, from standard input and print the
  5.   permutations.
  6.  */
  7.  
  8. void write( int *x, int n)
  9. {
  10. if (x != 0) {
  11. for (int i = 0; i < n; i++) {
  12. printf("%4d", x[i] );
  13. }
  14. printf("\n");
  15. }
  16. } // print
  17.  
  18.  
  19. void swap(int *x, int i, int j)
  20. {
  21. int t;
  22. t = x[i];
  23. x[i] = x[j];
  24. x[j] = t;
  25. }
  26.  
  27.  
  28. void recycle(int *x, int start, int n)
  29. {
  30. int tmp = x[start+1];
  31. for (int i = start+1; i <n-2; ++i) {
  32. x[i] = x[n-i-1];
  33. x[n-i-1] = tmp;
  34. }
  35. }
  36.  
  37.  
  38. void starterequiv(int *x, int start, int n)
  39. {
  40. write(x, n);
  41. if (start < n) {
  42. int i, j;
  43. for (i = n-1; i > start; i--) {
  44. for (j = i + 1; j < n; j++) {
  45. swap(x, i, j);
  46. starterequiv(x, i, n);
  47. }
  48. recycle(x, i, n);
  49. }
  50. }
  51. }
  52.  
  53.  
  54. void initiate(int *x, int n)
  55. {
  56. for (int i = 0; i <n; i++) {
  57. x[i] = i+1;
  58. }
  59. } // init
  60.  
  61.  
  62. void main()
  63. {
  64. char buf[100];
  65. int n;
  66. printf("Enter the number of elements: ");
  67. fgets(buf, sizeof(buf), stdin );
  68. sscanf_s(buf, "%d", &n);
  69.  
  70. if (n > 0 && n <= 100) {
  71. int *x = new int[n];
  72. initiate(x, n);
  73. starterequiv(x, 0, n);
  74. }
  75. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 61
Reputation: AceofSpades19 is on a distinguished road 
Solved Threads: 10
AceofSpades19's Avatar
AceofSpades19 AceofSpades19 is offline Offline
Junior Poster in Training

Re: problem with my c++ algorithm assginment

 
1
  #9
Sep 2nd, 2008
Don't use void main(), use int main() as I said in my previous post
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: problem with my c++ algorithm assginment

 
0
  #10
Sep 2nd, 2008
Originally Posted by AceofSpades19 View Post
Don't use void main(), use int main() as I said in my previous post
yup. i just change it. but the output is not change. i think there is a problem in starterequive function. the way i write the algorithm as below is wrong.:
  1. void starterequiv(int *x, int start, int n)
  2. {
  3. write(x, n);
  4. if (start < n) {
  5. int i, j;
  6. for (i = n-1; i > start; i--) {
  7. for (j = i + 1; j < n; j++) {
  8. swap(x, i, j);
  9. starterequiv(x, i, n);
  10. }
  11. reverse(x, i, n);
  12. }
  13. }
  14. }
i need to fix it but dont know. i need your all opinion.
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC