Computing the nth number of the fibonacci sequence, where n is entered at command line
I want to write a program to compute the nth number of the fibonnacci sequence, which i had done using printf and scanf. But I was hoping to change my program so that the sequence number is entered at the command li开发者_Go百科ne rather than entered when prompted by the program. This is what i've come up with. It compiles, but then it crashes when i run it... not sure why. Any suggestions would be appreciated.
This is a program to compute the nth number of the fibonnacci code using iteration. I have written it as such: You must enter the number of the sequence you wish to compute at the command line argv[1]. The program then takes this command line argument and uses it in the while loop, and also prints this number.
#include <stdio.h>
int main( int argc, char**argv ) {
int fib[3] = {0,1};
int counter = 0;
printf("The %dth Fibonacci number is:\n", atoi(argv[1]));
while ( counter < atoi(argv[1]) ) {
fib[2] = fib[0] + fib[1];
fib[0] = fib[1];
fib[1] = fib[2];
counter++;
}
printf("%d\n", fib[0]);
getchar();
return 0;
}
Check if the user actually passed an argument:
int main( int argc, char**argv ) {
if (argc < 2) {
printf("Usage: %s number\n", argv[0]);
return 1;
}
...
}
If he didn't, argv[1]
is null, and you'll crash
The program should check if a command line argument is present:
if (argc < 2)
{
printf ("usage: %s n\n where n is a positive integer\n", argv[0]);
return 1;
}
If no argument is supplied, it will probably crash.
= = = = = edit = = = = =
I don't see any cause for a crash once the bug above is fixed. This works okay:
#include <stdio.h>
int main( int argc, char**argv )
{
int fib[3] = {0,1};
int counter = 0;
if (argc < 2)
{
printf ("usage: %s number\n", argv[0]);
return 1;
}
printf("The %dth Fibonacci number is:\n", atoi(argv[1]));
while ( counter < atoi(argv[1]) )
{
fib[2] = fib[0] + fib[1];
fib[0] = fib[1];
fib[1] = fib[2];
counter++;
}
printf("%d\n", fib[0]);
return 0;
}
[wally@zenetfedora ~]$ ./a.out
usage: ./a.out number
[wally@zenetfedora ~]$ ./a.out 3
The 3th Fibonacci number is:
2
[wally@zenetfedora ~]$ ./a.out 4
The 4th Fibonacci number is:
3
[wally@zenetfedora ~]$ ./a.out 5
The 5th Fibonacci number is:
5
[wally@zenetfedora ~]$ ./a.out 6
The 6th Fibonacci number is:
8
[wally@zenetfedora ~]$ ./a.out 7
The 7th Fibonacci number is:
13
[wally@zenetfedora ~]$ ./a.out 8
The 8th Fibonacci number is:
21
[wally@zenetfedora ~]$ ./a.out 9
The 9th Fibonacci number is:
34
[wally@zenetfedora ~]$ ./a.out 10
The 10th Fibonacci number is:
55
What number did you test it with? Because an int won't be able to hold anything higher than the ~50th Fibonacci number. I had to do something like this in my Algorithms class and we had to find the 239th Fibonacci number which was 64202014863723094126901777428873111802307548623680
Which is something an int nor a long can hold correctly.I don't think that would make it crash but just giving a heads up if you need to find a large Fibonacci number.
#include<stdio.h>
#include<stdlib.h>
int main(int c,char *v[])
{
int *numberOfTerms;
int x,y,z,i,count;
if(c==1)
{
printf("provide number of terms as command line argument");
return 0;
}
numberOfTerms=(int *)malloc(sizeof(int));
*numberOfTerms=atoi(v[1]);
x=0;
y=1;
if(*numberOfTerms==0) return 0;
if(*numberOfTerms<=2)
{
for(i=0;i<*numberOfTerms;i++) printf("%d\n",i);
return 0;
}
printf("%d\n",x);
printf("%d\n",y);
count=2;
while(count<*numberOfTerms)
{
z=x+y;
printf("%d\n",z);
x=y;
y=z;
count++;
}
}
If you want to print the series upto a certain number specified as command line argument, then go for this program:
#include<stdio.h>
#include<stdlib.h>
int main(int c,char *v[])
{
int *number;
int x,y,z;
if(c==1)
{
printf("provide number upto which febonacci is to be printed (greater than 2)");
return 0;
}
number=(int *)malloc(sizeof(int));
*number=atoi(v[1]);
x=0;
y=1;
printf("%d\n",x);
printf("%d\n",y);
z=x+y;
while(z<=*number)
{
printf("%d\n",z);
x=y;
y=z;
z=x+y;
}
}
精彩评论