Thread: Logic problem

  1. #1
    Village id10t
    Join Date
    May 2008
    Posts
    57

    Logic problem

    Ok, i am busy learning recursion. I want a function that receives two int and add everyting from int a to int b. then it must return the sum
    Code:
    double Function(int Start, int End)
    {
    
    int sum=0;
    if(End>=Start)
    {
       cout<<Start<<endl;
       sum+=Start;
       Start++;
       Function(Start ,End);
       
    }
    else return sum;
    }
    I cant use sum because with each recursion sum gets int to zero. I dont want to pass a variable for sum to the function and i dont want to use a global variable... any suggestions for the noob?

    Thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Best way:
    Code:
    double foo(int begin, int end, double sum = 0)
    {
        if (begin == end)
            return sum;
        sum += begin++;
        return foo(begin, end, sum);
    }
    Otherwise you have to mess with a static or global variable and this is not recommended.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Village id10t
    Join Date
    May 2008
    Posts
    57
    Ok - so without using global varibales, this is the only way? what if I dont want to pass sum to the function, but only the 2 ints? is that possible?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's possible via a static variable, but then you would have trouble because you have to reset that everytime. The static variables will retain its value over the function's entire lifetime.
    However, by using an optional argument as the last, you don't have to pass in a third argument when calling the function, while the function will use the third argument itself when recursing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    14
    What about
    Code:
    double foo(int start, int end)
    {
       if ( end < start )
          return 0;
    
       return start + foo(start + 1, end);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logic
    By LordBronz in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2006, 05:41 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM