finding larger number with recursion

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2007
Posts: 9
Reputation: anga08628 is an unknown quantity at this point 
Solved Threads: 0
anga08628 anga08628 is offline Offline
Newbie Poster

finding larger number with recursion

 
0
  #1
Nov 5th, 2007
//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





  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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,131
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: finding larger number with recursion

 
0
  #2
Nov 5th, 2007
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: finding larger number with recursion

 
0
  #3
Nov 5th, 2007
Well the first question is, can you solve this problem using regular iteration?
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 54
Reputation: tracethepath is an unknown quantity at this point 
Solved Threads: 4
tracethepath's Avatar
tracethepath tracethepath is offline Offline
Junior Poster in Training

Re: finding larger number with recursion

 
-1
  #4
Nov 5th, 2007
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...
With Regards...
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 54
Reputation: tracethepath is an unknown quantity at this point 
Solved Threads: 4
tracethepath's Avatar
tracethepath tracethepath is offline Offline
Junior Poster in Training

Re: finding larger number with recursion

 
0
  #5
Nov 5th, 2007
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"??
With Regards...
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 9
Reputation: anga08628 is an unknown quantity at this point 
Solved Threads: 0
anga08628 anga08628 is offline Offline
Newbie Poster

Re: finding larger number with recursion

 
0
  #6
Nov 5th, 2007
so is int list[] an array? and i would have to call every element of it?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 9
Reputation: anga08628 is an unknown quantity at this point 
Solved Threads: 0
anga08628 anga08628 is offline Offline
Newbie Poster

Re: finding larger number with recursion

 
0
  #7
Nov 6th, 2007
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





  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 54
Reputation: tracethepath is an unknown quantity at this point 
Solved Threads: 4
tracethepath's Avatar
tracethepath tracethepath is offline Offline
Junior Poster in Training

Re: finding larger number with recursion

 
0
  #8
Nov 6th, 2007
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
  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...
With Regards...
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 6
Reputation: payara111 has a little shameless behaviour in the past 
Solved Threads: 0
payara111 payara111 is offline Offline
Newbie Poster

Re: finding larger number with recursion

 
-1
  #9
May 13th, 2009
Try It, I think it will solve your problem
  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. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 2435 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC