0

As part of a problem, I need to create a recursive function on a string.
Part of it is based on the nth element of Fibonacci.
The thing is that I need to get n ( the number of elements ) as an input, and only then I can create the array.
But I also need to create a recursive function that has to use the array.
I thought of adding the array to the function as a parameter but I'm not sure if that would decrease the function's efficiency.
So in case it would: Is there any way I can use that specific array in a function in c++? I calculate the array like this:

#include <bits/stdc++.h>
using namespace std;



char IOI(int n,int k) {
    if (n<3) {
        return n;
    }
    if (k<=fibo[n-2]) {
        return IOI(n-2,k);
    }
    else {
        return IOI(n-1,k-fibo[n-2]);
    }

}

int main() {
    int n,k;
    cin >> n >> k;

    int fibo[n+1];
    fibo[0] = 0;
    fibo[1] = 1;
    for (int i = 2; i<n; i++) {
        fibo[i] = fibo[i-1]+fibo[i-2];
    }

    cout << IOI(n,k)


  • Need types for your function arguments too. Just define your function argument as array, for ex.) fn(int array[]), and you're fine. Oh and be sure to pass size too, because it's actually just passing int*. If you want to loop through array, you need to pass size of array. – Asphodel Jan 27 '22 at 18:12
  • Yeah but wouldn't it be slow to pass the array as a parameter every time I recall the function? –  Jan 27 '22 at 18:13
  • it's passing int* actually, if your array is of ints of course – Asphodel Jan 27 '22 at 18:14
  • Looks like you are coming into C++ from a different language or trying to learn C++ from the code posted on competition sites. This is a tough road to travel. Different languages do things differently and the goal of competition code is to win, not teach. I strongly recommend learning the fundamentals from [a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) first. – user4581301 Jan 27 '22 at 18:15
  • Regarding `int fibo[n+1];`: [[Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) - Other good posts: [Why should I **not** `#include `?](https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H.) and possibly [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Ted Lyngmo Jan 27 '22 at 18:15
  • Arrays are never passed by value (copied) they are always passed by reference ([What is array to pointer decay?](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay)). This makes returning an array from a function tricky. – user4581301 Jan 27 '22 at 18:19
  • @user4581301 "*Arrays are never passed by value (copied)*" - to be more accurate, C-style arrays are always passed around by pointer. But C++-style `std::array` can be passed around by value and it will be copied. – Remy Lebeau Jan 27 '22 at 18:29
  • 2
    you never need to include bits/anything – pm100 Jan 27 '22 at 19:08

1 Answers1

1

Is there any way I can use that specific array in a function in c++?

Yes. Pass the array as an argument, using some form of indirection. Typically, this would be done using a parameter of type span.


cin >> n >> k;
int fibo[n+1];

This isn't allowed. The size of an array variable must be compile time constant in C++.

In order to use an array with dynamic size, the array must be allocated dynamically. The most convenient solution is to use std::vector.

eerorika
  • 232,697
  • 12
  • 197
  • 326