This is my whole program and what I am trying to do. Nothing is being outputted through the functions though. When I output the array in the client code it prints correctly but not through the recursive function. So I am wondering what I am doing incorrectly?
Input is like this: 3 7 9 3 8 2 10 11 4 14
The Sum function adds up from 1 all the way to the last number in the array so the sum of 1+2+3+4+5...+14=105.
Output should look like this:
Array: 3 7 9 3 8 2 10 11 4 14
Array in reverse order:14 4 11 10 2 8 3 9 7 3
SUM of 1-14: 105
3 power of 7: 2187
#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;
int last;
int intArray[MAX_SIZE];
int sum;
int x;
int n;
int power;
readToArray(intArray, inFile);
printArray(intArray, first, last, outFile);
printArrayRev(intArray, first, last, outFile);
outFile << "SUM of 1-10:" << sum << endl;
outFile << x << "power of" << n << ":" << power << endl;
return 0;
}
//recur.cxx
#include "recur.h"
void readToArray(int intArray[], ifstream& inFile)
{
int i;
for(i = 0; i < MAX_SIZE; i++)
{
inFile >> intArray[i];
}
}
int Power(int x, int n)
{
int power;
if(n == 0)
return 1;
else
{
power = (x*Power(x, n-1));
return power;
}
}
void printArray(const int intArray[], int first, int last, ofstream& outFile)
{
outFile << "Array:";
if(first <= last)
{
outFile << intArray[first] << " ";
printArray(intArray, first + 1, last, outFile);
}
}
void printArrayRev(const int intArray[], int first, int last, ofstream& outFile)
{
outFile << "Array in reverse order:";
if(last >= first)
{
outFile << intArray[first] << " ";
printArrayRev(intArray, first, last -1, outFile);
}
}
int sumOfInt(int n)
{
int sum;
if(n > 0)
{
sum = n + sumOfInt(n-1);
return sum;
}
else return 0;
}
//recur.h
#include <iostream>
#include <iomanip>
#include <fstream>
const int MAX_SIZE = 10;
using namespace std;
void readToArray(int intArray[], ifstream& inFile);
int Power(int x, int n);
void printArray(const int intArray[], int first, int last, ofstream& outFile);
void printArrayRev(const int intArray[], int first, int last, ofstream& outFile);
int sumOfInt(int n);
Thanks for all of your help.