So I am learning arrays, and i was given a piece of code in my book here which basically finds the average, min, and max value of a set of numbers, but the thing i don't understand is, when you are finding out the min and the max why you set min_val
and max_val
to nums[0]
or any other nums for that matter.
min_val = max_val = nums[0];
for(i=0; i<10; i++) {
if(nums[i] < min_val) min_val = nums[i];
if(nums[i] > max_val) max_val = nums[i];
}
cout << "Minimum value is " << min_val << "\n";
cout << "Maximum value is " << max_val << "\n";