Draw a rectangle made with printf function, consis

2019-09-23 01:46发布

问题:

I've been trying this for hours but couldn't figure out a way to do it. We were given an assignment in school to get 2 inputs for Height and Width(assuming they're between 2-50), and create a rectangle in this way: the rectangle frame will be made out of the letter a, the inner rectangle frame will be made out of the letter b, the inner rectangle frame of the inner rectangle frame will be made out of the letter c, and so on... for instance:

10X7:
aaaaaaaaaa
abbbbbbbba
abccccccba
abcddddcba
abccccccba
abbbbbbbba
aaaaaaaaaa

I was trying working with "for" loops for height and width and I was drawing matrix as draft to see how to change the letter jumping with ASCII values, but I couldn't figure out an algorithm. help would be very appreciated.

回答1:

As you have not posted code, I will also just verbally describe a possible algorithm.

You'll need two embedded for loops. Outside loop for lines (y), inside loop for characters within line (x). Both shall run from zero to xmax and ymax, which are the numbers specified on the command line minus one.

Inside the body of the inner loop you have to find the frame. It depends on the distance to the edge. But which edge? The closest one. So this distance is the minimum of (x, y, xmax-x, ymax-y).

Then you print 'a' + distance. Additionally, after each line, you print a linefeed.



标签: c printf