Question: Write a function to print an array of stars to the screen in the form of an isosceles triangle. The function will have one input
Write a function to print an array of stars to the screen in the form of an isosceles triangle. The function will have one input and no outputs. The input will be the number of stars in the base of the triangle. Heres an example of what the output would look like for an input value of 9:
*
***
*****
*******
*********
Since the input value must be an odd number, include a test at the beginning of the function to check that the input is odd. One way to do this is to use the built-in MATLAB mod function. The mod function returns the remainder when dividing one integer by another. For example, mod(6,2) = 0, since the remainder is zero when dividing 6 by 2. mod(7,2) = 1, since dividing 7 by 2 results in a remainder of 1.
Here's my code can you explain how it works and why it's not printing the full triangle as well as fix the code for me?
function ex7_prob4(numStars) if mod(numStars,2) == 0 fprintf('Error: n must be odd! ') else for i = 1:((numStars+1)/2) for j = i:((numStars-1)/2) fprintf('') end for k = 1:((2*i)-1) fprintf('*') end fprintf(' ') end end end
ex7_prob4(5)
>>
* *** *****
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
