C++ Recursion: Sum Array

Reply

Join Date: Jul 2007
Posts: 29
Reputation: xander85 is an unknown quantity at this point 
Solved Threads: 1
xander85's Avatar
xander85 xander85 is offline Offline
Light Poster

C++ Recursion: Sum Array

 
0
  #1
Jul 19th, 2007
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);
}
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 200
Reputation: shouvik.d is an unknown quantity at this point 
Solved Threads: 6
shouvik.d's Avatar
shouvik.d shouvik.d is offline Offline
Posting Whiz in Training

Re: C++ Recursion: Sum Array

 
0
  #2
Jul 19th, 2007
Originally Posted by xander85 View Post
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 is at all needed. replace it with
formalArray[lower];
that's it. there you go.

Happy coding
Regards
Shouvik
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 29
Reputation: xander85 is an unknown quantity at this point 
Solved Threads: 1
xander85's Avatar
xander85 xander85 is offline Offline
Light Poster

Re: C++ Recursion: Sum Array

 
0
  #3
Jul 19th, 2007
Wow, that was easy! I do not know what I was thinking... Thanks!
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 200
Reputation: shouvik.d is an unknown quantity at this point 
Solved Threads: 6
shouvik.d's Avatar
shouvik.d shouvik.d is offline Offline
Posting Whiz in Training

Re: C++ Recursion: Sum Array

 
0
  #4
Jul 19th, 2007
Originally Posted by xander85 View Post
Wow, that was easy! I do not know what I was thinking... Thanks!
U were thinking abt whom ?
Regards
Shouvik
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: dia ali is an unknown quantity at this point 
Solved Threads: 0
dia ali dia ali is offline Offline
Newbie Poster
 
-1
  #5
23 Days Ago
hy this cod also help me in my assigmnt!!!!!!!!!!!!!!
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC