954,136 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ Recursion: Sum Array

Hello all,

I am trying to finish an assignment in my programming class and I can't seem to get this compilation to work... For some strange reason the program will not sum the elements in the array... It is only returning the first input that I enter... Any hints??? I know that a for loop would make more sense, but the assignment asks for recursion to be used...

#include <iostream>

using namespace std;

int arraySum(const int formalAray[], int lower, int upper);

int main()
{
	int list[10];

	cout << "Please enter 10 numbers to calculate their sum... " << endl
		<< endl << endl;

	for(int i = 0; i < 10; i++)
		cin >> list[i];

	cout << endl << endl << "The numbers you entered are: " << endl;

	for(int i = 0; i < 10; i++)
		cout << list[i] << ' ';

	cout << endl << endl;

	cout << "The sum of the ten numbers you entered is: " << arraySum(list, 0, 9) << endl
		<< endl << endl;

	return 0;
}

int arraySum(const int formalArray[], int lower, int upper)
{	
	int sum = 0, temp;

	if(lower == upper)
		return formalArray[lower];
	else
		return sum + arraySum(formalArray, lower + 1, upper);
}
xander85
Light Poster
29 posts since Jul 2007
Reputation Points: 16
Solved Threads: 1
 

Hello all,

int arraySum(const int formalArray[], int lower, int upper)
{    
    int sum = 0, temp;
 
    if(lower == upper)
        return formalArray[lower];
    else
        return sum + arraySum(formalArray, lower + 1, upper);
}



Hi dear,

You have written a good code an efficirnt one indeed I just don't get how could you miss the silly mistake.

return sum + arraySum(formalArray, lower + 1, upper);


I don't think

sum


is at all needed. replace it with

formalArray[lower];


that's it. there you go.

Happy coding :)

shouvik.d
Junior Poster
198 posts since Jan 2007
Reputation Points: 64
Solved Threads: 7
 

Wow, that was easy! I do not know what I was thinking... Thanks!

xander85
Light Poster
29 posts since Jul 2007
Reputation Points: 16
Solved Threads: 1
 
Wow, that was easy! I do not know what I was thinking... Thanks!



U were thinking abt whom ;) ?

shouvik.d
Junior Poster
198 posts since Jan 2007
Reputation Points: 64
Solved Threads: 7
 

hy this cod also help me in my assigmnt!!!!!!!!!!!!!!

dia ali
Newbie Poster
1 post since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

4 students just copied this code and lost all their points for the assignment.

Dumbledor
Newbie Poster
4 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 
4 students just copied this code and lost all their points for the assignment.

Out of curiosity... which school did this happen at?

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 
4 students just copied this code and lost all their points for the assignment.


Excellent. More teachers should use google to find ctrl-v-code.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Great, now at least they will learn something from their copied code :)

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Thank you for the support. It is a nasty job but some one must do it...

Dumbledor
Newbie Poster
4 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 
int arraySum(const int formalArray[], int lower, int upper)
{
int sum = 0, temp;
sum+=formalArray[lower];
if(lower >= upper)
return sum;
else
return arraySum(formalArray, lower + 1, upper);
}


try this...

LoganJames
Newbie Poster
16 posts since May 2010
Reputation Points: 4
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You