I am working with an array of 10 numbers and I have already read them in and printed them out. The two functions below are the ones I am having trouble with. This is what the input looks like: 3 7 9 3 8 2 10 11 4 14
Write a recursive int function ”sumOfInt“ with an intger parameter(n) which calculates the sum of 1 to n(n is the last value of the in intArry).
So for the array above the output should be: SUM of 1..14: 105
Write a recursive int function ”Power“ with two integer parameters(x & n) which calculate x power of n (x is the 1st element of the array & n is the 2nd element of the array).
So for the array above the output should be: 3 power of 7: 2187
I believe the problem I am having is the way I am calling the functions in my main and possibly the functions themselves so this is my main and the functions that are being called.
#include "recur.h"
int main()
{
ifstream inFile;
ofstream outFile;
inFile.open("in.data");
outFile.open("out.data");
if(inFile.fail() || outFile.fail())
{
outFile << "Input or Output file ERROR!" << endl;
}
int first = FIRST;
int last = LAST;
int intArray[MAX_SIZE];
int sum;
int x;
int n;
int power;
readToArray(intArray, inFile);
outFile << "Array: ";
printArray(outFile, intArray, first, last);
outFile << endl;
outFile << "Array in reverse order: ";
printArrayRev(intArray, first, last, outFile);
outFile << endl;
sum = sumOfInt(n);
outFile << "SUM of " << last << ":" << sum << endl;
power = Power(x, n);
outFile << x << "power of" << n << ":" << power << endl;
return 0;
}
int Power(int x, int n)
//**********************************************
{
int power;
if(n == 0)
return 1;
else
{
power = (x*Power(x, n-1));
return power;
}
}
int sumOfInt(int n)
//*********************************************
{
if(n == 1)
return 1;
else
return(n + sumOfInt(n-1));
}
Thanks for your help!