Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,231 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,856 people online right now. Registration is fast and FREE... Join Now!




Recursive Sequence 1, 2, 4, 7, 13, 24

 
Reply to this topicStart new topic

Recursive Sequence 1, 2, 4, 7, 13, 24

kkgaming
19 Mar, 2007 - 02:05 PM
Post #1

D.I.C Head
**

Joined: 7 Feb, 2007
Posts: 75


My Contributions
I want to create a sequence as follows: 1,2,4,7,13,24. This works by Finding the next term by adding the values of the 3 terms before it in the sequence. I am really struggling to get anywhere writing this code recursively, no idea where to begin.

Here is the iterative code:

CODE

#include <stdio.h>
#include <stdlib.h>
#define iterations 6

//Sequence List Iteration Function 1, 2, 4, 7, 13, 24

void Sequence1()
{

  int i;
  int number[3]={1,2,4};
  int A, B;

  printf("%d %d ", number[0], number[1]);

for (A = 0; A <  iterations-2; A++)
   {
   printf("%d ",number[2]);
   int number2 = number[2] + number[1] + number[0];
   for (B = 0; B < 2; B++) number[B] = number[B + 1];
   number[2]=number2;
   }
  printf("\n");
  }

int main()
{




//Print out iteration greater than
  
   Sequence1();



return 0;
}


This post has been edited by kkgaming: 19 Mar, 2007 - 02:06 PM
User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: Recursive Sequence 1, 2, 4, 7, 13, 24
20 Mar, 2007 - 11:02 AM
Post #2

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 292



Thanked: 2 times
Dream Kudos: 50
My Contributions
http://dreamincode.net/forums/showtopic25218.htm
User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: Recursive Sequence 1, 2, 4, 7, 13, 24
21 Mar, 2007 - 08:50 PM
Post #3

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 292



Thanked: 2 times
Dream Kudos: 50
My Contributions
Lets take a look at a couple of items.

CODE

  int number[iterations]={1,2,4,7,13,24};


void Sequence(int i, int *number) {
  if (i == iterations) {
    printf("\n");
    return;
  }
  
  printf("%d ", number[i]);
  
  return Sequence(i + 1, number);
}


User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Recursive Sequence 1, 2, 4, 7, 13, 24
21 Mar, 2007 - 11:06 PM
Post #4

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
To write this code, think on the lines of the recursive Fibonacci Series Function.
You'll end up getting this code.
CODE

int func(int n)
{
    if(n<1)
      return 0;
    else
    {
        if(n==1)
           return 1;
        else
           return func(n-3)+func(n-2)+func(n-1);
     }
}

int main()
{
   for(int i=2;i<10;i++)
      cout<<func(i)<<endl;

   return 0;
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 03:23PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month