Codice:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
/* function to swap array elements */
void swap (int v[], int i, int j)
{
int t;
t = v[i];
v[i] = v[j];
v[j] = t;
}
void onChange(int a[], int v[], int n)
{
int j;
double x, y, z;
for (j = 0; j < n; j++)
printf ("%d ", a[v[j]]);
printf ("\n");
for (j = 2; j < n; j++ )
{
x = pow(a[v[j]] - a[v[j-1]], 2);
y = pow(a[v[j]] - a[v[j-2]], 2)/2;
z = x + y;
printf("Il quadrato della differenza tra %d e %d e' -> %f\n", a[v[j]], a[v[j-1]], x);
printf("La meta' del quadrato della differenza tra %d e %d e' -> %f\n", a[v[j]], a[v[j-2]], y);
printf("La somma tra %f e %f e' -> %f\n", x, y, z);
}
}
/* recursive function to generate permutations */
void perm (int a[], int v[], int n, int i, void (*pfnOnChange)(int a[], int v[], int n))
{
/* this function generates the permutations of the array
* from element i to element n-1
*/
int j;
int t;
/* if we are at the end of the array, we have one permutation
* we can use (here we print it; you could as easily hand the
* array off to some other function that uses it for something
*/
if (i == n)
{
if ( pfnOnChange )
pfnOnChange(a, v, n);
/*
for (j = 0; j < n; j++)
printf ("%d ", v[j]);
printf ("\n");
*/
}
else
{
/* recursively explore the permutations starting
* at index i going through index n-1
*/
for (j = i; j < n; j++)
{
/* try the array with i and j switched */
/* swap (v, i, j); */
t = v[i];
v[i] = v[j];
v[j] = t;
perm (a, v, n, i+1, pfnOnChange);
/* swap them back the way they were */
/* swap (v, i, j); */
t = v[i];
v[i] = v[j];
v[j] = t;
}
}
}
/* little driver function to print perms of first 5 integers */
int main(/*int argc, char *argv[]*/)
{
clock_t c_start, c_end;
double TempoImpiegato;
int *a;
int *v;
int N;
int i;
N = 4;
a = (int*)calloc(N + 1, sizeof(int));
if ( !a )
{
printf("Error: insufficient memory.\n");
return -1;
}
a[0] = 0;
a[1] = 1;
a[2] = 5;
a[3] = 7;
a[4] = 3;
v = (int*)calloc(N + 1, sizeof(int));
if ( !v )
{
printf("Error: insufficient memory.\n");
free(a);
return -1;
}
for (i = 0; i < N; i++)
v[i] = i+1;
c_start = clock();
perm (a, v, N, 0, onChange);
c_end = clock();
TempoImpiegato = (double)(c_end - c_start) / CLOCKS_PER_SEC;
printf("\nTempo impiegato -> %5.5f secondi\n\n", TempoImpiegato);
free(v);
return 0;
}
ehehhee grazie per l'aiuto e per il consiglio del Prof ehehe..