Dynamic Memory C++ problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Dynamic Memory C++ problem

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. This is my code: #include <iostream> using namespace std; int main() { int n; cin>>n; //size of the array //your code goes here int nums[n]; while(n>0){ int x; cin>>x; nums[n-1] = x; n--; } int max = nums[0]; for(int i=0; i<n; i++) { if(nums[i]>max) max = nums[i]; } cout << max; return 0; } It passed 4 of the 5 tests. Can someone please explain what is wrong with this code ? Thank you.

13th Jun 2022, 2:12 PM
Florian Catalin
0 Answers