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";

If you don't set min_val and max_val to something, it will be some random number to start --which may be lower or higher than any value in your array. So, if your array were 3 5 97 -2 12 but min_val were -349762, you'd have a problem.

Both values must be a value in the array, and both values must not be smaller/larger than the smallest/largest value in the array. Might as well initialize them to the first value in the array, thus guaranteeing both conditions are true.

Hope this helps.

If you don't set min_val and max_val to something, it will be some random number to start --which may be lower or higher than any value in your array. So, if your array were 3 5 97 -2 12 but min_val were -349762, you'd have a problem.

Both values must be a value in the array, and both values must not be smaller/larger than the smallest/largest value in the array. Might as well initialize them to the first value in the array, thus guaranteeing both conditions are true.

Hope this helps.

Yeah, thanks. I think I get it, but only time will tell.
The next program I am dealing with requires me to set up an array with random numbers in each slot, then sort the array from highest to lowest... this is the code i have for it,

int nums[10];
    int a, b, t;
    int size;
    
    size = 10;
    
    for(t=0; t<size; t++) nums[t] = rand();
    
    cout << "Original array is:\n   ";
    for(t=0; t<size; t++) cout << nums[t] << " ";
    
    for(a=1; a<size; a++)
    for(b=size-1; b>=a; b--) {
                  if(nums[b-1] > nums[b]) {
                               t = nums[b-1];
                               nums[b-1] = nums[b];
                               nums[b] = t;
                               }
                  }
    cout << "\nSorted array is: \n";
    for(t=0; t<size; t++) cout << nums[t] << ' ';

I was wondering if you could explain to me just exactly what happens after for(a=1; a<size; a++) . I got overwhelmed and lost myself.

<Yeah, thanks. I think I get it, but only time will tell.
You sound iffy, so in other words what that first line does for you is it gives it an assigned value. without an assigned number it can't compare anything to it.

so during these lines:

for(i=0; i<10; i++) {
             if(nums[i] < min_val) min_val = nums[i];
             if(nums[i] > max_val) max_val = nums[i]; 
             }

it compares and lets say min_val = max_val = nums[0]; was not there. at this point then your min_val and max_val will be empty. thus it will not work. Hope that cleared it up a bit.


Somethings i would do is clean this up a bit. Certain things like

int size;
    size = 10;

is a constant so make it a constant.

for(t=0; t<size; t++) 
           nums[t] = rand();

Sets a random number to your array nums. and since you set your array size to 12 you have an array that holds 12 values.

cout << "Original array is:\n   ";
    for(t=0; t<size; t++) 
           cout << nums[t] << " ";

This prints out the values in the array so every number between nums[0] and nums[11] will be displayed.

for(a=1; a<size; a++)

this sets a loop to go 11 times.

for(b=size-1; b>=a; b--)

says b = 11 and while b is greater or equal to a, run the loop. then after subtract 1 from b.
{

if(nums[b-1] > nums[b]) {

loops once saying that is nums[b-1] is greater than nums then run the loop.

t = nums[b-1];
                               nums[b-1] = nums[b];
                               nums[b] = t;

with this I'm not too sure what it is you are trying to do. you set nums[b-1] to t. then make it to that every value is going to hold the value of nums. then later on you set num to t. then the next line


cout << "\nSorted array is: \n";
    for(t=0; t<size; t++) cout << nums[t] << ' ';

sets t to 0. you did nothing with the value t at this point.


If you're trying to find the lowest and highest value you need a comparison int variable something like if (nubs[b+1]> nums)
highest = nums[b+1];

This article has been dead for over six months. Start a new discussion instead.