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 Answers
+ 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