Puzzles A21 ... A30

Part A — Loop Problems

These puzzles involve simple loops. Of course, "simple" is a relative term . . .


Puzzle A21 — print a pine tree of stars

[M-21]Write a main() program that prints an ASCII-art version of a pine tree:

...........*...........
..........***..........
.........*****.........
........*******........
.......*********.......
......***********......
.....*************.....
....***************....
...*****************...
..*******************..
.*********************.
***********************
..........***..........
..........***..........
..........***..........
..........***..........

You might want to add a ornament at the peak of the tree, or perhaps put some decorations within the tree.


Puzzle A22 — print a n by m block of stars centered on a N by M background of dots

[M-22]Write a main() program that prints a block of stars centered inside a block of dots. Make n, m, N, and M constants of the program, or make them optional command line parameters. Here is the output when n==5, m==7, N==11, M==13:

.............
.............
.............
...*******...
...*******...
...*******...
...*******...
...*******...
.............
.............
.............

If you need an explanation of command line parameters, look at the answer (or check you C text book.)


Puzzle A23 — print a n by n square divided into quarants of stars, dots, dollars, and ohs

[M-12]Write a main() program that prints an n by n square of characters. Divide the square into quarters, and fill each quarter with a different character. Here is the output when n==15:

*******........
*******........
*******........
*******........
*******........
*******........
*******........
$$$$$$$oooooooo
$$$$$$$oooooooo
$$$$$$$oooooooo
$$$$$$$oooooooo
$$$$$$$oooooooo
$$$$$$$oooooooo
$$$$$$$oooooooo
$$$$$$$oooooooo

For extra fun, make n and the characters for each quadrant command line parameters. (But the answer does not do this.)


Puzzle A24 — print a square divided into 8 horizontal bands of alternating stars and dots

[M-8]Write a main() program that prints an n by n square of characters. Divide the square into 8 horizontal bands. The bands alternate between star and dot. Declare n as a variable. Here is the output when n==16:

****************
****************
................
................
****************
****************
................
................
****************
****************
................
................
****************
****************
................
................


Puzzle A25 — print a square divided into bands four rows wide of 4 different chars

[M-12]Write a main() program that prints an n by n square of characters. Make the square out of bands that are 4 characters wide. Use four different characters for the bands, and keep repeating the bands until the square is finished. Here is the output when n==24:

************************
************************
************************
************************
........................
........................
........................
........................
oooooooooooooooooooooooo
oooooooooooooooooooooooo
oooooooooooooooooooooooo
oooooooooooooooooooooooo
++++++++++++++++++++++++
++++++++++++++++++++++++
++++++++++++++++++++++++
++++++++++++++++++++++++
************************
************************
************************
************************
........................
........................
........................
........................

If n is not a multiple of four, the last band will have fewer than four rows.


Puzzle A26 — print a square of alternating stars and dots

[M-8]Write a main() program that prints an n x n square composed of alternating star and dot characters.

*.*.*.*.*.*.*.*.*
.*.*.*.*.*.*.*.*.
*.*.*.*.*.*.*.*.*
.*.*.*.*.*.*.*.*.
*.*.*.*.*.*.*.*.*
.*.*.*.*.*.*.*.*.
*.*.*.*.*.*.*.*.*
.*.*.*.*.*.*.*.*.
*.*.*.*.*.*.*.*.*
.*.*.*.*.*.*.*.*.
*.*.*.*.*.*.*.*.*
.*.*.*.*.*.*.*.*.
*.*.*.*.*.*.*.*.*
.*.*.*.*.*.*.*.*.
*.*.*.*.*.*.*.*.*
.*.*.*.*.*.*.*.*.
*.*.*.*.*.*.*.*.*

Write the program so that it works regardless of whether n is even or odd.


Puzzle A27 — print a checkerboard divided into squares of dots and stars

[M-10]Write a main() program that prints an n by n checkerboard made up of m by m squares. Here is a 24x24 checkboard made of 6x6 squares:

******......******......
******......******......
******......******......
******......******......
******......******......
******......******......
......******......******
......******......******
......******......******
......******......******
......******......******
......******......******
******......******......
******......******......
******......******......
******......******......
******......******......
******......******......
......******......******
......******......******
......******......******
......******......******
......******......******
......******......******

You can use this program to create image files that can be viewed with image processing programs. To do this, do not output "\n" at the end of each line (don't output anything at the end of a line). Change the two characters into '!' and '~'. These characters are at the beginning and at the end of the printable ascii characters. This increases the contrast between the dark and light squares. Now redirect the output of the program to a file:

C:\> check.exe > checkerboard.raw

If you have Adobe Photoshop you can open this file using the "raw" image format. In this image format, each byte of the file corresponds directly to a pixel in the image, and the file has no header or other information in it. You may have to tell Photoshop the size of your image.

Here is what Photoshop shows for a 256x256 raw image:

Most image viewers do not, unfortunately, support raw images. You may have an image viewer that supports "portable gray map" images, PGM. If this is the case, create the file as above but put the following as the first two lines:

  P2
  256 256 255

Each line ends with "\n". The P2 on the first line says that the image is in PGM format, the next line lists the number of columns, the number of rows, and the maximum gray level.


Puzzle A28 — print a square with a wide diagonal band

[M-8]Write a main() program that prints a square with an 8 character wide band down the diagonal.

****............
*****...........
******..........
*******.........
********........
.********.......
..********......
...********.....
....********....
.....********...
......********..
.......********.
........********
.........*******
..........******
...........*****


Puzzle A29 — print a square composed of four triangles

[H-12]Write a main() program that prints a square that consists of four triangles. The base of each triangle is a side of the square, as below. When there is an odd number of columns, the triangles meet at a point:

n==17 n==18
*****************
.***************.
..*************..
...***********...
....*********....
.....*******.....
......*****......
.......***.......
........*........
.......***.......
......*****......
.....*******.....
....*********....
...***********...
..*************..
.***************.
*****************
******************
.****************.
..**************..
...************...
....**********....
.....********.....
......******......
.......****.......
........**........
........**........
.......****.......
......******......
.....********.....
....**********....
...************...
..**************..
.****************.
******************

Getting this to work may take some fussing. Hint: think about the diagonal lines.


Puzzle A30 — print a wavey line down the the page

[H-8]Write a main() program that prints a few cycles of a cosine wave down the page.

              *
             *
          *
         *
       *
       *
        *
          *
             *
              *
             *
          *
         *
       *
       *
         *
          *
             *

Hint: decide on how many cycles of the wave you want. Multiply that number by two*pi to find how many radians the wave goes through. Divide that by the number of rows to determine the number of radians for each row.


— Return to the main contents page