% loop.m
% CS 221 HW 2
% Part 2, Problem 1


% [a.] Create a 1-by-1000 array of random numbers between 0 and 100. 
% 
% Note: There are several ways to do this. This approach uses
% rand(rows, cols) to create a 1 by 1000 array of random numbers
% between 0.0 and 1.0. Then we multiply the constant 100 by every
% element in the array and store the result in variable "data".
% You could have also written a loop to do the same thing.

array_length = 1000;
data = 100.0 * rand(1, array_length);


% [b.] Initialize variables to hold the minimum, maximum, and sum 
% of the values.
%
% Again, there is more than one way to do this. Following the hint
% in the problem set, we initialized the minimum to POSITIVE inifinity
% and initialized the maximum to NEGATIVE infinity. But you could have
% also set both values to data(1,1) and then iterated from 2:1000 in
% the "for" loop.

min_value = Inf;
max_value = -Inf;
sum = 0.0;

% [c.] Use a for-loop to examine each of the values in the array, 
% and update the minimum, maximum and sum as required.
%
% NOTE: MATLAB actually allows you to replace the first two lines here
% with:
%
% for value = data
% 
% and then everything else will be the same as what follows. This 
% frequently simplifies code, but we haven't officially shown you how
% to do this yet. See the help file on "for" loops for details. 

for col = 1:array_length
    value = data(1, col);
    if value < min_value
        min_value = value;
    end
    if value > max_value
        max_value = value;
    end
    sum = sum + value;
end


% [d.] After the loop, compute the mean by dividing the sum by 
% the number of elements processed. 

mean = sum / array_length;


% [e.] Print the minimum, maximum, and mean, all on one line. 

fprintf('The max is %f, the min is %f, and the mean is %f.\n', max_value, min_value, mean);


% [f.] Save your program in a file loop.m.
% NOTE: This is an important step. 
