Crunch Posted September 10, 2004 Share Posted September 10, 2004 hello everyone I need a function in my program that adds the sum of the the first n integers in the array using recursion Things I got: array is already defined n is already been input from the user Link to comment Share on other sites More sharing options...
0 azcodemonkey Posted September 10, 2004 Share Posted September 10, 2004 Show us what you've got code-wise. Is this homework? Link to comment Share on other sites More sharing options...
0 boocher Posted September 10, 2004 Share Posted September 10, 2004 Show us what you've got code-wise. Is this homework? I agree. Is there a reason why you're having trouble or are you just being lazy? That's not a very hard concept. _--boocher Link to comment Share on other sites More sharing options...
0 Crunch Posted September 10, 2004 Author Share Posted September 10, 2004 yes it is homework /* Assignment #1: This is a program written to compute the sum of the first n integers in an array. Jason Pankiw, September 05, 2004 */ #include <iostream> using namespace std; //Where the actual computation will take place int compute(int); //array of 20 variables I defined const int array[20] = {21, 6, 84, 15, 1, ? ? ? ?99, 25, 8, 34, 4, ? ? ? ?78, 9, 2, 73, 41, ? ? ? ?12, 69, 11, 7, 5}; void main() { //user picks the number of variables to compute int input = 0; // boolean variable for tests bool truth = false; // the sum being displayed to the console int sum = 0; // 'Truth test' to see if input is in correct form while(truth == false) { ? ? ? ?//user prompt to enter "input" ?cout << "Please enter a number between 1 and 20" << endl; ?cin >> input; ? ? ? ?// if input is correct, you exit the while-loop ?if (input <= 20 && input >= 1) ? truth = true; } //test cout << "INPUT: " <<input << endl; //call to the function "compute" sum = compute(input); //test cout << "SUM: " << sum << endl; //test ? ?cout << "INPUT: " <<input << endl; } // ------------------------------------------------------------------------------ // compute(int) will add the numbers in the array using recursion // Precondition: input must pass 'truth test' // Postcondition: returns the sum of the integers // ------------------------------------------------------------------------------ int compute(int i) { if (i == 1) ?return array[i-1]; else ?return compute(array[i-1]) + compute(array[i-2]); } Please enter a number between 1 and 2015 INPUT: 15 Press any key to continue it doesnt get through the function compute. Link to comment Share on other sites More sharing options...
0 Crunch Posted September 10, 2004 Author Share Posted September 10, 2004 (edited) dont give me the answers I just need to know what is wrong ahhhh I know the problem is where the addition it supposed to take place I am just having problems putting the array and implementing the i variable Edited September 10, 2004 by Crunch Link to comment Share on other sites More sharing options...
0 Crunch Posted September 10, 2004 Author Share Posted September 10, 2004 well I think I got it int compute(int i) { if (i == 1) return array[i-1]; else return array[i-1] + compute(i-1); } is that the way you would write it? Link to comment Share on other sites More sharing options...
0 Laughing-Man Posted September 10, 2004 Share Posted September 10, 2004 If you look at your code, the first value of i is going to be 15. In the last part of your addition statement you are trying to compute: compute(array[15-2]), which is equal to: compute(array[13]), which is equal to: compute(73) The next time it goes through the loop i = 73 then when you get to the addition part you are accessing array[72] and array[71], which do not exist. Link to comment Share on other sites More sharing options...
0 Laughing-Man Posted September 10, 2004 Share Posted September 10, 2004 Doing the program in my head, what you just posted as the correction looks like the right answer. Link to comment Share on other sites More sharing options...
0 jdlarson83 Posted September 10, 2004 Share Posted September 10, 2004 looks like a lot of crap on the screen, where do you go to school format your code makes it easier to read!! Link to comment Share on other sites More sharing options...
0 Crunch Posted September 10, 2004 Author Share Posted September 10, 2004 yes that is the problem I was having with it from the start 'i' doesnt initialize on every recursion with the correction it fixes the problem Link to comment Share on other sites More sharing options...
0 Crunch Posted September 10, 2004 Author Share Posted September 10, 2004 looks like a lot of crap on the screen, where do you go to schoolformat your code makes it easier to read!! when I copy/pasted the code from Microsoft Visual C++ .NET it kinda messed up the spacing and indentation so that isnt my fault if you want to look at the file I will gladly send it looks 10x better sorry if it looks bad Link to comment Share on other sites More sharing options...
Question
Crunch
hello everyone
I need a function in my program that adds the sum of the the first n integers in the array using recursion
Things I got:
array is already defined
n is already been input from the user
Link to comment
Share on other sites
10 answers to this question
Recommended Posts