%-------------------------------------% % Homework 6: Problem 1 % % Date: 10/22/2002 % % % % Gun Srijuntongsiri % % gs61 % % ?????? % %-------------------------------------% % Algorithm % --------- % For each line, first output the correct number of spaces preceding the % first asterisk. Then output the asterisk. And then, if applicable, output % the required number of spaces between the first and second asterisk, and % then output the second asterisk. The first and last line are exception. % For the first line, only one asterisk is required. For the last line, % all are asterisks and no space. % Variables % --------- % height = Height of the triangle % i,j = indices for loop % Ask for the desired height height = input('Please enter the height of the triangle: '); fprintf('\n'); % For the top line, only do if height > 0 if height > 0 % There are height-1 spaces before the first star for j=1:height-1, fprintf(' '); end fprintf('*\n'); end % For the second line to the second to last line, one line at a time... for i=2:height-1 % Space preceding the left asterisk for j=1:height-i fprintf(' '); end fprintf('*'); % Space between the two asterisks for j=1:2*i-3 fprintf(' '); end fprintf('*\n'); end % For the last line, all are asterisks if height > 1 for j=1:2*height-1 fprintf('*'); end end fprintf('\n\n');