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;

  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;
      power = (x*Power(x, n-1));
      return power;

int sumOfInt(int n)
 if(n == 1)
    return 1;
    return(n + sumOfInt(n-1));

Thanks for your help!

Recommended Answers

All 5 Replies

You posted what the output SHOULD be. What is it actually (I assume it's garbage of some sort, but what kind of garbage?)

Sorry I forgot to put it. It gives a Segmentation fault when I compile it. The functions that print the arrays still work though, just not the Power and sumOfInt functions.

A seg fault doesn't occur at compile time, only at runtime. I assume you mean you get a seg fault when you run it.

A seg fault where? How far do you get into the program before it seg faults?

Most importantly, does the program even make it to the Power() and sumOfInt() functions or does it crash before that? If it crashes before that, then the problem is before that and you are focusing on the wrong functions.

Provided that you get into the Power() and the sumOfInt() functions, there is no array in those so it's a little hard to seg fault from dereferencing a bad address. What's left is an infinite recursive call (i.e. you keep calling and calling the function and no base condition ever kicks in and you eventually run out of memory on the stack due to thousands or millions of function calls that need to be kept track of. To test that, simply go into the functions and print out the parameters att he top. Make sure you are passing what you think you are to them. If not, there's your problem. If you're calling that function thousands of time, it'll print that out too.

>> The functions that print the arrays still work though, just not the Power and sumOfInt functions.

OK, I missed this part of your post. The rest of my post still stands. Stick some debugging output in there that prints the parameters of the functions and confirm they are what should be passed. You can also print them in main and check them there.

Hint : If you print out a value and it looks completely random (i.e. -672154 or something), it could BE completely random (i.e. uninitialized).

Alright I have tried that and yeah the output I got is random numbers. I'm not sure how to fix this though, I'm not sure what I have to change in my functions or in my main when calling the functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.