Lab 7: Branches and Loops, Part I

Objectives

  • Learn to use if statements to implements branches
  • Learn to use while statements to implement loops

ASCII Art

Before image editors and fancy graphics became commonplace, folks would spend inordinate amounts of time piecing together artwork from the standard character set — so called “ASCII art”. This particular (and peculiar) art form retains a substantial following; you can read more about it on Wikipedia at http://en.wikipedia.org/wiki/ASCII_art. The following is a fairly complex sample:

                                                   ,....... 
                                                 ,-'      ``. 
                                               ,"'           `. 
                                  ,-"""""""""""               `, 
                                ,"                             `, 
                               ,                                `. 
                              :               ,'                 : 
                            ,-.                                  `, 
                           :  :                             ...,-,' 
                           `. :                           ,'    ,,, 
                            `"`                          .'     ' : 
                               `.                       ,.        : 
                                 `-..                  ,'         , 
                                    ``-.               :         ,' 
                                        `,--,-.        :        ,' 
                                         : `  :      ,.'`.    ,-' 
          ,-,                            :    ,'   ,-'   `"-'"' 
         ,   ',                         ,'    ,---,` 
         :..   `.                      :      :   :' 
            ",  '-.  ,-'               :      )   : 
            ,"""`-:.,'                 :      :    : 
       ..  ,       ,`--,.               :,'""-. '  ` 
      '  `,'  -.  :     :              ,''`,. `-'  : 
      :    ,,     :      :.            .      :`   : 
      `--'",,', ,"       :,`-.,.     ,' "`    :'   `. 
           `'.,-:.....,.,"    '`,,,  '       :`,  ,-". 
           ,"                      "T.       :,`,",, :.... 
     ,.,---.           -hrr-        `,      `'`,.,' ":    ""---.. 
  ,-"'     :                        '                :           "-,. 
,'         :                       ,'.              .`..            `, 
`.          `'""""""""""""""""""""-`,:.............;  , "`  .        : 
 ``..                                               "",.,'"`     ,.-" 
    `"--,...                                               ...--'' 
           `"`-----,....                       ....,-----"" 
                       `"""""""""""""""""""""""

In this lab, you will write programs to generate some simple ASCII art based on values entered by the user. To be correct, your programs should make use of loops (and, if necessary, branches) to achieve the desired output, and should not rely on “hardcoded” puts statements.

Exercise 1

Write a program that reads a number N from the user between 1 and 20 (you can assume that the user types in a valid number) and then prints out a square of N by N stars (* characters). For example, if the user enters the number ‘5′, the program should print out the following:

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

Save your program in a file named “square.rb”.

Exercise 2

Write a program that reads a number N from the user between 1 and 20 (you can assume that the user types in a valid number) and then prints out a diamond of N by N stars (* characters). For example, if the user enters the number ‘2′, the program should print out the following:

 *
* *
 *

The number ‘5′ would result in the following:

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

Save your program in a file named “diamond.rb”.

Leave a comment

You must be logged in to post a comment.