I have to make a recursive function that will compute the sum of the first n integer in an array of at least n integers. I believe I have the function complete the cout statement however is causing an error. Any help would be appreciated.
#include <iostream>
using namespace std;
int n = 0;
int array[];
int sum(int array[], int n);
int main()
{
cout << sum(array, 4)<< endl;
return 0;
}//end main
int sum(int array[], int n)
{
if(n <= 0)
{
return 0;
}
else
{
return array[0] + sum(array + 1, n-1);
}
}// end Compute
Here is the error:
fatal error LNK1120: 1 unresolved externals
Your variable
arrayis never defined anywhere andint array[];is not valid syntax (but for some reason your compiler is treating it as anexterndeclaration or something). Change the invalid syntax to define it: