Lab 6 – Algorithms and Latin Squares

Lab Overview
This assignment will give you more experience on the use of both count-controlled and sentinel-controlled loops.

This week I will ask you to work with a partner of your choice.  You will turn in a SINGLE answer sheet for both partners. 

I will talk about this at the start of the lab today, but to put it in writing -- when you work with a partner you should:

Failure to follow these guidelines may cause you to lose points for this activity.
Secondly, this week you will be submitting some of the programs you create to eLearning. I will give you specific instruction on when to do this in the lab as you go.

A Latin Square is an n*n table filled with n different symbols in such a way that each symbol occurs exactly once in each row and exactly once in each column (see ). For example, two possible Latin Squares of order 6:

1 2 3 4 5 6
2 3 4 5 6 1
3 4 5 6 1 2
4 5 6 1 2 3
5 6 1 2 3 4
6 1 2 3 4 5

3 4 5 6 1 2
4 5 6 1 2 3
5 6 1 2 3 4
6 1 2 3 4 5
1 2 3 4 5 6
2 3 4 5 6 1

The top-left numbers are 1 and 3 respectively.

Activity A – Hold on!

Before we begin, let’s develop an algorithm to solve this problem!  If you were not in class yesterday, please take the time to review the lecture notes.

Get out a piece of scratch paper and write/draw out what sort of steps you need to take to solve the project specification below.  For example, how many loops do you need?  What will each loop do?  How will each loop end?  You may want to write out pseudocode, which is not quite Python code but similar English statements to help you think about the problem.

Once you have an idea of what you want to do, download this template here and create your design document.  Write down your detailed algorithm of what you will code, step by step.  Make sure you explain what each loop is going to do.

Project Description / Specification  (Just read this, do not code it)

Your program will ask user to input two numbers. The first number is the order of square; the second one is the top-left number of the square. Note that the second number should be between 1 and the first number, so your program should check this situation. Then, your program will print the corresponding Latin Square. Here is some example output:

Here are some hints:

  1. The range function and the % (modulus) operator are both useful for this project.
  2. How would you generate a sequence beginning with 1 of the appropriate order? A sequence of order 5 would be 1 2 3 4 5
  3. How would you generate a sequence of the appropriate order beginning with a number other than 1? For a sequence of order 5 starting with 3 would be 3 4 5 1 2
  4. How would you generate the second in the Latin sequence? For example, starting with 1 2 3 4 the next sequence would be 2 3 4 1

[Q1] Do you need count-controlled or sentinel-controlled loops to solve this problem?  Why?

Once you have your design document created, please print it out to submit to the TA.

[SIG 1] Please show your printed design document to the TA or instructor.

Activity B – Start coding

Code the Latin Squares project described in Activity A.  Name it  Use your design document and algorithm as a guide.  Place comments in your code from your design document to guide your coding.

[SIG 2] Once you have the program working, please raise your hand to demonstrate it to the TA or instructor.

Please submit this code on eLearning.

Activity C – Add a loop to continue

Add a loop to your Latin Squares program to ask the user to continue.  For example:

[Q2] Do you think you will need a count-controlled or sentinel-controlled loop to add this functionality?  Why?

[SIG3] We are now nearing the end of the lab. I may not have time to come over and watch you demonstrate your programs for a signature. No matter what happens, please submit the program to eLearning so that I can grade it later. Only one partner needs to submit the programs.
If there is time, please raise your hand to demonstrate your program.

Activity D – Generate all Latin Squares (extra credit)

If you get to this activity in time, you may try it for 2 points of extra lab credit.

Modify your program to take in only 1 input (the order of the square). Now, generate every possible square starting with the top left number of 1:

For example:

[SIG 4] Demonstrate this program for your extra credit signature!

Don't forget to turn in your answer sheet to an instructor before you leave to get points for the lab!!