Find The Largest Element | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Find The Largest Element

Help please!!! Complete the code to find out the largest element of an array. Dynamic Memory The code provided calculates the largest element of the array nums and outputs it. Complete the code to declare the nums array and take the array elements as input. The array can be of any variable size, so the first input should be the size of the array, followed by its elements. Sample Input 4 12 7 9 34 Sample Output 34 Explanation The first input number (4) represents the size of the array, the next 4 numbers are the elements. The maximum value is 34. Declare a dynamic array and take each element as input in a loop. #include <iostream> using namespace std; int main() { int n; cin>>n; //size of the array //введите код сюда int max = nums[0]; for(int i=0; i<n; i++) { if(nums[i]>max) max = nums[i]; } cout << max; return 0; }

21st Mar 2021, 8:09 AM
Денис
Денис - avatar
6 Answers
+ 2
Declare the nums array after receiving its length n: int nums[n];
21st Mar 2021, 8:31 AM
JaScript
JaScript - avatar
+ 2
#include<iostream> using namespace std; int main () { int n, i, max; cin>>n; int arr[n]; for (i = 0; i < n; i++) { cin >> arr[i]; } max = arr[0]; for (i = 0; i < n; i++) { if (max < arr[i]) max = arr[i]; } cout<<max<<endl; return 0; } Declare the array nums....
21st Mar 2021, 8:40 AM
Ashutosh Kumar
Ashutosh Kumar - avatar
+ 2
It is a tip for getting ahead not the solution. Where do you see a problem visph ?
21st Mar 2021, 9:03 AM
JaScript
JaScript - avatar
+ 1
JaScript if you declare the nums array, you must fill it with cin to get all array values ^^
21st Mar 2021, 8:33 AM
visph
visph - avatar
0
there's no nums array declared in your code... you must either declare and fill the nums array by using cin n times, or use cin to get values (as you initialize max with the first value, you then need to loop from 1 to n not included): #include <iostream> using namespace std; int main() { int n; cin>>n; //size of the array int max, num; cin >> max; for (int i=1; i<n; i++) { cin >> num; if (num > max) { max = num; } } cout << max; return 0; }
21st Mar 2021, 8:22 AM
visph
visph - avatar
- 1
Денис nums is an input array so you have to first declare it then store values in this array. int n = 0; cin >> n; int num[n]; for (int i = 0; i < n; i++) { cin >> nums[i]; } Here is complete solution: https://code.sololearn.com/cyZ2oDC4Izvw/?ref=app
21st Mar 2021, 8:42 AM
A͢J
A͢J - avatar