0

Stuck on 33.2 challenge, dynamic memory

Hey peeps, I understand how you can input the array size (n) and then input each value, but I’m having difficulty linking that to the given code in the challenge- which selects the maximum value of the values. This code is as far as I can get without my brain hurting đŸ€Ș #include <iostream> using namespace std; int main() { int n; int *ptr; cin>>n; ptr = new int[n]; for(int i=0;i<n;i++) { cin>>ptr[i]; int max = nums[0]; for(int i=0; i<n; i++) { if(nums[i]>max) max = nums[i]; } cout << max; } return 0; }

28th Feb 2021, 2:04 AM
MillĂŒ
MillĂŒ - avatar
3 RĂ©ponses
+ 3
(1) You're nesting for loops, which I don't think you've noticed. for(int i=0;i<n;i++) { cin>>ptr[i]; } <- Missing And there is an extra brackets before return 0. (2) nums[] does not exist, ptr[] does. (3) You did not delete ptr at the end of the code.
28th Feb 2021, 2:22 AM
äœ çŸ„é“èŠć‰‡ïŒŒæˆ‘äčŸæ˜Ż
äœ çŸ„é“èŠć‰‡ïŒŒæˆ‘äčŸæ˜Ż - avatar
+ 2
thank you so much!
28th Feb 2021, 2:46 AM
MillĂŒ
MillĂŒ - avatar