
% Lab 7 Solutions
% CS 1109, Summer 2011
% July 25

% This scripts prompts the user to enter a number n and prints out a square
% composed of *s whose side is of length n. The left diagonal of the square
% is also filled in. We assume that the user enters a number n >= 3.

clc
clear

% Get dimension of square
n = input('Enter a value for n: ');

% The main idea here is to think of the square pattern as ``embedded'' in a
% matrix, i.e., each cell of the matrix either has a * or a space. Note
% that this is simply for visualization purposes --- we don't actually have
% to construct this matrix. It just helps us ``see'' which cells need to be
% filled with *s
for i = 1 : n
    for j = 1 : n
        % Print a star if the current cell (i, j) is either in the first or
        % last row, the first or last column, or along the left diagonal 
        % (i.e., where i == j)
        if (i == 1) | (j == 1) | (i == n) | (j == n) | (i == j)
            fprintf('*');
        else
            fprintf(' ');
        end
    end
    
    % Print a newline *after* we have printed one complete row
    fprintf('\n');
end

