Programming question

Hello. I am a beginner in programming and wanted to know if someone can help. I have no idea what I am doing or where to even start. If anyone can help me on where to begin I would appreciate it. Here is the assignment.

What does the following program do?. Copy the sample program and compile it, then find the results. Submit a report which is more than one page length to explain how the program works and how you can improve it.

The problem is I don't even know what is wrong with the program? I understand that it adds up all the numbers but am I supposed to put in the program that it is supposed to add up the numbers?

#include <iostream>
using std::cout;
using std::endl;

int whatIsThis( int [], int ); //funtion prototype

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; //indicates succesful termination
} // end main

// What does this function do?
int whatIsThis( int b[], int size)
{
if ( size == 1 ) // base case
return b [ 0 ];
else // recursive step
return b [ size - 1 ] + whatIsThis( b, size - 1 );
} //end function whatIsThis
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/176922/
http://www.cplusplus.com/forum/windows/176924/
http://www.cplusplus.com/forum/general/176928/
Last edited on
recursive function ( it calls itself) basically it calculat the sum of all element in the array and put it in the first element the. return the value of the first elemtn( the sum)

Last edited on
Ok. Thank you. If I understand you correctly this program no matter what numbers I enter, it will calculate the sum? So I don't have to write out each number?
closed account (48T7M4Gy)
try int a[ arraySize ] = { 112, 107, -219, 1 };
closed account (48T7M4Gy)
Not now. But please avoid multiple postings on the same question. Once is enough to get a reply.

I hope you tried the suggestion with changing the numbers.
I played around with the numbers and even changed the size. I changed the 10 to a 5. I used kemort's example in which after I ran the program I got a 1. I put in my own numbers and got the same results too.

int main()
{
const int arraySize = 5;
int a[ arraySize] = { 112, 107, -219, 1 };
int result = whatIsThis( a, arraySize);

cout << "Result is" << result << endl;
return 0; //indicates succesful termination
} // end main

int main()
{
const int arraySize = 10;
int a[ arraySize] = { 200, 150, -350, 1 };
int result = whatIsThis( a, arraySize);

cout << "Result is" << result << endl;
Ok will do. I never used this forum before. Thank so much for the feedback! Yes I did. I changed several of the numbers to see what kind of results I would get. It seems that if I add the (-) in front of an integer it's going to still provide me with the sum but subtract from the other numbers. When I changed the arraySize I did not see much of a difference unless I did something wrong.
closed account (48T7M4Gy)
Great stuff. The array is a container for the list of numbers and the program goes though each number to give their total. That's why you got 1 as the answer. Try 1,2,3,4 and you should get 10.

Changing arraySize changes the size of the container and not the numbers themselves. So there's nothing much to immediately see. There's a tutorial here on this site where you can look up if you want more info on arrays. :)

PS and putting a - in front of a number makes it negative which has the effect of subtracting the number from the total in this case.
Last edited on
Aahh. Great. Thanks for explaining it that way! Makes much more sense now as I am changing the numbers. I appreciate all of your help! I'm going to work on finishing it up and then can post my final source code. Thanks again :-)
closed account (48T7M4Gy)
We all look forward to the final result. Post it here when you're ready.
Here is what I came up with. It seems to run better. I took your advise about the arrays after I reviewed the tutorial. Hope this is what the professor is looking for :-)

#include <iostream>
using namespace std;

int main()
{

int size;
double sum = 0;
cout << "I'll tell you sum of numbers. \n" ; // user will be prompted to decide how many numbers they want to enter
cout << "\nHow many numbers do you wish to find?"; //user will be prompted to input the number
cin >> size; // will display the user input

// allocates the array
int * array = new int[size];

// first loop - get the data
for (int count=0; count<size; count++)
{
cout << "Enter Number: "; // user will be prompted to enter the first number, second nmbr, etc until max is 10
cin >> array[count]; //inputs the numbers that the user entered
}

// second loop - accumulate the total
for (int count=0; count<size; count++)
sum += array[count]; //calculates all of the numbers that the user enters

cout << "The sum is " << sum << endl; // the results are displayed of all numbers entered


// de-allocate the array
delete [] array;

return 0; // indicates successful termination of the program
}
closed account (48T7M4Gy)
Looks pretty good. One small point, cin doesn't display the user input, your comment on the second cin statement is a better description of cin workings.

I hope you get good marks :)
Topic archived. No new replies allowed.