0

33.2 challenge C++

hello. in practice 33.2 dynamic memory it ask u to find largest element in an array. my solution does not link to dynamic memory my solution: #include <iostream> using namespace std; int main() { int n; cin>>n; int nums[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; return 0; } but the title and the linked topic is dynamic memory. and i dont know how i can link dynamic memory. anyone has a better way by linking it to dynamic memory? thanks!

11th Sep 2021, 2:10 PM
Joe Li
4 Réponses
+ 4
I think the practice expected you to make use of `new` and `delete` pair to create the array (and release memory afterwards). At the moment you are using VLA (https://en.m.wikipedia.org/wiki/Variable-length_array) where you should be allocating array memory dynamically.
11th Sep 2021, 3:07 PM
Ipang
+ 1
Joe Li , this task sounds more complicated than it is. to get it run just use: int nums[n]; instead of: int nums[n]={};
11th Sep 2021, 3:51 PM
Lothar
Lothar - avatar
+ 1
thanks for your help! Martin Taylor Ipang Lothar
16th Sep 2021, 3:27 PM
Joe Li