This is the question I've tried to attempt "Given an array A[] of size n. The task is to find the largest element in it.
Input:
n = 5
A[] = {1, 8, 7, 56, 90}
Output:
90
Explanation:
The largest element of the given array is 90.
I'm unable to get the desired outcome, my output screen freezes. I think the problem is in passing by reference, please help me solve and understand my mistake
Here's my code:
#include <iostream>
using namespace std;
int largest(int* arr[], int* n) {
int max = 0;
for (int first = 0; first <* n; first++) {
if (*arr[first] > max) {
max = *arr[first];
}
}
return max;
}
int main() {
cout << "enter how many numbers you want in an array = ";
int userinput;
cin >> userinput;
int* myarray=new int[userinput];
for (int loop = 0; loop < userinput; loop++) {
cin >> myarray[loop];
}
cout << "Your array = { ";
for (int loop = 0; loop < userinput; loop++) {
cout<< myarray[loop]<<" ";
}
cout << "}";
cout << endl;
cout<< "Largest number in the array = "<<largest(&myarray,&
userinput);
return 0;
}