| | |
finding larger number with recursion
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2007
Posts: 9
Reputation:
Solved Threads: 0
//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
//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!
//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)
# include <iostream> using namespace std; int max(int list[], int n) int main () { int list[30] cout << "please type in numbers: "; cin.getline (list, 30, '\n'); cout << "original list: " << list [] << endl; cout << "largest value: " << n << endl; system ("pause"); return 0; } int max(int list[], int n) {
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.
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
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
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...
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...
•
•
Join Date: Oct 2007
Posts: 9
Reputation:
Solved Threads: 0
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
//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)
# include <iostream> using namespace std; int max(int[], int); int main () { int list[40]; int number_size=0; cout << "how many numbers you want to input? "; cin >> number_size; for(int i=0; i<number_size;i++) cin >> list[i]; cout << "original list: " << endl; for(int i=0; i<number_size; i++); cout << list[i] << endl; cout << "largest value: " << max(list, number_size) << endl; system ("pause"); return 0; } int max(int l[], int n) { static int count=0; int maximum= l[count]; if (maximum<l[count]) maximum=l[count]; count++; if (count==n) return maximum; return max(l,n); }
Last edited by Ancient Dragon; Nov 6th, 2007 at 8:57 am. Reason: replace color tags with code tags
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
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...
and line 29 should not be there...instead it should be
C++ Syntax (Toggle Plain Text)
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...
•
•
Join Date: May 2009
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
Try It, I think it will solve your problem
C++ Syntax (Toggle Plain Text)
#include<iostream.h> #include<conio.h> int find(int *a,int start,int end) { if(start<end) { if(a[start]<a[end]) { cout<<"1st Arrys condition"<<a[start]<<" "<<a[end]<<endl; return find(a,start+1,end); } else cout<<"2nd Arrys condition"<<a[start]<<" "<<a[end]<<endl; return find(a,start,end-1); } cout<<"Last= "<<a[end]<<endl; } void main() { clrscr(); const int size=8; int arr[size]={7,6,13,12,2,9,5,99}; for(int i=0;i<size;i++) { cout<<arr[i]<<" "; } cout<<endl; find(arr,0,size-1); getch(); }
![]() |
Similar Threads
- We only give homework help to those who show effort (Computer Science)
- Help in finding 1500`th ugly number. (C++)
- How to get the result for smaller number divide larger number? (MS SQL)
- finding the smallest number (C++)
- finding max value of 10 integers (C++)
- Number of times a number appears in an array (C++)
- program for finding factorial of a number using *operator overloading (C++)
- Finding out Google PR number (Search Engine Optimization)
Other Threads in the C++ Forum
- Previous Thread: making a graph in c++
- Next Thread: Using recursion to find largest item in an array
Views: 2435 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






