
09-01-2006, 23:09
|
|
|
|
חבר מתאריך: 10.12.05
הודעות: 6
|
|
|
Pemute
שלום לכולם מה נשמע?
יש לי בעיה אם פונקציה ידועה העונה לשם פרמוטציה... אני בטוח שמי שמבין יכול לעזור
כתבתי תוכנית רקורסיבית אשר אמורה לפתור בעיה זו, מסיבה שאינה ברורה לי היא לא עושה את זה
אני לא מבין את סוג השגיאה?
#include <stdio.h>
void permute(int k, int n, int arr[]);
void swap(int *x, int *t);
void print_permutation(int n, int* arr);
void main(){
int i,num,*arr;
printf("Enter positive integer greater than 0:\n");
scanf("%d\n",&num);
for (i=1; i<=num; i++) arr[i] = i; //inputs the numbers in the array
permute(1,num,arr);
}//main
void permute(int k, int n, int arr[]){
int i;
for (i=k; i<=n; i++) {
swap(arr[i], arr[k]); // put the k-th element in the i-th position
if (k == n)
print_permutation(n, arr);
else
permute(k+1, n, arr);
swap(arr[i], arr[k]); // return the k-th element to its original position
}
}
void swap(int x[],int y[]){
int temp;
temp = x[0];
x[0] = y[0];
y[0] = temp;
}
void print_permutation(int n, int* arr){
int i;
for (i=1; i<=n; i++)
printf("%d",arr[i]);
putchar('\n');
}
תוודה רבה
|