Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Strategic Database Technology Management For The Year 2000

Authors: Alan Simon

1st Edition

155860264X, 978-1558602649

More Books

Students also viewed these Databases questions

Question

Briefly explain the key concepts regarding each figure.

Answered: 1 week ago