Question: Question 2 ( 4 0 points ) Write a MATLAB function that takes the integer n as an argument and prints out a complex diamond

Question 2(40 points)
Write a MATLAB function that takes the integer n as an argument and prints out a complex diamond shape
with 2n -1 rows, where each row alternates between numbers and stars. For example, if n is 4, the output
should look like this:
1
1*1
1*2*1
1*2*3*1
1*2*1
1*1
1
Hint: Use nested loops to print spaces, numbers, and stars.
here is the code I used to answer the question, however some errors appear when I run the funcion. Fix the following code in the function so that it will run proporly:
function printComplexDiamond(n)
for i =1:n
fprintf(repmat('',1, n - i));
for j =1:i
fprintf('%d', j);
if j < i
fprintf('*');
end
end
fprintf('');
end
for i = n-1:-1:1
fprintf(repmat('',1, n - i));
for j =1:i
fprintf('%d', j);
if j < i
fprintf('*');
end
end
fprintf('');
end
end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!