% Assignment 2 - functions and flow control
%
% Nigel Stepp <stepp@atistar.net>
%
% It might help to paste some of these things into their own files.
% If you have questions, or want comments, feel free to email me 
% your answers.
%
% *** Note: if Question 1 is seeming too hard, just skip it and we can
% go over it in class. ***
%
% Topics covered: functions, input and output parameters, flow control

% Question 1:
% 
% An undergrad messed up your data files, and instead of having
% a column for each variable that you are measuring (3 of them), there
% is just one long column.
%
% But, you know you took 5 observations of each variable! (we'll assume
% that 5 observations is enough :) )
%
% Since you know the number of observations, and the undergrad fortunately
% kept the data in order, you should be able to reconstruct your data matrix.
%
% Take the data in 'bad_data' and create a 5x3 matrix of your
% corrected data called 'data'.
%
% hint: you may need to *iterate*.
% other hint: if you want to be extra elegant, consider using
%             the 'mod' builtin function which computes the modulus.
%             If you use mod, you will want to look up 'ceil' as well.

bad_data = [5,8,3,4,5,10,14,27,19,2,18.2,72,89,105,37];

% 'data' should end up like this:
% 5 10  18.2
% 8 14  72
% 3 27  89
% 4 19 105
% 5  2  37

% Here are 4 ways to solve this problem (not the only 4, though):

% Matlab vector slicing non-loop method (this is an instance of "hard-coding"
% which is usually discouraged).
data(:,1) = bad_data(1:5);
data(:,2) = bad_data(6:10);
data(:,3) = bad_data(11:15);

% Uses matlab vector slicing with a for loop (notice how it is more general)
for i=1:3
	data(:,i) = bad_data( 5*(i-1)+1:5*i );
end;

% nested loop example
for i=1:3
	for j=1:5
		data(j,i) = bad_data( j + 5*(i-1) );
	end;
end;

% Uses the mod() and ceil() builtin functions
for i=1:length(bad_data)
	row = mod(i-1,5) + 1;
	column = ceil(i/5);
	data(row, column) = bad_data(i);
end;


% Question 2:
%
% Great, looks like this poor undergrad did this with *all* of
% your data files. So you have to run this on all of them.
%
% Write a function which takes a bad_data vector as an input parameter
% and returns a corrected matrix.
%
% hint: This is just an extension of Question 1.
%
% If you want to be really general, also make the function take
% the number of observations as an
% additional input parameter. In Question 1, this would be
% 5.

function data = fix_data( bad_data, observations )
	for i=1:length(bad_data)
		row = mod(i-1, observations) + 1;
		column = ceil(i/observations);
		data(row, column) = bad_data(i);
	end;
end;

% Question 3:
%
% Here is a while loop which asks the user for input. Notice that
% it loops while true is true (forever!).  This is called an infinite
% loop.
%
% Add code where requested.
%
% This uses the builtin functions 'input', 'disp', and 'strcmp'. See MATLAB help
% for their uses (it's good to get in the habit of using it as a reference).

while true
	entry = input('Please type something and hit enter: ', 's');

	if strcmp(entry, 'Knock knock')
		disp('Who is there?');
	end;
	if strcmp(entry, 'What is your name?')
		disp('Hi, my name is MATLAB, thank you for programming me.');
	end;

	% Here, place some code that will stop the loop if the user
	% enters 'quit'. You might want to say goodbye, also.
	if strcmp(entry, 'quit')
		disp('Bye!');
		break;
	end;

	disp( ['(By the way, you entered "', entry, '")'] );
end;

