| | |
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);
}
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);
}
•
•
•
•
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); }
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];
Happy coding
Regards
Shouvik
Shouvik
![]() |
Similar Threads
- To find the maximum subset sum in an array. (C)
- Getting sum of array (PHP)
- pointer and array arithmetic (C)
- C++ complete binary tree using an array. Unexpected end file (C++)
Other Threads in the IT Professionals' Lounge Forum
- Previous Thread: Visual learning to enhance school cirriculums
- Next Thread: redhat 9 command prompt
| Thread Tools | Search this Thread |
1gbit advertising advice amazon answers archive british broadband business businessprocesses career carrier censorship cern china cio collectiveintelligence connectivity consumer consumers corporateearnings datatransfer debtcollectors dictionary digg digital ebay ecommerce email employment environment facebook food government grid high-definition hottub infodelivery infotech intel internet interview ipod isp japan kindle lhc library malware marketing mit moonfruit news onlineshopping piracy piratebay pope porn program questions r&d religion remoteworking research retail security sex shopping simple skype smallbusiness smb sms socialmedia socialnetworking software softwareengineer spam speed spending startrek statistics stocks study stumbleupon survey tabletpc technology touch-screen touchscreen twitter uk videoinprint voips web webdeveloper windows words






?