Thread: recursion project

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    12

    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

  2. #2
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    why not, what do you have so far?

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    4

    Cool 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....

  4. #4
    Unregistered
    Guest
    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.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    12

    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);
    	}
    }
    Last edited by Psycho; 03-31-2002 at 03:27 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursion Project
    By mrmet924 in forum C Programming
    Replies: 4
    Last Post: 05-09-2009, 11:23 PM
  2. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  3. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM