| | |
problem with my c++ algorithm assginment
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2008
Posts: 51
Reputation:
Solved Threads: 0
hello, dear all, i have a slightly problem with my assigment algorithm as follows:
i need an output as follows:
1234
1432
1423
1324
1342
1243
. but i didnt get it. how to fix it?
many thanks.
C++ Syntax (Toggle Plain Text)
void write( int *x, int n) { if (x != 0) { for (int i = 0; i < n; i++) { printf("%4d", x[i] ); } printf("\n"); } void swap(int *x, int i, int j) { int t; t = x[i]; x[i] = x[j]; x[j] = t; } void reverse(int *x, int start, int n) { int tmp = x[start]; for (int i = start; i <n-1; ++i) { x[i] = x[n-i-1]; x[n-i-1] = tmp; } } void starterequiv(int *x, int start, int n) { write(x, n); if (start < n) { int i, j; for (i = n-1; i > start; i--) { for (j = i + 1; j < n; j++) { swap(x, i, j); starterequiv(x, i, n); } reverse(x, i, n); } } } void initiate(int *x, int n) { for (int i = 0; i <n; i++) { x[i] = i+1; } } // init void main() { char buf[100]; int n; printf("Enter the number of elements: "); fgets(buf, sizeof(buf), stdin ); sscanf_s(buf, "%d", &n); if (n > 0 && n <= 100) { int *x = new int[n]; initiate(x, n); starterequiv(x, 0, n); } }
i need an output as follows:
1234
1432
1423
1324
1342
1243
. but i didnt get it. how to fix it?
many thanks.
•
•
Join Date: Dec 2007
Posts: 360
Reputation:
Solved Threads: 69
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
Please use code tags - Please mark solved threads as solved
•
•
Join Date: Aug 2008
Posts: 51
Reputation:
Solved Threads: 0
•
•
•
•
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?
•
•
Join Date: Aug 2008
Posts: 51
Reputation:
Solved Threads: 0
•
•
•
•
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 . . .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
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.
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Aug 2008
Posts: 51
Reputation:
Solved Threads: 0
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
#include <stdio.h> /** Read a number, n, from standard input and print the permutations. */ void write( int *x, int n) { if (x != 0) { for (int i = 0; i < n; i++) { printf("%4d", x[i] ); } printf("\n"); } void swap(int *x, int i, int j) { int t; t = x[i]; x[i] = x[j]; x[j] = t; } void recycle(int *x, int start, int n) { int tmp = x[start]; for (int i = start; i <n-1; ++i) { x[i] = x[n-i-1]; x[n-i-1] = tmp; } } void starterequiv(int *x, int start, int n) { write(x, n); if (start < n) { int i, j; for (i = n-1; i > start; i--) { for (j = i + 1; j < n; j++) { swap(x, i, j); starterequiv(x, i, n); } recycle(x, i, n); } } } void initiate(int *x, int n) { for (int i = 0; i <n; i++) { x[i] = i+1; } } // init void main() { char buf[100]; int n; printf("Enter the number of elements: "); fgets(buf, sizeof(buf), stdin ); sscanf_s(buf, "%d", &n); if (n > 0 && n <= 100) { int *x = new int[n]; initiate(x, n); starterequiv(x, 0, n); } }
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
•
•
Join Date: Aug 2008
Posts: 51
Reputation:
Solved Threads: 0
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
#include <stdio.h> /** Read a number, n, from standard input and print the permutations. */ void write( int *x, int n) { if (x != 0) { for (int i = 0; i < n; i++) { printf("%4d", x[i] ); } printf("\n"); } void swap(int *x, int i, int j) { int t; t = x[i]; x[i] = x[j]; x[j] = t; } void recycle(int *x, int start, int n) { int tmp = x[start+1]; for (int i = start+1; i <n-2; ++i) { x[i] = x[n-i-1]; x[n-i-1] = tmp; } } void starterequiv(int *x, int start, int n) { write(x, n); if (start < n) { int i, j; for (i = n-1; i > start; i--) { for (j = i + 1; j < n; j++) { swap(x, i, j); starterequiv(x, i, n); } recycle(x, i, n); } } } void initiate(int *x, int n) { for (int i = 0; i <n; i++) { x[i] = i+1; } } // init void main() { char buf[100]; int n; printf("Enter the number of elements: "); fgets(buf, sizeof(buf), stdin ); sscanf_s(buf, "%d", &n); if (n > 0 && n <= 100) { int *x = new int[n]; initiate(x, n); starterequiv(x, 0, n); } }
•
•
Join Date: Aug 2008
Posts: 51
Reputation:
Solved Threads: 0
•
•
•
•
Don't use void main(), use int main() as I said in my previous post
C++ Syntax (Toggle Plain Text)
void starterequiv(int *x, int start, int n) { write(x, n); if (start < n) { int i, j; for (i = n-1; i > start; i--) { for (j = i + 1; j < n; j++) { swap(x, i, j); starterequiv(x, i, n); } reverse(x, i, n); } } }
![]() |
Other Threads in the C++ Forum
- Previous Thread: Infinite Loop
- Next Thread: please solve my problem
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings struct temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






