//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

# 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)
{

//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!

Dani AI

Generated

A few focused fixes and a compact recursive pattern you can apply.

Treat n consistently — here it's easiest to make n the number of elements. The recursive idea: if there's only one element return it (base case); otherwise compute the max of the first n-1 elements and compare that with list[n-1]. Read the count and the numbers with cin >> (as pointed out, getline reads a string and won't parse integers). Avoid using static counters inside the recursive function — that persistent state breaks correctness and makes repeated calls unsafe. Also watch the common for-loop mistakes flagged (no stray semicolons, don't redeclare i unintentionally).

A minimal, working example:

#include <iostream>
using namespace std;

int max(int list[], int n) {
    if (n == 1) return list[0];
    int best = max(list, n - 1);
    return (best >= list[n - 1]) ? best : list[n - 1];
}

int main() {
    int count;
    if (!(cin >> count) || count <= 0) return 0;
    int arr[1000];
    for (int i = 0; i < count; ++i) cin >> arr[i];
    cout << "Original list: ";
    for (int i = 0; i < count; ++i) cout << arr[i] << (i + 1 < count ? " " : "");
    cout << "\nLargest value: " << max(arr, count) << "\n";
    return 0;
}

Notes and troubleshooting

  • If the assignment instead expects n as the last index, use if (start == end) ... or adjust calls to max(list, lastIndex+1). The start/end style is the alternative pattern (as used), but make the function return the value — print in main.
  • For very large arrays recursion depth may hit the stack; for classroom sizes it's fine.
  • Keep I/O and printing in main as required; the recursive function should only compute and return the result.

Recommended Answers

All 8 Replies

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.

Well the first question is, can you solve this problem using regular iteration?

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...

commented: Isn't that what I said? -2

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"??

so is int list[] an array? and i would have to call every element of it?

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

# 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);
}

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

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...

Try It, I think it will solve your problem

#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();
}
commented: Look at the dates, please. -7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.