The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
i am having a struggle with understanding this code properly. Is whatIsThis a proper name for the class on functions of the program or should it be something else. I have not been able to manage to get the program compiled with the source codeÂ
//Ex. 7.18: Ex07_18.cpp
//What does this program do?
#include <iostream>
using std::cout;
using std::endl;
int whatIsThis(int[], int); // This is the function prototype which tells the
// compiler that program would have the definition of
// a function named whatIsThis and would have the parameters
// integer array and an integer value
Â
int main()Â Â Â // main function
Â
{
      const int arraySize = 10; // declares a constant integer variable and initializes it to 10
      int a[arraySize] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // declares an array a with size arraySize and initializes it
Â
      int result = whatIsThis(a, arraySize);  // it calls the function whatIsThis and passes array a and arraySize values and receives
      // an integer value from the function
Â
      cout << "Results is " << result << endl; // prints the result to the console output
      return 0; //indicates successful termination
} // end main
Â
// This function recursively finds the sum of the elements in the arrau
int whatIsThis(int b[], int size)
{
      if (size == 1) // base case, at this case it will terminate the recursive function
             return b[0];
      else // recursive step, at this it would call itself again recursively to add the next element into the sum
             return b[size - 1] + whatIsThis(b, size - 1);
} // end function whatIsThis
-----------