Reputation: 502
i've been trying to do a program that counts till the number that the user inputs (f,ex: 10) with recursion.
If the user inputs 10, the program will have to print out: 1,2,3,4,5,6,7,8,9,10.
I'm getting a [1] 49348 segmentation fault ./recs.exe
and i wanted to know why am i getting it to prevent it in the future.
Here is my code:
#include <stdio.h>
int numbers(int n);
int main(int argc, char const *argv[]) {
int number;
int i;
printf("Put a number and the program will do everything for ya.\n");
printf("Number: ");
scanf("%d", &number);
for(i=0;i<=number;i++){
printf("%d, ", numbers(i));
}
}
int numbers(int n){
if(n==0||n==1){
return n;
} else {
return(numbers(n+1)); // I think the problem is here.
}
}
Hope you guys can explain my error and help me understand why it's happening to me to help me avoid this silly mistake. Thanks :)
Upvotes: 1
Views: 2787
Reputation: 12354
You have a few issues in your algorithm.
the for
loop calls the numbers
program multiple times, starting the recursion multiple times. You need to start it only once, right?
you calling recursion with n+1
, actually making every next invocation with an incremental value, which could be incremented indefinitely. Your recursion has no exit and will die with some out of memory or a crash.
To solve the first issue, just instantiate it once.
To print it correctly, you need to have the print statement withing the recursion.
For the second issue, you can do different approaches. The following is the easiest to understand and it passes max
as a second argument.
void numbers(int n, int max) {
printf(%d ", n);
if (n >= max)
return;
numbers (n + 1, max);
}
int main() {
...
numbers(1, number);
}
a more efficient way is having a single argument and count it down to 0. But you have to be careful on when you want to print. In your case, if you need increment order of values, you have to make sure that the print happens after you return from recursion.
Here is the program which implements the second method:
#include <stdio.h>
void numbers(int n);
int main(int argc, char const *argv[]) {
int number;
int i;
printf("Put a number and the program will do everything for ya.\n");
printf("Number: ");
scanf("%d", &number);
// start recursion.
numbers(number);
printf("\n");
}
void numbers(int n){
if (n == 0)
return;
numbers(n-1);
// after return from recursion
printf("%d ", n);
}
Upvotes: 2
Reputation: 4145
As discussed in the comments, your implementation will not stop counting because it has no idea what the target value is. For example, if the user enters '2', the code would run numbers(0)
, which would return 0, then numbers(1)
which would return 1, then numbers(2)
which would call numbers(3)
and so on, until the stack ran out of space.
The following should work according to my understanding of the problem.
void count(int from, int to)
{
printf( "%d", from );
if( from < to )
{
printf( ", ");
count( from+1, to );
}
}
int main(int argc, char const *argv[])
{
int number;
printf("Put a number and the program will do everything for ya.\n");
printf("Number: ");
scanf("%d", &number);
count(1,number);
}
I've moved the printing into the recursive function because it makes it easier to output each number (and intelligently print the comma that comes between numbers), and it didn't make sense to me to use a recursive function and a for
loop to count.
The function simply prints out the current value and then checks to see if it has reached its goal. If not, it recursively counts to the next number.
Upvotes: 1
Reputation: 1280
Your recursion never end if you send a number >= 2, for exemple, if i send 2 to your numbers function, the next call step is 3, the next one is 4, 5, 6 ect....
The correct code is :
int main(int argc, char const *argv[]) {
int number;
int i;
printf("Put a number and the program will do everything for ya.\n");
printf("Number: ");
scanf("%d", &number);
for(i=0;i<=number;i++){
printf("%d, ", i);
}
if you want to do recursion, you can do a countdown, that lends more to the recursion
int main(int argc, char const *argv[]) {
int number;
printf("Put a number and the program will do everything for ya.\n");
printf("Number: ");
scanf("%d", &number);
numbers(number);
}
void numbers(int n)
{
printf("%d, ", n);
if (n > 0)
numbers(n - 1);
}
Upvotes: 1