Question: Explain what the following program does.

``````#include <iostream>
using namespace std;

int whatIsThis( int [], int);

int main()
{
const int arraySize = 10;
int a[arraySize] = {1,2,3,4,5,6,7,8,9,10};

int result = whatIsThis(a, arraySize);

cout << “Result is “ << result << endl;
return 0;
}

//what does this function do?
int whatIsThis(int b[], int size)
{
if (size == 1)
return b[0];
else
return b + whatIsThis (b, size – 1);
}

---------------------------------------------------

Here are my answer.... plz check am i correct?

Explain what the following program does.

My answer: FOUND OUT TOTAL VALUE OF MULTIDIMENSIONAL ARRAY

#include <iostream>
using namespace std;

int whatIsThis( int [], int);   // PROTOTYPE < whatIsThis  - ARRAY SEARCH >
int main()
{
const int arraySize = 10;   // SIZE OF ARRAY
int a[arraySize] = {1,2,3,4,5,6,7,8,9,10};   // CREATE ARRAY

int result = whatIsThis(a, arraySize);

cout << “Result is “ << result << endl;
return 0;
}
//what does this function do?>> //COMPARE KEY TO EVERY ELEMENT OF ARRAY UNTIL LOCATION IS FOUND OR UNTIL END ARRAY REACHED AND THEN SUM THE AMOUNT OF TOTAL ARRAYS FOUND. // <<int whatIsThis(int b[], int size)
{
if (size == 1)
return b[0];
else
return b + whatIsThis (b, size – 1);
}
``````

I think you are correct, the function determines the sum of the values in the array passed to it. In C/C++ the function whatIsThis() is called a recursive function. Recursive functions can be very useful.

ok..did u have any suggestion on my answer...maybe i should put more..or else...

Just pay attention to the structure of whatIsThis(). Note how it keeps calling itself until a specified value is obtained, at which time it stops. Those features are what make it a recursive function. That knowledge will be more valuable in the future than determining what the function actually accomplishes. If your lecturer hasn't discussed recursion yet pay attention when they do, referencing this exercise as they go along.

Be a part of the DaniWeb community

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