I use this code, but get an segmentation error. What is wrong here ?
GNU nano 2.2.6 File: taak8.c
#include<stdio.h>
double recursie(double som,double oud, double x, int stap){
double y = oud*x/stap;
if(y >= 1/1000){
return recursie(som+y,y,x,stap++);
} else {
return som;
}
}
double exp(double x){
return recursie(1,1,x,1);
}
int main(){
double inp;
scanf("%lf",&inp);
printf("your result %lf",exp(inp));
return 0;
}
if(y >= 1/1000) problem is there. 1/1000 will always be 0. so put 0 there directly.
But i think you wanted to do this below , try this instead
if(y >= 1.0/1000)
and this also
recursie(som+y,y,x,++stap);
use ++stap to increment stap instead of stap++.Because you have to send the incremented value of stap to recursive function call.
You need to increment your step before you call the recursive step, not after. In other words, you need to use pre-increment, not post-increment:
return recursie(som+y,y,x,stap+1);
Related
I'm a programming rookie who has not yet started. I just learned recursion and there are some problems with the use of recursion. There is a homework is judge prime numbers :using int prime(int x); and return boolean value.
Initially I found that because the variable is initialized and assigned inside the function,the program can't achieve self-increment. Because every time it enters a new level of recursion, the variable will be reassigned. Even if you write a variable auto-increment statement, it will only auto-increase the variables stored in the current recursive stack. Once the variable enters a new recursive level, the variable is only initialized according to the definition and cannot be continuously auto-incremented.
The solution to the failure is as follows:
#include <math.h>
#define false 0
#define true 1
int prime(int x){
double high=sqrt(x);
int low=2;
if((x%low==0 && x!=2) || low>high){
return false;
}
else if(x<2){
return false;
}
else{
return true;
}
low++;
return prime(x);
}
When asking questions, I found a successful solution:
#include <math.h>
#define false 0
#define true 1
int prime(int x){
double high=mysqrt(x);
static int low=2;
if((x%low==0 && x!=2)||low>high){
return false;
}
else if(x<2){
return false;
}
else{
return true;
}
low++;
return prime(x);
}
But I can't understand why using static to modify the variable can make the variable correctly increment when entering a new layer of recursion instead of executing the previous int low=2;
Ask the master to solve the confusion for me, what happened to the two in the memory space?
In addition, there seems to be another solution, which seems to be to set a flag variable, but I did not understand it. Can someone provide other solutions?
In a nutshell, ordinary variables (int low;) get created for each function call independently, while static (static int low = 2;) are created once and shared between all the functions.
However, static is not the best approach to use in such cases, because different function calls may need to have different values of high/low.
Instead, you may add explicit parameters to the function, something like this (the algorithm is wrong, but it's the general principle):
int prime(int x) { return prime_impl(x, 2, sqrt(x)); }
int prime_impl(int x, int low, double high) {
if(x<2) {
return false;
}
else if((x%low==0 && x!=2)||low>high) {
return true;
}
else {
return prime_impl(x, low+1, high);
}
}
I am very new to programming in C, and can't seem to locate the cause of the segmentation error that I have been getting. The program I wrote is as follows:
# include <stdio.h>
# include <stdlib.h>
int recursive(int x){
if(x=0)
{
return 2;
}
else
{
return 3*(x-1)+recursive(x-1)+1;
}
}
int main(int argc, char *argv[])
{
int N = atoi(argv[1]);
return recursive(N);
}
I would appreciate any help.
Thanks a lot
if(x=0){...}
it's wrong
It should be
if(x==0){...}
Note:
if (x = 0)
is the same as:
x = 0; if (x)
This:
if(x=0){
is not a (pure) test, it's an assignment. It works in the if since it also has a value (zero), but it's always false so that branch is never taken, i.e. the recursion never stops.
You should enable all compiler warnings, this is very commonly caught by compilers.
Change if(x = 0) to if(0 == x)
It is a good rule of hand to write 0 == x instead of x == 0 because in case of a typo like = instead of == the compiler will give an error.
The segfault error is from the use of argv[1]. Make sure you call your function with an argument, as follow:
$ ./a.out 6
with a.out the name of your program, and 6 the number you want to apply the function on.
The following line will create a segfault :
$ ./a.out
because the first argument isn't set.
Plus, watch out on the second line : use == instead of =
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
My professor gave us this code in class to show how a program works and said "go home and try it and you'll see it works".... well after 30 minutes I cannot get it to run. can someone please help me and point me in the right direction. Thank you!
-I get function definition on the end "double g(double x)"
-On the first else where x_left = x_mid control reaches end of non-void function
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define FALSE 0
#define TRUE 1
#define NO_ROOT -99999.0
//function prototypes
double bisect(double, double, double, double f(double farg));
// evaluation of function
double g(double);
double h(double);
int main(void) {
double x_left, x_right, epsilon, root; //declare variables
// get endpoint and error tolerance
printf("\nEnter interval endpoints > ");
scanf("%lf%lf", &x_left, &x_right);
printf("\nEnter tolerance > ");
scanf("%lf", &epsilon);
//use bisect function to look for roots of functions
printf("\n\n For function g(x)");
root = bisect(x_left, x_right, epsilon, g);
if (root != NO_ROOT)
printf("\n g(%.7f) = %e\n", root, g(root));
printf("\n\n For function h(x)");
root = bisect(x_left, x_right, epsilon, h);
if (root != NO_ROOT)
printf("\n h(%.7f) = %e\n", root, h(root));
system("pause");
return (0);
}
// bisection method program coding
double bisect(double x_left, double x_right, double epsilon, double f(double farg)){
double x_mid, f_left, f_right, f_mid;
int root_found;
// computes function at initial end points
f_left = f(x_left);
f_right = f(x_right);
// if no change in sign
if (f_left * f_right > 0) {
printf("\nmay not be no root in [%.7f, %.7f]\n\n", x_left, x_right);
return NO_ROOT;
}
// searches as long as interval size is large enough
root_found = FALSE;
while (fabs(x_right - x_left) > epsilon && !root_found) {
// compute the mid point
x_mid = (x_left + x_right) / 2.0;
f_mid = f(x_mid);
if (f_mid == 0.0) {
root_found = TRUE;}
else if (f_left * f_mid < 0.0) {
x_right = x_mid;
} else {
x_left = x_mid;
}
// trace loop execution
if (root_found)
printf("\nRoot found at x = %.7f , midpoint of [%.7f, %.7f] ", x_mid, x_leftx_right);
else
printf("\nNew interval is [%.7f, %.7f] \n\n", x_left, x_right);
//if there is a root
return ((x_left + x_right)/2.0);
}
// functions for which roots are sought
double g(double x){
return (5 * pow(x, 3.0) - 2 * pow(x, 2.0) +3);
}
double h(double x){
return (pow(x, 4.0) - 3 * pow(x,2.0) - 8);
};
}
I get an error on this line:
printf("\nRoot found at x = %.7f , midpoint of [%.7f, %.7f] ", x_mid, x_leftx_right
saying that x_leftx_right is undeclared.
If I change this to x_left, x_right then it compiles OK except for "undefined reference to g" and "undefined reference to h".
The reason for the undefined reference to g is that you never provided a function definition for the function g that was prototyped by double g(double);. You did provide a nested function g within bisect. Nested functions are a non-standard extension, and bisect::g is a different function to g. Similarly for h.
To fix this, move the definitions of g and h to be after the end of the bisect function; instead of inside that function.
The reason for your "control reaches end of non-void function" warning is probably because there is no return statement after the while loop.
Your line return ((x_left + x_right)/2.0); line is within the loop begun by while (fabs(x_right - x_left) > epsilon && !root_found) {. If this loop finishes by the loop condition no longer being true, then the execution hits the end of the function without returning anything.
NB. If you indent your code properly so that you line up { then you are less likely to have this sort of problem. Your editor should have a key that you can use to find matching curly-braces. Also, operating your compiler in strict standard mode would have given an error about the use of nested function.
`
I have a problem with bisection method (recursive implementation) that doesn't work. The program just crashes after entering a&b values ...
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define e 0.0001
#define dbg 1
using namespace std;
double f(double x){
return x*x*x-x-2;
}
double bisection(double a,double b){
double x1;
x1=(b+a)/2;
if(x1>e){
if(f(b)*f(x1)<0)
{
a=x1;
}
else
if(f(a)*f(x1)<0)
b=x1;
bisection(a,b);
}
return x1;
}
int main () {
int a,b;
double root;
printf("a=");
scanf("%d",&a);
printf("b=");
scanf("%d",&b);
if(f(a)*f(b)<0){
root=bisection(a,b);
printf("root %g",root);
}
system("pause");
return 0;
}
I have tried to display some debugging messages, but I couldn't figure it out.
As #Gene pointed out, you never use the result of the recursive call. Further, what you DO return is just the midpoint between a&b, which you don't need recursion to find. (Related?)
Note that, if the 2 ifs used to change either a or b for the recursive call both fail, then you make a recursive call w/ unchanged values of a & b ==> infinite recursion, a sure way to get a segfault.
In my code I define a step size "h" before a while loop. Somehow it seems to change by itself when I try to use it in the loop. If I define it inside the loop it seems to be ok, but the data I get doesn't seem to be right so I'm guessing the problem might be related.
Even when I print it at this location (see the printf in the code) the output is 3 values and I have no idea why. If you see anything else that's not related but seems wrong please tell me, as I said I'm getting unexpected values (it may just be my formulas).
int main()
{
FILE *f1;
f1 = fopen("Question2 part 3 solution.txt", "w");
double r0=0.05;
double dr0=-a*a*r0/sqrt(1+pow(a*a*r0,2)),h=0.01;
double k[4][3],x[]={dr0,z0,T0,r0},x1[]={0,0,0,0}, s=0;
int i,j;
while(s<=1)
{
//Runge-Kutta
for (j=0;j<4;j++)
{
for (i=0;i<4;i++)
{
if (j==0){k[i][0]=h*System(i,x[0],x[1],x[2],x[3]);}
if (j==1){k[i][1]=h*System(i,x[0]+k[0][0]/2.0,x[1]+k[1][0]/2.0,x[2]+k[2][0]/2.0,x[3]+k[3][0]/2.0);}
if (j==2){k[i][2]=h*System(i,x[0]+k[0][1]/2.0,x[1]+k[1][1]/2.0,x[2]+k[2][1]/2.0,x[3]+k[3][1]/2.0);}
if (j==3){k[i][3]=h*System(i,x[0]+k[0][2],x[1]+k[1][2],x[2]+k[2][2],x[3]+k[3][2]);}
}
}
for (i=0;i<4;i++)
{
x[i]=x[i]+(k[i][0]+2.0*k[i][1]+2.0*k[i][2]+k[i][3])/6.0;
}
printf("%8.3lf",h);
s+=h;
}
fclose(f1);
return(0);
}
double System(int i,double dr, double z, double T, double r)
{
//printf("%e\t%e\t%e\t%e\n",dr,z,T,r);
if (T==T0 && z==z0 && i==0) {return (-a*a*dr)*pow(1-dr*dr,3/2)/2.0;}
if (i==0 && T!=0){return (-a*a*r*(1-dr*dr)-dr*sqrt(1-dr*dr))/T;}
if (i==1){return (-sqrt(1-dr*dr));}
if (i==2){return (-a*a*r*dr+sqrt(1-dr*dr));}
if (i==3){return (dr);}
//if (i==3){return (-m2*l1*l2*B*theta1dt*theta2dt*sin(theta2-theta1)-l2*m2*g*B*sin(theta2));}
}
Thanks in advance!
See the declaration of k:
double k[4][3]
And then see this statement
k[i][3]=...
Here you write beyond the boundaries of the array, leading to undefined behavior.
You are overrunning the memory, variable k is defined as double k[4][3], but you are updating k[i][3]when j==3