% NCIAE
% Computer Applications for Engineers
% Assignment 3

close all; clear; clc

% Problem 9.17

fprintf("Problem 9.17\n\n")

levels = [
    3680.12 3668.05 3654.25 3617.61 3594.38 3563.41 3596.26 3601.41
    3678.48 3665.02 3651.01 3613    3589.11 3560.35 3591.94 3598.63
    3677.23 3663.35 3648.63 3608.95 3584.49 3557.42 3589.22 3597.85
    3676.44 3662.56 3646.79 3605.92 3583.02 3557.52 3589.94 3599.75
    3676.76 3665.27 3644.88 3606.11 3584.7  3571.60 3598.27 3604.68
    3682.19 3672.19 3642.98 3615.39 3587.01 3598.06 3609.36 3610.94
    3682.86 3671.37 3637.53 3613.64 3583.07 3607.73 3608.79 3609.47
    3681.12 3667.81 3630.83 3607.32 3575.85 3604.96 3604.93 3605.56
    3678.7  3665.45 3627.1  3604.11 3571.07 3602.20 3602.08 3602.27
    3676.96 3663.47 3625.59 3602.92 3570.7  3602.31 3606.12 3601.27
    3674.93 3661.25 3623.98 3601.24 3569.69 3602.65 3607.46 3599.71
    3671.59 3658.07 3621.65 3598.82 3565.73 3600.14 3604.96 3596.79
];

months = [
    "January"
    "February"
    "March"
    "April"
    "May"
    "June"
    "July"
    "August"
    "September"
    "October"
    "November"
    "December"
];

years = 2000:2007;

% Part a: Mean water level

yearly_means = zeros(size(years));
overall_mean = 0;

for i = 1:length(years)
    for j = 1:length(months)
        yearly_means(i) = yearly_means(i) + levels(j,i);
        overall_mean = overall_mean + levels(j,i);
    end
    yearly_means(i) = yearly_means(i) / length(months);
end

overall_mean = overall_mean / (length(years) * length(months));

fprintf("Yearly means for years:\n")
fprintf("%7d ", years)
fprintf("\n")
fprintf("%7.2f ", yearly_means')
fprintf("\n")

fprintf("\nOverall mean:\n%0.2f\n", overall_mean)

% Part b: Number of months exceeding overall average

months_exceeding = zeros(size(years));

for i = 1:length(years)
    for j = 1:length(months)
        if levels(j,i) > overall_mean
            months_exceeding(i) = months_exceeding(i) + 1;
        end
    end
end

fprintf("\nNumber of months exceeding overall average:\n")
fprintf("%4d ", years);
fprintf("\n")
fprintf("%4d ", months_exceeding)
fprintf("\n")

% Part c: Report

fprintf("\nMonths exceeding overall average:\n")

for i = 1:length(years)
    for j = 1:length(months)
        if levels(j,i) > overall_mean
            fprintf("%02d-%d\n", j, years(i))
        end
    end
end

% Part d: Monthly average

monthly_average = zeros(size(months));

for i = 1:length(months)
    for j = 1:length(years)
        monthly_average(i) = monthly_average(i) + levels(i,j);
    end
    monthly_average(i) = monthly_average(i) / length(monthly_average);
end

fprintf("\nMonthly average:\n")
fprintf("%9s ", months)
fprintf("\n")
fprintf("%9.2f ", monthly_average)
fprintf("\n")

% Problem 9.18

fprintf("\nProblem 9.18\n")

% Part a

x = randi([0, 9], 1, 100000);

fprintf("\nElement-wise square %d digits\n", length(x))

tic
y = x.^2;
toc

% Part b

clear x y

x = randi([0, 9], 1, 100000);

fprintf("\nFor loop square %d digits\n", length(x))

tic
y = [];
for i = 1:length(x)
    y = [y, x(i)^2];
end
toc

% Part c

clear x

x = randi([0, 9], 1, 2000);

fprintf("\nElement-wise print square of %d digits\n", length(x))

tic
fprintf("%d ", x.^2);
fprintf("\n")
toc

clear x

x = randi([0, 9], 1, 2000);

fprintf("\nFor loop print square of %d digits\n", length(x))

tic
for i = 1:length(x)
    fprintf("%d ", x(i)^2)
end
fprintf("\n")
toc

% Part d

clear x y

x = 100 * rand(1, 1000);

fprintf("\nAdding a constant value to a %d element array\n", length(x))

tic
y = [];
for i = 1:length(x)
    y = [y, x + (sin(0.3) + cos(pi/3)) * factorial(5)];
end
toc

clear x y

x = 100 * rand(1, 1000);

fprintf("\nAdding a cached constant value to a %d element array\n", length(x))

tic
y = [];
cache = (sin(0.3) + cos(pi/3)) * factorial(5);
for i = 1:length(x)
    y = [y, x + cache];
end
toc

% Part d

clear x y

x = randi([0, 9], 1, 100000);

fprintf("\nFor loop square %d digits with constant size array\n", length(x))

tic
y = zeros(size(x));
for i = 1:length(x)
    y(i) = x(i)^2;
end
toc