Lab 2: Patterns

What You Should Know Coming Into Lab:

You should have seen loops in some other language before coming to this class. Reviewing section 4 on "Loops" in Zybooks will help you prepare for this lab. You can complete the lab using any of the three main different types of loops:

Getting Started:

  1. As is our practice, in each lab you must work with a partner, taking turns being the driver and navigator.

  2. Please get ready to take the quiz as soon as you arrive, as the quiz (see links below) will be given within the first few minutes of lab.

  3. The starting code and test cases are found in section 4.29 of Zybooks, which is where you should complete your work.

  4. Please do not hardcode to the test cases provided! (This will result in a zero from this lab and onwards)

Quiz:

The quiz will be available during the first few minutes of lab. Your TA will give you the password for your lab section. You may use either your own laptop or one of the lab machines. (For the lab computers your account name and password are the same as your UIC NetId and Password.) Please click on your Lab time below.

9am, 10am, 11am, 12pm, 1pm, 2pm, 3pm, 4pm, 5pm

Important: After you have finished your quiz, if you have not already done so, use this google form to enter your information and acknowledge that you have read the Academic Honesty policy on the course web site, and that you agree to abide by it.

Activity:

Write a program to display a selected geometric figure by printing out a series of '*'. Once your code is complete be sure to run the test cases within Zybooks, after which you should ask one of the TAs to verify your output from the code you and your partner created. Each different kind of figure has a test case you can see plus one that is hidden.

Your program will prompt for the type of figure as well as an odd number for the size, and will use that information to display the appropriately sized pattern.

Level 1: Square (1 Point)

Take the odd integer input as the length of a side and output a square pattern. For example:

Running it again and supplying a different size should result in a different sized figure being displayed. Your program should use the input size to work correctly for any valid input, both for this case and subsequent cases in this program.

Level 2: Triangle (1 Point)

Take an odd integer input as the max triangle width and output a triangle pattern. For example:

Level 3: Diamond (Extra Credit, 1 Point)

The pattern maker takes an odd integer as the maximum width and outputs a diamond pattern. For example:

Hint: You can use setfill(' ') along with setw(n) to display n spaces.

Variations:

  • Instead of the space character in setfill(' ')you could use something else such as setfill('*').

  • Instead of putting a number for n in setw(n) you could use a variable there.

Consider the following example using setfill(' ') with setw(n) in a cout statement to give the output: *** Done.