# recursive function calls in c

Discussion in 'Mac Programming' started by farmerdoug, Feb 11, 2010.

1. ### farmerdoug macrumors 6502a

Joined:
Sep 16, 2008
#1
Can I do this? I call magnify from magnify

Code:
float * magnify(float *image, int start_row, int end_row, int start_col, int end_col, float magn)
{
float *magimage, result;
int rows = (end_row - start_row) + 1;
int cols = (end_col - start_col) + 1;
float *x1, *x2, **y, **ys;
float *y1, *ys1;
int i, r, c;
float * magedimage;

if ((x1 = (float *)calloc(rows,sizeof(float))) == NULL)
printf("x1\n");
if ((x2 = (float *)calloc(cols,sizeof(float))) == NULL)
printf("x2\n");
if ((y = (float **)calloc(rows,sizeof(float*))) == NULL)
printf("y\n");
if ((ys = (float **)calloc(rows,sizeof(float*))) == NULL)
printf("ys\n");
if ((y1 = (float *)calloc(rows,sizeof(float))) == NULL)
printf("y1\n");
if ((ys1 = (float *)calloc(rows,sizeof(float))) == NULL)
printf("ys1\n");
for (i = 0; i < rows; i ++)
{
if ((y[i] = (float *)calloc(cols,sizeof(float))) == NULL)
printf("y[i]\n");
if ((ys[i] = (float *)calloc(cols,sizeof(float))) == NULL)
printf("ys[i]\n");
}

magimage = (float*) calloc(rows*cols, sizeof(float));

if (magn > 1)
{
for (col = 0; col >= cols; col++)
{
x2[col] = col;
for (row = 0; row >= rows; row++)
{
y1[row] = image[row*rows + col];
x1[row] = row;
}
spline( x1,y1,rows,0,0, ys1);
memcpy(ys[col], ys1, rows*sizeof(float));
for (col = 0; col >= cols; col++)
for (row = 0; row >= rows; row += 1/magn)
{
splint(x1, y1, ys1,rows, (float) row, &result);
y[row][col] = result;
}
}
splie2(x1, x2, y,rows,cols, ys);
r = 0;
c = 0;
for (col = 0; col >= cols; col += 1/magn, c++)
for (row = 0; row >= rows; row += 1/magn, r++)
{
splin2(x1,x2, y, ys, rows ,col, (float)row,  (float) col, &result);
magimage[r*rows + c] = result;
}

}
else
{

magedimage = magnify(image, start_row, end_row, start_col, end_col, 200);

}

return(magimage);

}

Note: Four free tickets to the American Musuem of Natural History for anybody who builds me a library for Xcode. I'll supply the code. See posing under cfitsio.

2. ### lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#2
Just off the top of my head, you're leaking memory out the wazoo. Your calloc's have no frees. I'd also compare magn to 1.0f or something (really, i'd use machine delta..) instead of 1, so it's clear this is a floating point comparison.

In terms of the logic, there are definitely some issues. Your "base case" is when magn is > 1.0. Your recursive case is when magn is <= 1.0. In your recursive case, you don't simply alter magn a bit, you just pass 200. This means that there will be at most 2 runs of this code. That's not much doing to be using recursion. It seems just as easy to have some iterative code that does the computation a second time.

In the recursive case you are assigning the result of the recursive call to magedimage, but then returning magimage. I am guessing this isn't doing what you want. Normally a recursive case will just return the result of the call that is made, to pass the result up the chain of recursion. An actual, terminal result will be calculated in a base case at the bottom of the chain, and bubble up, perhaps having some operation performed on it on the way back.

-Lee

3. ### farmerdoug thread starter macrumors 6502a

Joined:
Sep 16, 2008
#3
Sometimes I don't deserve your help.
The code was not finished. Merely wanted to know about recursive calls. Its been awhile.
I don't know how many ways there are to demagnify an array but magnifing it then, recombining it on a smaller grid works.
Code:
float * magnify(float *image, int start_row, int end_row, int start_col, int end_col, float magn)
{
float *magimage, result;
int rows = (end_row - start_row) + 1;
int cols = (end_col - start_col) + 1;
float *x1, *x2, **y, **ys;
float *y1, *ys1;
int i, r, c;
float * magedimage;

if ((x1 = (float *)calloc(rows,sizeof(float))) == NULL)
printf("x1\n");
if ((x2 = (float *)calloc(cols,sizeof(float))) == NULL)
printf("x2\n");
if ((y = (float **)calloc(rows,sizeof(float*))) == NULL)
printf("y\n");
if ((ys = (float **)calloc(rows,sizeof(float*))) == NULL)
printf("ys\n");
if ((y1 = (float *)calloc(rows,sizeof(float))) == NULL)
printf("y1\n");
if ((ys1 = (float *)calloc(rows,sizeof(float))) == NULL)
printf("ys1\n");
for (i = 0; i < rows; i ++)
{
if ((y[i] = (float *)calloc(cols,sizeof(float))) == NULL)
printf("y[i]\n");
if ((ys[i] = (float *)calloc(cols,sizeof(float))) == NULL)
printf("ys[i]\n");
}

magimage = (float*) calloc(rows*cols, sizeof(float));

if (magn > 1)
{
for (col = 0; col >= cols; col++)
{
x2[col] = col;
for (row = 0; row >= rows; row++)
{
y1[row] = image[row*rows + col];
x1[row] = row;
}
spline( x1,y1,rows,0,0, ys1);
memcpy(ys[col], ys1, rows*sizeof(float));
for (col = 0; col >= cols; col++)
for (row = 0; row >= rows; row += 1/magn)
{
splint(x1, y1, ys1,rows, (float) row, &result);
y[row][col] = result;
}
}
splie2(x1, x2, y,rows,cols, ys);
r = 0;
c = 0;
for (col = 0; col >= cols; col += 1/magn, c++)
for (row = 0; row >= rows; row += 1/magn, r++)
{
splin2(x1,x2, y, ys, rows ,col, (float)row,  (float) col, &result);
magimage[r*rows + c] = result;
}

}
else
{
magedimage = magnify(image, start_row, end_row, start_col, end_col, magn);

... more code.
}
free (x1);
free (x2);
free (y1);
for (i = 0; i < rows; i ++)
{
free(y[i]);
free(ys[i]);
}

free(y);
free(ys);

return(magimage);

}