[英]Storing Fibonacci numbers in an array (C)
Can't get my program to output the correct number. 无法让我的程序输出正确的数字。 I feel like I am making a simple mistake.
我觉得我在犯一个简单的错误。 This is written in C.
这是用C编写的。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i;
int list[n];
while(1)
{
scanf("%d", &n);
if(n == -1)
{
break;
}
else
{
for(i = 2; i < n; i++)
{
list[i] = list[i-1]+list[i-2];
}
printf("%d %d", i, list[i] );
}
}
}
(To make things simpler, I'm going to ignore dealing with input.) (为简化起见,我将忽略处理输入。)
First problem is turning on compiler warnings. 第一个问题是打开编译器警告。 Most C compilers don't give you warnings by default, you have to ask for them.
默认情况下,大多数C编译器都不给您警告,而您必须要求它们。 Usually by compiling with
-Wall
. 通常通过
-Wall
进行编译。 Once we do that, the basic problem is revealed. 一旦我们做到了,基本问题就会暴露出来。
test.c:6:14: warning: variable 'n' is uninitialized when used here [-Wuninitialized]
int list[n];
^
test.c:5:10: note: initialize the variable 'n' to silence this warning
int n, i;
^
= 0
1 warning generated.
int list[n]
immediately creates a list of size n. int list[n]
立即创建大小为n的列表。 Since n
is uninitialized it will be garbage. 由于
n
未初始化,因此将是垃圾。 You can printf("%d\\n", n);
您可以
printf("%d\\n", n);
and see, it'll be something like 1551959272. 会看到像1551959272这样的东西。
So either n
needs to be initialized, or you need to reallocate list
dynamically as n
changes. 因此,要么需要初始化
n
,要么需要随着n
变化动态地重新分配list
。 Dynamic allocation and reallocation gets complicated, so let's just make it a static size. 动态分配和重新分配变得很复杂,因此让我们将其设为静态大小。
So we get this. 所以我们得到了。
#include <stdio.h>
#include <stdlib.h>
int main() {
/* Allocate an array of MAX_N integers */
const int MAX_N = 10;
int list[MAX_N];
/* Do Fibonacci */
for(int i = 2; i < MAX_N; i++) {
list[i] = list[i-1]+list[i-2];
}
/* Print each element of the list and its index */
for( int i = 0; i < MAX_N; i++ ) {
printf("%d\n", list[i]);
}
}
That runs, but we get nothing but zeros (or garbage). 那可以运行,但是我们只能得到零(或垃圾)。 You have a problem with your Fibonacci algorithm.
您的斐波那契算法有问题。 It's
f(n) = f(n-1) + f(n-2)
with the initial conditions f(0) = 0
and f(1) = 1
. 它是
f(n) = f(n-1) + f(n-2)
,初始条件为f(0) = 0
和f(1) = 1
。 You don't set those initial conditions. 您无需设置这些初始条件。
list
is never initialized, so list[0]
and list[1]
will contain whatever garbage was in that hunk of memory. list
从未被初始化,因此list[0]
和list[1]
将包含该大块内存中的所有垃圾。
#include <stdio.h>
#include <stdlib.h>
int main() {
/* Allocate an array of MAX_N integers */
const int MAX_N = 10;
int list[MAX_N];
/* Set the initial conditions */
list[0] = 0;
list[1] = 1;
/* Do Fibonacci */
for(int i = 2; i < MAX_N; i++) {
list[i] = list[i-1]+list[i-2];
}
/* Print each element of the list and its index */
for( int i = 0; i < MAX_N; i++ ) {
printf("%d\n", list[i]);
}
}
Now it works. 现在可以了。
0 0
1 1
2 1
3 2
4 3
5 5
6 8
7 13
8 21
9 34
You don't have return in main function. 您没有main函数的return。
n must be defined previous. n必须事先定义。 Otherwise it took random value from memory.
否则,它将从内存中获取随机值。 So, your list array is created with unknown value.
因此,您创建的列表数组的值未知。
int list[n];
Also, this will never happends, becous n is declared, but not defined. 同样,绝不会发生这种情况,因为已声明但未定义n。
i < n;
Is this what you need? 这是您需要的吗?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int F[100];
F[0] = 0;
F[1] = 1;
int i = 2;
while(1)
{
if(i < 100)
{
F[i] = F[i-1] + F[i-2];
i++;
}
else
{
break;
}
}
i = 0;
while(1)
{
if(i < 100)
{
printf("%d ; ", F[i]);
i++;
}
else
{
break;
}
}
return 0;
}
Here is code snippet, 这是代码片段,
#include <stdio.h>
int main()
{
int MAX_SIZE = 100; //Initial value
int n, i;
int list[MAX_SIZE];
printf("Enter value of 'n'");
scanf("%d",&n);
if(n < 0){
printf("'n' cannot be negative number");
return 0;
}else if (n==1){
list[0]=0;
}else if(n == 2){
list[0]=0;
list[1]=1;
}else{
list[0]=0;
list[1]=1;
for(i = 2; i <= n; i++)
{
list[i] = list[i-1]+list[i-2];
}
}
//To view array elements
for(int i=0;i<n;i++){
printf("%3d",list[i]);
}
}
You need to allocate memory on demand for each iteration. 您需要根据需要为每个迭代分配内存。 In your code, n is uninitalized which leads to unpredectiable behavior.
在您的代码中,n是未初始化的,这导致无法预测的行为。 Also you need to initialize
list[0]
and list[1]
since this is the 'base' case. 您还需要初始化
list[0]
和list[1]
因为这是“基本”情况。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i;
int* list; /* Declare a pointer to the list */
while(1)
{
scanf("%d", &n);
if(n == -1)
{
break;
}
else if ( n > 0 )
{
list = (int *) malloc( n * sizeof(int) );
list[0] = 1;
list[1] = 1;
for(i = 2; i < n; i++)
{
list[i] = list[i-1]+list[i-2];
}
printf("%d %d\n", i, list[i-1] );
free(list);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.