返回 C 中斐波那契数列的特定数字

发布于 2024-10-19 04:44:38 字数 497 浏览 5 评论 0原文

我正在编写一个 C 程序来计算斐波那契序列中的特定数字,尽管我无法将序列作为数组返回......

我做错了什么?

int fibonacci(int ceiling)
{
  int counter;
  int num1 = 1, num2 = 1;
  static int fibArray[1000];
  for (counter = 1; counter < ceiling; counter+=2)
    {
      fibArray[counter] = num1;
      fibArray[counter+1] = num2;
      num2 += num1;
      num1 += num2;
    }
  return &(fibArray);
}

我也收到错误:

fibonacci.c:28: warning: return makes integer from pointer without a cast

I'm writing a C program that calculates a specific number in the fibonacci sequence, though I'm having trouble returning the sequence as an array....

What am I doing wrong?

int fibonacci(int ceiling)
{
  int counter;
  int num1 = 1, num2 = 1;
  static int fibArray[1000];
  for (counter = 1; counter < ceiling; counter+=2)
    {
      fibArray[counter] = num1;
      fibArray[counter+1] = num2;
      num2 += num1;
      num1 += num2;
    }
  return &(fibArray);
}

I also get the error:

fibonacci.c:28: warning: return makes integer from pointer without a cast

?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

聽兲甴掵 2024-10-26 04:44:38

由于您要返回指向数组的指针,因此返回类型应为 int*。这是示例代码:

int* fibonacci(int ceiling) //Modified return type
{
  int counter;
  int num1 = 1, num2 = 1;
  static int fibArray[1000];
  for (counter = 1; counter < ceiling; counter+=2)
    {
      fibArray[counter] = num1;
      fibArray[counter+1] = num2;
      num2 += num1;
      num1 += num2;
    }
  return (fibArray); //Return the address of the array's starting position
}

Since you are returning a pointer to an array, the return type should be int*. Here is sample code:

int* fibonacci(int ceiling) //Modified return type
{
  int counter;
  int num1 = 1, num2 = 1;
  static int fibArray[1000];
  for (counter = 1; counter < ceiling; counter+=2)
    {
      fibArray[counter] = num1;
      fibArray[counter+1] = num2;
      num2 += num1;
      num1 += num2;
    }
  return (fibArray); //Return the address of the array's starting position
}
难忘№最初的完美 2024-10-26 04:44:38

您想从 fibArray 返回一个元素,对吗?在这种情况下,请使用 return fibArray[...];,其中 ... 是元素索引。

You want to return one element from fibArray, correct? In that case, use return fibArray[...];, where ... is the element index.

祁梦 2024-10-26 04:44:38

你不需要数组。您只需要两个整数来保存当前值和先前值。然后你做:

newValue = previousValue + newValue;
previousValue = newValue - previousValue;`

你应该被设置。当达到限制时,只需返回 newValue 即可。

如果您确实想返回整个数组(您说的是“计算斐波那契序列中的特定数字”,这不是整个序列)。将函数的返回类型设置为 int* 并使用数组。

You don't need an array. You just need two integers in which you hold current value and previous value. You then make:

newValue = previousValue + newValue;
previousValue = newValue - previousValue;`

and you should be set. When the limit is being hit, just return the newValue.

If you do want to return the whole array (you are saying "calculates a specific number in the fibonacci sequence" which is not the whole sequence). Make the return type of your function int* and use the array.

蓝天白云 2024-10-26 04:44:38
//you dont need all the "includes" I have in here. These are just a basic format I work with. This should get you going if you arent going already. cheers. :) 

#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<cmath>
#include<stdio.h>
#include<cctype>
#include<list>
#include<string>

using namespace std;
int main()
//-------------------------------declaration of variables----------------------------------
{
   int num1, num2;
   int initial_value, final_value;
//-----------------------------------------inputs------------------------------------------
   cout << "What is your first number? :";
   cin  >> num1;
   initial_value = num1;
   cout << "What is your second number? :";
   cin >> num2;
   final_value = num2;
//-----------------------------------------dasloop----------------------------------------
   do
   {
       final_value = initial_value + final_value;
       initial_value = final_value - initial_value;
       cout  <<  final_value  <<  endl;
   }
   while(final_value <= 1000);

//---------------------------exits perfectly when greater than 1000------------------------
   cout << endl << endl;
   system("pause");
   return 0;
}
//I have it exit at 1000 because its a nice round number that allows you enough room
//to see the code, and sequence are both correct. 
//you dont need all the "includes" I have in here. These are just a basic format I work with. This should get you going if you arent going already. cheers. :) 

#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<cmath>
#include<stdio.h>
#include<cctype>
#include<list>
#include<string>

using namespace std;
int main()
//-------------------------------declaration of variables----------------------------------
{
   int num1, num2;
   int initial_value, final_value;
//-----------------------------------------inputs------------------------------------------
   cout << "What is your first number? :";
   cin  >> num1;
   initial_value = num1;
   cout << "What is your second number? :";
   cin >> num2;
   final_value = num2;
//-----------------------------------------dasloop----------------------------------------
   do
   {
       final_value = initial_value + final_value;
       initial_value = final_value - initial_value;
       cout  <<  final_value  <<  endl;
   }
   while(final_value <= 1000);

//---------------------------exits perfectly when greater than 1000------------------------
   cout << endl << endl;
   system("pause");
   return 0;
}
//I have it exit at 1000 because its a nice round number that allows you enough room
//to see the code, and sequence are both correct. 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文