0

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

?

tekknolagi
  • 10,663
  • 24
  • 75
  • 119

4 Answers4

3

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
}
Asha
  • 11,002
  • 6
  • 44
  • 66
2

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

Jeremiah Willcock
  • 30,161
  • 7
  • 76
  • 78
0

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.

Cristian Toader
  • 96
  • 1
  • 1
  • 5
-1
//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.