944,007 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 10894
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 19th, 2007
0

C++ Recursion: Sum Array

Expand Post »
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...

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int arraySum(const int formalAray[], int lower, int upper);
  7.  
  8. int main()
  9. {
  10. int list[10];
  11.  
  12. cout << "Please enter 10 numbers to calculate their sum... " << endl
  13. << endl << endl;
  14.  
  15. for(int i = 0; i < 10; i++)
  16. cin >> list[i];
  17.  
  18. cout << endl << endl << "The numbers you entered are: " << endl;
  19.  
  20. for(int i = 0; i < 10; i++)
  21. cout << list[i] << ' ';
  22.  
  23. cout << endl << endl;
  24.  
  25. cout << "The sum of the ten numbers you entered is: " << arraySum(list, 0, 9) << endl
  26. << endl << endl;
  27.  
  28. return 0;
  29. }
  30.  
  31. int arraySum(const int formalArray[], int lower, int upper)
  32. {
  33. int sum = 0, temp;
  34.  
  35. if(lower == upper)
  36. return formalArray[lower];
  37. else
  38. return sum + arraySum(formalArray, lower + 1, upper);
  39. }
Last edited by Nick Evan; May 25th, 2010 at 11:49 am. Reason: added code-tags
Similar Threads
Reputation Points: 16
Solved Threads: 1
Light Poster
xander85 is offline Offline
29 posts
since Jul 2007
Jul 19th, 2007
0

Re: C++ Recursion: Sum Array

Click to Expand / Collapse  Quote originally posted by xander85 ...
Hello all,


C++ Syntax (Toggle Plain Text)
  1.  
  2. int arraySum(const int formalArray[], int lower, int upper)
  3. {
  4. int sum = 0, temp;
  5.  
  6. if(lower == upper)
  7. return formalArray[lower];
  8. else
  9. return sum + arraySum(formalArray, lower + 1, upper);
  10. }
Hi dear,

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

C++ Syntax (Toggle Plain Text)
  1. return sum + arraySum(formalArray, lower + 1, upper);

I don't think
C++ Syntax (Toggle Plain Text)
  1. sum
is at all needed. replace it with
C++ Syntax (Toggle Plain Text)
  1. formalArray[lower];
that's it. there you go.

Happy coding
Reputation Points: 64
Solved Threads: 7
Junior Poster
shouvik.d is offline Offline
198 posts
since Jan 2007
Jul 19th, 2007
0

Re: C++ Recursion: Sum Array

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

Re: C++ Recursion: Sum Array

Click to Expand / Collapse  Quote originally posted by xander85 ...
Wow, that was easy! I do not know what I was thinking... Thanks!
U were thinking abt whom ?
Reputation Points: 64
Solved Threads: 7
Junior Poster
shouvik.d is offline Offline
198 posts
since Jan 2007
Nov 11th, 2009
-1
Re: C++ Recursion: Sum Array
hy this cod also help me in my assigmnt!!!!!!!!!!!!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dia ali is offline Offline
1 posts
since Nov 2009
May 17th, 2010
0
Re: C++ Recursion: Sum Array
4 students just copied this code and lost all their points for the assignment.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Dumbledor is offline Offline
2 posts
since May 2010
May 18th, 2010
0
Re: C++ Recursion: Sum Array
Click to Expand / Collapse  Quote originally posted by Dumbledor ...
4 students just copied this code and lost all their points for the assignment.
Out of curiosity... which school did this happen at?
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
May 18th, 2010
0
Re: C++ Recursion: Sum Array
Click to Expand / Collapse  Quote originally posted by Dumbledor ...
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.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
May 18th, 2010
0
Re: C++ Recursion: Sum Array
Great, now at least they will learn something from their copied code
Last edited by Ezzaral; May 18th, 2010 at 12:40 pm.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
May 18th, 2010
0
Re: C++ Recursion: Sum Array
Thank you for the support. It is a nasty job but some one must do it...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Dumbledor is offline Offline
2 posts
since May 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: Past Exam Question : Strings
Next Thread in C++ Forum Timeline: How to stop the output window in VS 2010?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC