1 Attachment(s)

Divide and Conquer: array in the reverse order

Hi!

I'm having a problem with a divide and conquer algorithm. The function dac must return an array in the reverse order. What the function does is splitting the array, and make two recursive calls. We supose the problem known for each of the two halfs, and then, we combine the solution depending on if the array is even or odd.

Well, a classic divide and conquer problem.

I'll put the code here. It compiles correctly, but it doesn't work. (1,2,3,4,5,6) array, in the reverse order, according to the executable file is: (6,0,4,3,2,1). :mad:

I'd be grateful if someone helped. Thanks.

Code:

`#include <stdio.h>`

void daq (int *v, int begin, int end);

int main()

{

int i,k; //iterators

int *v=(int*)malloc(6*sizeof(int));

//int v[6];

for (i=0; i<7; i++)

{

printf(" v[%d]: ", i);

scanf("%d", &v[i]);

}

printf("\nThe array is:\n");

for (k=0; k<7; k++)

{

printf("v[%d]= %d ", k, v[k]);

}

printf("\n");

dac(v,0,6); //first call

printf("The array, in the reverse order, is:\n");

for (k=0; k<7; k++)

{

printf("v[%d]= %d ", k, v[k]);

}

printf("\n");

free(v);

return 0;

}

void dac (int *v, int beg, int enn)

{

//base: size==1

if (fin==ini); //don't do anything

else

{

int mid=(beg+end)/2;

int j;

dac(v, beg, mid);

dac(v, mid+1, end);

if (mid%2==0) //size is even

{

int g=0;

for (j=beg; j<=mid; j++,g++)

{

int aux=v[j];

v[j]=v[g+mid+1];

v[g+mid+1]=aux;

}

}

else { //odd

int h,aux2,g;

g=1;

for (h=beg+1; h<=mid; h++,g++)

{

aux2=v[h];

v[h]=v[mid+g];

v[mid+g]=aux2;

}

int z;

for (z=beg; z<mid; z++)

{

int aux3;

aux3=v[z];

v[z]=v[z+1];

v[z+1]=aux3;

}

}

}

}