Now I am practicing using Vim on Linux.
I made a simple code like this
#include <stdio.h>
int factorial(int n){
if (n<0) { return 0; }
else if (n==0) { return 1; }
else { return n * factorial(n); }
}
int main(void){
int n = 0;
printf("Put n value : ");
scanf("%d", &n); /* non-OP inserted ";" to help people focus on the problem */
printf("%d! = %d\n", n, factorial(n));
return 0;
}
When I put -1 and 0, it works. They return 0 and 1.
However, when I put positive integer values on n, it didn't work.
I tried to find out the reason so I used gdb,
but it just said like this :
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400620 in factorial ()
What's wrong with my code? I even cannot understand the point.
When n > 0 your recursive program never terminates. The value of n is never decremented and so it continues running recursively until your run out of memory.
It should be return n * factorial(n-1);
You code is causing stack overflow. In the given function n is never decremented. Last statement should be
else { return n * factorial(n-1); }
Related
#include <stdio.h>
void main(){
static int n=7;
if(n){
n--;
main();
printf("%d",n);
}
}
Sol: I was trying to trace the recursion. In my logic answer should be 1234567, but the answer is 000000. My question is how to trace this recursion?
That is because you are calling main before printing.
if you do:
#include <stdio.h>
void main(){
static int n=7;
if(n){
n--;
// main();
printf("%d",n);
main();
}
return;
}
You will get the output: 6543210
But in your case, it will print only when the value of n is 0. And since 7 print is pending therefore 0000000
There exists only one variable since it's static. A slight modification (including the fix of void return type) demonstrates it:
#include <stdio.h>
int main(void)
{
static int n=7;
if(n) {
n--;
printf("%d",n);
main();
printf("%d",n);
}
}
Output:
65432100000000
The best way to trace it probably is using gdb to follow it along.
If you declare n as static, it's value would be preserved across function calls. Note that you are calling printf after the recursive step (calling main). This causes printf to see the value of n after all the calls to main.
Think of this sequence, executing with n = 2
if (n) // first time, n == 2.
n--; // n == 1
main();
if (n) // second time, n == 1
n--;
main();
if (n) // third time, n == 0, fails
printf("%d",n); // n == 0.
printf("%d",n); // n == 0.
i am trying to find factorial by recursively.My function works but why i recieve "segmenatation fault (core dumped)" when i input -1.
#include <stdio.h>
int fak(int number);
int main(){
int i;
printf("give me an integer: ");
scanf("%d",&i);
printf("factorial: %d\n",fak(i));
return 0;
}
int fak(int number){
if(number == 1 || number == 0){
return 1;
}
return number * fak(number - 1);
}
To answer the question: you will get a stack overflow because your recursive method will never end normally (see all the comments).
If you put in -1, the method will be called again with -2.
It depends on your stack-size when it will crash.
You can change your condition to:
if(number <= 1){
return 1;
}
Besides of that: the factorial of -1 doesn't exist.
When I tried to implement fibonacci sequence using recursion in C, I noticed that in my function fibo if I didn't use some kind of if statements that return 1 out of the function the program crashed.Why did this happens?
#include<stdio.h>
int fibo(int);
int main(void){
int n, num;
scanf("%d", &n);
num = fibo(n);
printf("apo: %d", num);
return 0;
}
int fibo(int n){
if(n==0)
return 0;
else if(n==1)
return 1;
else if(n!=0){
n = fibo(n-1) + fibo(n-2);
return n;
}
}
/*FOR EXAMPLE if I leave the fuction like this, it doesn't work*/
int fibo(int n){
n = fibo(n-1) + fibo(n-2);
return n;
}
Let's take the absolute simplest case of a recursive function.
int foo(int n)
{
return foo(n-1);
}
If you want to calculate foo(5) then the function foo will need to calculate foo(4) before returning. But same goes for foo(4). It has to calculate foo(3) first. So there is no value for n where the function does not need to call itself, thus recursing indefinitely. In theory that is. In practice it will crash.
Those cases that does not cause a recursive call are called base cases.
Your function needs a limit(base case) to no longer to execute itself, If you don't have the statement "if" to decide when to jump out of the function, your following program can't be executed. and the function won't stop execute until your program collapses.
This code is to print the Fibonacci series using recursion. So I thought to devise a recursion instead of using iteration but as soon as I just execute the code and as soon as the value provider function is executed it is showing some error "segmentation error". I want to do it this way only... Can anyone help? I'm just a beginner so please help and encourage me...
#include<stdio.h>
int fibonacci(int n)
{
int res;
if(n==0)
return 0;
if(n==1)
return 1;
else
res = fibonacci(n-1)+fibonacci(n-2);
return res;
}
int value_provider(int n)
{
int choice1;
if(n>=0)
{
choice1 = fibonacci(n-1);
n -- ;
}
printf("%d",choice1);
if(n>=0)
{
value_provider(n);
}
}
void main()
{
int n;
printf("enter the number");
scanf("%d",&n);
value_provider(n);
}
This code is showing segmentation fault...
What can I do to remove it rather than changing the code?
I want to do it only this way; please help!
I think your value_provider function has poor terminating conditions.
Don't try to calculate fibonacci of -1 so n must be >=1
Also once n is zero you need to finish the recursion, don't
call value_provider again.
Try something more like this;
int value_provider(int n)
{
int choice1=-1;
if(n>=1)
{
choice1 = fibonacci(n-1);
n -- ;
}
printf("%d ",choice1);
if(n>0)
{
value_provider(n);
}
}
I made this coding but I couldn't complete. The idea is should enter numbers and on the screen must shown as separately. Ex) If we enter:1234 result: 1 2 3 4.
#include<stdio.h>
int show_digit(int x);
int main(void)
{
int x;
printf("Enter the variable:");
scanf("%d", &x);
printf("%d ", show_digit(x));
return 0;
}
int show_digit(int x)
{
return show_digit(x / 10)%10;
}
You seemed to be trying to make show_digit() recursive, but you got stuck on how to actually do it. This refactored version of your function actually traverses from the last digit of the input to the first (which is the base case), and then starts printing out spaced digits as it comes out of the recursion. Note that I changed the return type of show_digits() to void because it is really now a utility function which does not compute anything.
void show_digit(int x)
{
if (x < 10)
{
printf("%d ", x);
return;
}
else
{
show_digit(x/10);
}
int the_digit = x % 10;
printf("%d ", the_digit);
return;
}
In this answer, I will not show you any code, but I will explain what's your issue.
I tried to compile your code, it compile successfully, but when I run it, the program crashed, is that your issue?
I know why your program crashed: Out of memory!
How did it happen?
Your function int show_digit(int) is a recursive function, in this function, you didn't made a statement to stop recursion, so, the recursion will continue until the function's stack reach maximum size of allowed memory, then it finally causes crashing.
Non-recursive version:
int digits[256];
int i = 0;
do {
digits[i++]=x%10;
x /= 10;
} while (x);
for (i=i-1; i>=0; i--) {
printf ("%d ", digits[i]);
}
It will work fine for positive integers.