+ 1

Find The Largest Element (PRO)

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 //your code goes here int* nums = NULL; nums = new int{n}; int max = nums[0]; for(int i=1; i<n; i++) { if(nums[i]>max) max = nums[i]; } cout << max; delete [] nums; return 0; } I'm missing something, not sure what it is. I'm not fond of how the question is written, or how there is no help system, or eventual solution after trying a certain amount of times. I know there are multiple ways to approach solving problems, but I really just think I am not seeing how they want you to create the numbers in the array that follow the first number (size).

21st Feb 2021, 12:52 AM
Fredric Kearnes
Fredric Kearnes - avatar
9 ответов
+ 3
// you need to initialize your array items: for (int i=0; i<n; ++i) cin >> nums[i]; /* however, you could avoid storing numbers and doing only one loop ;) */
21st Feb 2021, 1:06 AM
visph
visph - avatar
+ 7
Hi there, This is the code that worked for me. #include <iostream> using namespace std; int main() { int n; cin>>n; //size of the array //your code goes here int *nums=NULL; nums=new int[n]; int max = nums[0]; for(int i=0; i<n; i++) { cin>>nums[i]; if(nums[i]>max) max = nums[i]; } cout << max; delete [] nums; return 0; } Hope i've helped
28th Jun 2021, 10:23 PM
David Cabezas
David Cabezas - avatar
+ 1
#include <iostream> using namespace std; int main() { int n; cin>>n; //size of the array //your code goes here int *nums=NULL; nums=new int[n]; int max = nums[0]; for(int i=0; i<n; i++) { cin>>nums[i]; if(nums[i]>max) max = nums[i]; } cout << max; delete [] nums; return 0; } Good Luck
25th Jan 2022, 4:40 PM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
0
@visph I am not following what you are saying.
21st Feb 2021, 1:16 AM
Fredric Kearnes
Fredric Kearnes - avatar
21st Feb 2021, 1:26 AM
visph
visph - avatar
0
#include <iostream> using namespace std; int main() { int n; cin>>n; //size of the array //your code goes here int *nums = NULL; nums = new int[n]; int max = nums[0]; for(int i=0; i<n; i++) { cin>>nums[i]; if(nums[i]>max) max = nums[i]; } cout << max; delete [] nums; return 0; } Now i figured it out but i think i did it by a little luck and reading your guys clues, im not understanding what adding cin>>nums[i]; did? Im getting input to iterate thru whatever the input wants to put in the list and the largest number will run thru the loop and be max?? Please explain someone with better understanding and knowledge on this. thanks in advance
8th Feb 2022, 6:01 AM
William Alley
William Alley - avatar
0
#include <iostream> using namespace std; int main() { int n; int array; int mem; int num; cin>>n; while (array != n){ cin >> num; if (num > mem){ mem = num; } array++; } cout << mem; return 0; } /* everything storing XD */
15th Oct 2022, 5:53 PM
Mikita Wagner
Mikita Wagner - avatar
0
#include <iostream> using namespace std; int main() { int n; cin>>n; //size of the array //your code goes here int* nums = nullptr; // Null nums = new int[n]; for(int i = 0;i<n;i++) { cin>>nums[i]; } int max = nums[0]; for(int i=0; i<n; i++) { if(nums[i]>max) max = nums[i]; } cout << max; delete []nums; return 0; } Good luck :)
28th Nov 2022, 1:47 PM
lior levi
lior levi - avatar
0
I do this and correct: #include <iostream> using namespace std; int main() { int n; cin >> n; //size of the array //your code goes here int* nums = NULL; nums = new int{n}; for(int x=0; x< n; x++){ cin >> nums[x]; } int max = nums[0]; for(int i=1; i<=n; i++) { if(nums[i]>max) max = nums[i]; } cout << max; return 0; }
24th Aug 2024, 9:59 AM
Bá Anh Nguyễn
Bá Anh Nguyễn - avatar