Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [C] Need help with recursion program
New Posts  All Forums:Forum Nav:

# [C] Need help with recursion program

So I have a program to make using recursions.

User has to enter value of e. e has to be 0 < e < 1.
With recursion it has to make array A :
A0 = 0,
A1 = 1/2,
A2 = (A1+1)/(A1+2)
etc.

Until |AL - AL-1| <= e or L = 15.

This is what I have done so far, but it crashes lol:
Code:
``````#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define N 15

double rekursion(int);
void toscreen(double[],int);

int main(void)
{
double e,subtraction;
double A[N];
int L=0;
printf("Enter e: ");
scanf("%lf",&e);
if(0 > e || e > 1)
{
printf("e has to be higher than 0 and lower than 1");
exit(1);
}
A[L]=rekursion(L);
do
{
L++;
A[L]=rekursion(L);
subtraction=A[L]-A[L-1];
}while ((L<N)&&(fabs(subtraction)>e));
toscreen(A,L);
return 0;

}

double rekursion(int argL)
{
if(argL==0)
{
return 0;
}
else
{
return rekursion(argL+1)/rekursion(argL+2);
}
}

void toscreen(double massA[], int argL)
{
int i=0;
printf("%d. \t%.5lf\n", i, massA[0],massA[i]);
for(i=1;i<argL;i++)
{
printf("%d.\t%.5lf\t%.5lf\n",i,massA[i],massA[i]-massA[i-1]);
}
}```
```

Thanks.
Edited by Andr3az - 4/27/12 at 7:49am
 Companion Box (15 items)
CPUMotherboardGraphicsRAM
AMD Phenom II X4 960T 870 Extreme3 R2.0 AMD 280x 12gb G..Skill 1333 mhz DDR3
Hard DriveOptical DriveCoolingOS
120gb Intel 520 ssd + SAMSUNG 150 GB + Samsung... TSSTcorp CD/DVDW SH-S182M ATA D Xigmatek S1283 Windows 8 x64
MonitorKeyboardPowerCase
23.6" AOC E2461FWH Logitech Internet 350 Keyboard Corsair VX550W Shinobi FC-SH09A
MouseAudio
Logitech Mouse Asus Xonar DX
 Companion Box (15 items)
CPUMotherboardGraphicsRAM
AMD Phenom II X4 960T 870 Extreme3 R2.0 AMD 280x 12gb G..Skill 1333 mhz DDR3
Hard DriveOptical DriveCoolingOS
120gb Intel 520 ssd + SAMSUNG 150 GB + Samsung... TSSTcorp CD/DVDW SH-S182M ATA D Xigmatek S1283 Windows 8 x64
MonitorKeyboardPowerCase
23.6" AOC E2461FWH Logitech Internet 350 Keyboard Corsair VX550W Shinobi FC-SH09A
MouseAudio
Logitech Mouse Asus Xonar DX
It looks like you need to stop the rekursion function from running indefinitely.

The way it's designed now, if you call rekursion(1), it will then call rekursion(2), which will call rekursion(3) and rekursion(4), which will keep going forever.

The terminating condition argL==0 will never be reached.
But doesn't the
Code:
``````do
{
L++;
A[L]=rekursion(L);
subtraction=A[L]-A[L-1];
}while ((L<N)&&(fabs(subtraction)>e));```
```

Stop the loop? ( i got the do while thingy from an example ).
 Companion Box (15 items)
CPUMotherboardGraphicsRAM
AMD Phenom II X4 960T 870 Extreme3 R2.0 AMD 280x 12gb G..Skill 1333 mhz DDR3
Hard DriveOptical DriveCoolingOS
120gb Intel 520 ssd + SAMSUNG 150 GB + Samsung... TSSTcorp CD/DVDW SH-S182M ATA D Xigmatek S1283 Windows 8 x64
MonitorKeyboardPowerCase
23.6" AOC E2461FWH Logitech Internet 350 Keyboard Corsair VX550W Shinobi FC-SH09A
MouseAudio
Logitech Mouse Asus Xonar DX
 Companion Box (15 items)
CPUMotherboardGraphicsRAM
AMD Phenom II X4 960T 870 Extreme3 R2.0 AMD 280x 12gb G..Skill 1333 mhz DDR3
Hard DriveOptical DriveCoolingOS
120gb Intel 520 ssd + SAMSUNG 150 GB + Samsung... TSSTcorp CD/DVDW SH-S182M ATA D Xigmatek S1283 Windows 8 x64
MonitorKeyboardPowerCase
23.6" AOC E2461FWH Logitech Internet 350 Keyboard Corsair VX550W Shinobi FC-SH09A
MouseAudio
Logitech Mouse Asus Xonar DX
Quote:
Originally Posted by Andr3az

But doesn't the
Code:
``````do
{
L++;
A[L]=rekursion(L);
subtraction=A[L]-A[L-1];
}while ((L<N)&&(fabs(subtraction)>e));```
```
Stop the loop? ( i got the do while thingy from an example ).

Nope. Once you execute the "rekursion" method, it takes over and keeps on calling itself (rekursion(1) and rekursion(2) then onward) so the do...while loop does nothing at all after the first call to that method.

You should design it as such so that the argument in the rekursion() call eventually reaches 0. That'll be the only way it stops.
 MacBook Pro 13" (6 items) Toshiba Satellite L655 (13 items) Desktop (13 items)
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB
OSMonitor
Windows 7 64bit HP 15.6" 1366x768
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W
 MacBook Pro 13" (6 items) Toshiba Satellite L655 (13 items) Desktop (13 items)
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB
OSMonitor
Windows 7 64bit HP 15.6" 1366x768
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W
New Posts  All Forums:Forum Nav:
Return Home
Back to Forum: Coding and Programming
• [C] Need help with recursion program
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [C] Need help with recursion program