Help me pls, can't figure it out. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Help me pls, can't figure it out.

Find bigest number. int main() { int n; cin>>n; //size of the array //your code goes here int *nums = new int[n]; int max = nums[0]; nums [0] =n; for(int i=0; i<n; i++) { if(nums[i]>max) max = nums[i]; } cout << max; delete []nums; return 0; } Why it is only looking for first number?

27th Feb 2021, 7:01 PM
Marina Bruzgelevičė
Marina Bruzgelevičė - avatar
3 Answers
0
Thanks, did't know about max = *max_element(nums, nums + n); just could't figure it how to determine it.
27th Feb 2021, 8:09 PM
Marina Bruzgelevičė
Marina Bruzgelevičė - avatar
0
And by the way, still haven't learned about #include <algorithm>, so here was a mistake too, just did not include it before.
27th Feb 2021, 8:13 PM
Marina Bruzgelevičė
Marina Bruzgelevičė - avatar
- 1
Initialize max with 0 perhaps it should be the maximum number in the array .If zero not the max so swap it with the true maximum nums . int max = 0;
27th Feb 2021, 7:38 PM
HBhZ_C
HBhZ_C - avatar