-
recursion project
I found this random project on the net to test my understanding of recursion... I've been working on it for a long time, still can't get it to work.
Min/Max/Avg
Create a program that will take in ten doubles, then display them,
then give the Maximum, Minimum, and the Average. Hint: Use recursion to cut down the size of the program.
how would you do this? I can do it simply with functions, but can't get it to work with recursion.
Thanks.
-Psycho
-
why not, what do you have so far?
-
help
hi !
the thing is that u can use the recursive functions and then call em up wherever u want ... if u still want some more help then i can help u by making a comprehensive program on that....:cool:
-
Here is an example of recursion.
Code:
void DrawGrid(int iteration,int x,int y,int x2,int y2)
{
if ((x2-x)<=1) return;
int midx=(x2+x)>>1;
int midy=(y2+y)>>1;
color=random(255);
Box(x,y,x2,y2,color);
DrawGrid(iteration-1,x,y,midx,midy);
DrawGrid(iteration-1,midx,y,x2,mid);
DrawGrid(iteration-1,x,midy,midx,y2);
DrawGrid(iteration-1,midx,midy,x2,y2);
}
void Box(int x,int y,int x2,int y2,int color)
{
//Draw a box
}
Slow the code down and you will begin to understand what it is doing and hopefully what recursion is all about. There are times when it is very useful, and there are times when it should be avoided like the plague.
Recursion takes up a lot of stack space since every time you call a function, a stack frame is created. Also, this function takes parameters which must be pushed/popped off of the stack which takes time as well. This function should not overflow the stack, but a function like a floodfill most certainly would. So, if stack space is not a problem and if implementing the function iteratively is overly complex then use recursion. Otherwise, go with the iterative approach. Recursion is used in fractals and is also used to generate terrain maps on the fly. The above algorithm could be altered to produce a terrain using the midpoint displacement algo.
-
Thanks
Thanks for your help everyone. I finally got it last night.
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int * MinMaxAve(int values[], int size);
int main()
{
int doubles[] = {2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048};
int len = sizeof doubles/sizeof doubles[0];
for(int i=0; i<len; i++)
{
cout << "Input " << setw(2) << i << ": " << doubles[i] << endl;
}
int * returns = MinMaxAve(doubles, len);
cout << "Minimum Value=" << *(returns) << endl;
cout << "Maximum Value=" << *(returns + 1) << endl;
cout << "Average Value=" << *(returns + 2) << endl;
return 0;
}
int * MinMaxAve(int values[], int size)
{
static int index = 0;
static int returns[3];
static int ave = 0;
static int min = values[index];
static int max = values[index];
if(index < size)
{
min = (min < values[index]) ? min : values[index];
max = (max > values[index]) ? max : values[index];
ave += values[index];
++index;
return(MinMaxAve(values, size));
}
else
{
returns[0] = min;
returns[1] = max;
returns[2] = ave / index;
return(returns);
}
}