Im lost sum1 help me with the code, im new to this Matlab programming

조회 수: 1 (최근 30일)
Merapelo CHamme
Merapelo CHamme 2013년 12월 3일
편집: Walter Roberson 2013년 12월 3일
Write a script that requests two positive integers greater than 3 (with error checking). The
numbers represent the x- and y-dimensions of a rectangle. Your program should then display a
rectangle of asterisks (*) with the specified dimensions. The rectangle with dimensions x = 5
and y = 4 would be displayed as :
*****
* *
* *
*****

답변 (1개)

sixwwwwww
sixwwwwww 2013년 12월 3일
Dear Merapelo, you can do it like this:
width = input('Enter width of rectangle (value should be positive and greater than 3): ');
if ~isnumeric(width)
error('Value for width is not a valid number')
elseif width < 4
error('Width is less than 3')
end
height = input('Enter height of rectangle (value should be positive and greater than 3): ');
if ~isnumeric(height)
error('Value for height is not a valid number')
elseif height < 4
error('Height is less than 3')
end
NumberOfAsteriksInEachDirection = 100;
x = [zeros(1, NumberOfAsteriksInEachDirection), linspace(0, width, NumberOfAsteriksInEachDirection), linspace(0, width, NumberOfAsteriksInEachDirection),...
ones(1, NumberOfAsteriksInEachDirection) * width];
y = [linspace(0, height, NumberOfAsteriksInEachDirection), zeros(1, NumberOfAsteriksInEachDirection), ones(1, NumberOfAsteriksInEachDirection) * height,...
linspace(0, height, NumberOfAsteriksInEachDirection)];
plot(x, y, '*'), xlim([-1 width+1]), ylim([-1 height+1])
I hope it helps. Good luck!

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by