please solution in the practice question of c++ of dynamic memory. Sample input are 4,12,7,9 and 34. Sample output is 34. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

please solution in the practice question of c++ of dynamic memory. Sample input are 4,12,7,9 and 34. Sample output is 34.

C++ #include <iostream> using namespace std; int main() { int n; cin>>n; //size of the array //your code goes here int max = nums[0]; for(int i=0; i<n; i++) { if(nums[i]>max) max = nums[i]; } cout << max; return 0; }

21st Nov 2023, 7:22 PM
chukwudi anthony mbegbu
5 Answers
+ 2
/*in Sololearn input box, type: 4 12 7 9 34 Submit */ #include <iostream> using namespace std; int main() { int n; cin>>n; //size of the array //your code goes here 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; }
22nd Nov 2023, 11:29 AM
Bob_Li
Bob_Li - avatar
+ 1
Create an array of size n int* nums = new int[n]; Then loop n times to assign input values to the array for (int j = 0; j < n; j++) { cin>>nums[j]; }
21st Nov 2023, 7:49 PM
Yumi
Yumi - avatar
+ 1
chukwudi anthony mbegbu Probably not relevant, since the lesson was possibly about how to create arrays, but you don't even have to create an array if you only want max. #include <iostream> using namespace std; int main() { int n; cin>>n; int max = 0; for(int i=0; i<n; i++) { cin>>n; if(n>max) max = n; } cout << max; }
22nd Nov 2023, 4:24 AM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li thanks
22nd Nov 2023, 2:30 PM
chukwudi anthony mbegbu
0
Please can you type how i should code. How can one initialize and declare the nums?
22nd Nov 2023, 11:09 AM
chukwudi anthony mbegbu