Question: Write a function printDiamond ( n ) which prints ascii art of a diamond with a size based on the positive integer n . For

Write a function printDiamond (n) which prints ascii art of a diamond with a size based on the positive integer n. For example, printDiamond(4) would print:
11
2****2
3********3
4************4
3********3
2****2
11
Whereas printDiamond(3) would print:
11
2****2
3********3
2****2
11
You'll want to create a loop where each iteration prints a single line of the ascii art. To draw multiple spaces and multiple asterisks on a single line, consider using the * operator, which can be used to repeat a string an integer number of times.
Hint 1: Every line is composed of three parts: outer spaces, inner asterisks, and two numbers on the outside of the diamond. For example, the second line of the size =3 diamond has one space, then the number 2, then two asterisks, then the number 2 again. Consider each of these parts individually, note how they change between iterations, then determine how to map the loop control variable to each part separately.
Hint 2: if you're feeling overwhelmed, simplify the problem by breaking it down into parts! Start by just making the top half of the diamond. First, get the numbers to appear correctly; second, add in the asterisks. Finally, add the leading spaces.
Hint 3: how can the program switch from the increasing top half to the decreasing bottom half? Consider using two separate loops (one going up, one going down), or a sinale loop with a conditional that chanqes how the loop control variable is used.
 Write a function printDiamond (n) which prints ascii art of a

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!