943,908 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2179
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 29th, 2008
0

problem with my c++ algorithm assginment

Expand Post »
hello, dear all, i have a slightly problem with my assigment algorithm as follows:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shamila08 is offline Offline
51 posts
since Aug 2008
Aug 29th, 2008
0

Re: problem with my c++ algorithm assginment

don't use void main(), use int main(). void main() doesn't compile on most compilers and is incorrect according to the standard
Reputation Points: 61
Solved Threads: 10
Junior Poster in Training
AceofSpades19 is offline Offline
61 posts
since Jun 2008
Aug 29th, 2008
0

Re: problem with my c++ algorithm assginment

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.
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
Sep 1st, 2008
0

Re: problem with my c++ algorithm assginment

Click to Expand / Collapse  Quote originally posted by jencas ...
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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shamila08 is offline Offline
51 posts
since Aug 2008
Sep 1st, 2008
0

Re: problem with my c++ algorithm assginment

Click to Expand / Collapse  Quote originally posted by jencas ...
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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shamila08 is offline Offline
51 posts
since Aug 2008
Sep 2nd, 2008
0

Re: problem with my c++ algorithm assginment

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.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Sep 2nd, 2008
0

Re: problem with my c++ algorithm assginment

Click to Expand / Collapse  Quote originally posted by vmanes ...
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
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shamila08 is offline Offline
51 posts
since Aug 2008
Sep 2nd, 2008
0

Re: problem with my c++ algorithm assginment

Click to Expand / Collapse  Quote originally posted by vmanes ...
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
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shamila08 is offline Offline
51 posts
since Aug 2008
Sep 2nd, 2008
1

Re: problem with my c++ algorithm assginment

Don't use void main(), use int main() as I said in my previous post
Reputation Points: 61
Solved Threads: 10
Junior Poster in Training
AceofSpades19 is offline Offline
61 posts
since Jun 2008
Sep 2nd, 2008
0

Re: problem with my c++ algorithm assginment

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.:
C++ Syntax (Toggle Plain Text)
  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.
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: Infinite Loop
Next Thread in C++ Forum Timeline: please solve my problem





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


Follow us on Twitter


© 2011 DaniWeb® LLC