Thread: recursive formulas with arrays

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    53

    recursive formulas with arrays

    Hi, I'm trying to make a program that prints the first 100 numbers of cont, using the recursive function listed below. Unfortunately, what I wrote doesn't work, so I was wondering, if it was even possible to do this.

    Code:
    #include <stdio.h>
    int main()
    {
        int cont[100],n;
    
        cont[n]=(n+1);
        for(n=1;cont<=100;n++)
        {
            printf("%s", cont);
        }
    }
    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You seem to not know what recursion is. All you have is a loop. A recursive function is a function that calls itself. Like so:
    Code:
    void foo( int x )
    {
        if ( x < 10 )
            foo( --x );
        else
            printf("x is %d\n", x );
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    ...

    ...

    Okay, several problems here:

    1) There is nothing recursive about that code whatsoever.
    2) You're using n uninitialized
    3) Your cont array doesn't actually contain anything meaningful.
    4) The %s specifier for printf() expects the passed argument to be a string. cont is not a string.
    If you understand what you're doing, you're not learning anything.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    5) I have no idea what the hell that foo function is supposed to do. I blame tequila.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It's supposed to be a very long loop if x is less than 10.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  5. recursive 2D arrays
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-13-2002, 01:29 PM