Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- void swap(int *num1, int *num2) { // standard swap function
- int temp; // temporary var
- temp = *num1; // store to temp
- *num1 = *num2; // swap it
- *num2 = temp; // swap it again :D
- }
- int rearange(int *numbers, int index, int num, int fact) {
- swap(&numbers[index], &numbers[index-1]); // swap number at index with the one behind it
- /*int temp = numbers[index];
- numbers[index] = numbers[index-1];
- numbers[index-1] = temp;*/
- int i;
- for (i = 1; i <= num; ++i) // print the current sequence
- {
- printf("%d ", numbers[i]);
- }
- printf("\n");
- fact--; // decrement how many sequences remain
- index--; // decrement our index in the array
- if (index > 1) {
- if (fact > 0) // If we have more sequences remaining
- rearange(numbers, index, num, fact);
- } else {
- return fact;
- }// Do it all again! :D
- }
- int main() {
- int num, i; // our number and a counter
- printf("Enter a number less than 10: ");
- scanf("%d", &num); // get the number from the user
- int numbers[11]; // create an array of appropriate size
- // fill array
- for (i = 1; i <= num; i++) { // fill the array from 1 to num
- numbers[i] = i;
- }
- int fact = 1; // calculate the factorial to determine
- for (i = 1; i <= num; ++i) // how many possible sequences
- {
- fact = fact * i;
- }
- do {
- fact = rearange(numbers, num, num, fact);
- } while (fact > 0);
- //rearange(numbers, num, num, fact); // begin rearranging by recursion
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement