Please help with the following code, I must be getting this all wrong cause using this in recursive doesn’t give me correct results, rewriting this as iterative with a while it works fine.
Question: finding the largest element in an array
///////////////////////////
#include <iostream>
int largest(int first[], int temp, int count, int i)
{
if (count == (i+1))
return temp;
else
{
if (first
> temp)
{
temp = first;
i++;
return largest(first,temp,count,i);
}
else
i++;
}
}
using namespace std;
int main() {
int arr[3];
arr[0] = 3;
arr[1] = 7;
arr[2] = 4;
arr[3] = 3;
int temp = 1;
int count = 4; // size of array
int i = 0;
cout <<largest(arr,temp,count,i)<<endl;;
return 0;
}