944,044 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3331
  • C++ RSS
Nov 5th, 2007
0

finding larger number with recursion

Expand Post »
//Write a function: int max(int list[], int n) that recursively finds the largest integer between list[0] and list[n]. Assume at least one element in the list. Test it with a main program that takes as input an integer count followed by that many values. Output the original values followed by the maximum. Do not use a loop in max(). Output the value in the main program, not in the funcion.
//Sample input:
//5 50 30 90 20 80
//Sample output:
//Original list: 50 30 90 20 80
//Largest value: 90





cpp Syntax (Toggle Plain Text)
  1. # include <iostream>
  2. using namespace std;
  3. int max(int list[], int n)
  4. int main ()
  5. {
  6. int list[30]
  7.  
  8. cout << "please type in numbers: ";
  9. cin.getline (list, 30, '\n');
  10. cout << "original list: " << list [] << endl;
  11. cout << "largest value: " << n << endl;
  12.  
  13.  
  14. system ("pause");
  15. return 0;
  16. }
  17.  
  18.  
  19. int max(int list[], int n)
  20. {
//am not sure what to do next. I get the concept that i have to keep minmizing it, bit i so not know how to do that. please help!
Last edited by WaltP; Nov 5th, 2007 at 12:52 am. Reason: Added CODE tags -- you actually typed right over how to use them when you entered this post... And removed unecessary colors.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anga08628 is offline Offline
9 posts
since Oct 2007
Nov 5th, 2007
0

Re: finding larger number with recursion

Is there something about CODE tags you don't understand? If not, you can either ask or read those words on the back of the input box for information.

The project states "Test it with a main program that takes as input an integer count followed by that many values." Where did you read an integer count? Doesn't getline() read a string, not a list of integers? You need to use a loop to read (and output) your numbers.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Nov 5th, 2007
0

Re: finding larger number with recursion

Well the first question is, can you solve this problem using regular iteration?
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Nov 5th, 2007
-1

Re: finding larger number with recursion

instead of line 9, you should use a for loop to get the values for the integer array list...

and as WaltP said you have not read an integer count...it should have been read before line 8...

line 10 too is incorrect...you dont display an integer array like that...you have to use the for loop there...
Reputation Points: 8
Solved Threads: 4
Junior Poster in Training
tracethepath is offline Offline
54 posts
since Jul 2007
Nov 5th, 2007
0

Re: finding larger number with recursion

n again you have to call the function max between lines 10 and 11...or otherwise from where you will get the value for "n"??
Reputation Points: 8
Solved Threads: 4
Junior Poster in Training
tracethepath is offline Offline
54 posts
since Jul 2007
Nov 5th, 2007
0

Re: finding larger number with recursion

so is int list[] an array? and i would have to call every element of it?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anga08628 is offline Offline
9 posts
since Oct 2007
Nov 6th, 2007
0

Re: finding larger number with recursion

WHAT WOULD BE ANOTHER WAY I COULD WRITE THE LAST PART OF THIS FUNCTION?






//Write a function: int max(int list[], int n) that recursively finds the largest integer between list[0] and list[n]. Assume at least one element in the list. Test it with a main program that takes as input an integer count followed by that many values. Output the original values followed by the maximum. Do not use a loop in max(). Output the value in the main program, not in the funcion.
//Sample input:
//5 50 30 90 20 80
//Sample output:
//Original list: 50 30 90 20 80
//Largest value: 90





c++ Syntax (Toggle Plain Text)
  1. # include <iostream>
  2. using namespace std;
  3. int max(int[], int);
  4. int main ()
  5. {
  6. int list[40];
  7. int number_size=0;
  8.  
  9.  
  10. cout << "how many numbers you want to input? ";
  11. cin >> number_size;
  12. for(int i=0; i<number_size;i++)
  13. cin >> list[i];
  14. cout << "original list: " << endl;
  15. for(int i=0; i<number_size; i++);
  16. cout << list[i] << endl;
  17. cout << "largest value: " << max(list, number_size) << endl;
  18.  
  19.  
  20. system ("pause");
  21. return 0;
  22. }
  23.  
  24.  
  25. int max(int l[], int n)
  26. {
  27.  
  28. static int count=0;
  29. int maximum= l[count];
  30.  
  31. if (maximum<l[count])
  32. maximum=l[count];
  33. count++;
  34. if (count==n)
  35. return maximum;
  36. return max(l,n);
  37. }
Last edited by Ancient Dragon; Nov 6th, 2007 at 8:57 am. Reason: replace color tags with code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anga08628 is offline Offline
9 posts
since Oct 2007
Nov 6th, 2007
0

Re: finding larger number with recursion

line 15, you have declare i again...that is not correct...and you have placed a semi colon after for loop there...so line 16 will run just once and will not give the desired output...

and line 29 should not be there...instead it should be
C++ Syntax (Toggle Plain Text)
  1.  
  2. static int maximum=l[0];

otherwise as the function is called again and again maximum value will be initialised to present l[count]...and the maximum value till that point will be lost...
Reputation Points: 8
Solved Threads: 4
Junior Poster in Training
tracethepath is offline Offline
54 posts
since Jul 2007
May 13th, 2009
-1

Re: finding larger number with recursion

Quote ...
Try It, I think it will solve your problem
C++ Syntax (Toggle Plain Text)
  1. #include<iostream.h>
  2. #include<conio.h>
  3.  
  4.  
  5. int find(int *a,int start,int end)
  6. {
  7.  
  8.  
  9. if(start<end)
  10. {
  11.  
  12. if(a[start]<a[end])
  13. {
  14.  
  15. cout<<"1st Arrys condition"<<a[start]<<" "<<a[end]<<endl;
  16. return find(a,start+1,end);
  17.  
  18. }
  19. else
  20. cout<<"2nd Arrys condition"<<a[start]<<" "<<a[end]<<endl;
  21. return find(a,start,end-1);
  22.  
  23. }
  24. cout<<"Last= "<<a[end]<<endl;
  25. }
  26.  
  27. void main()
  28. {
  29. clrscr();
  30.  
  31. const int size=8;
  32. int arr[size]={7,6,13,12,2,9,5,99};
  33.  
  34. for(int i=0;i<size;i++)
  35. {
  36. cout<<arr[i]<<" ";
  37. }
  38. cout<<endl;
  39. find(arr,0,size-1);
  40.  
  41. getch();
  42. }
Reputation Points: -4
Solved Threads: 0
Newbie Poster
payara111 is offline Offline
6 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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.
Message:
Previous Thread in C++ Forum Timeline: making a graph in c++
Next Thread in C++ Forum Timeline: Using recursion to find largest item in an array





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


Follow us on Twitter


© 2011 DaniWeb® LLC