Microsoft Word - M4 - L6 - Loop Structures.doc
Page 1 of 4
CSE 1321L: Programming and Problem Solving I Lab
Lab 7
Repetition Structures
What students will learn:
• Using nested for-loops
Overview: If there’s one thing computers are good at, it’s repeating something over and over. The
concept of repetition (which some call “iteration” and others “looping”) is not terribly difficult since
we humans repeat things in our daily lives. Any decent programming language is going to support
iteration and usually allows for three different kinds of “looping templates”. These templates are
exactly what this lab is going to cover.
The three kinds of loops we’ll cover are the for, while and do-while loop. You want to memorize
the templates for these. Before that, it’s important to know when to use them. Here’s an overall
guideline to help you out:
1. Use a for loop when you want to repeat something a certain number of times. For example,
if you want to repeat something 100 times, and a for loop is a good candidate for that. Or, if
you wanted to count from 50 to 3000 in increments of 10, you could do that too.
2. Use a while loop is useful when you don’t know how many times something will repeat; the
loop could “go on forever”. As an example, if you ask a user to enter a number between 1-
10 and they consistently enter 45, this could go on forever. Eventually (and hopefully), the
user would enter a valid number.
3. Use a do-while loop when the loop must execute at least one time. The loops above can
execute 0 times, but not this one! The reason is because, for all loops, there is a test to see
if the loop should continue repeating. With a do-while loop, that test is at the bottom.
In this Lab we are specifically concerned with the for-loop. In addition to what we learned in the last
Lab, for-loops just like the if-statements can be nested and can be used to produce some
interesting results.
Nested for-loop statements allows us to traverse 2-D arrays, which we will learn about later in the
semester. Amongst other things, it can allow us to draw interesting shapes with only a few lines.
In this lab we have three exercises, the first will ask you to draw a “n” x ”n” grid using stars (where
“n” is a user input), the second will ask you to code a program that will draw a right triangle that
has its hypotenuse side on the right, and lastly you are going to be asked to code a program that
will draw a right triangle that has its hypotenuse side facing the left.
Intentionally Left Empty, please proceed to Next Page
Page 2 of 4
Lab7A (Warmup): Please write a program that asks the user for a value. Based on the value given to
the program by the user please use nested for-loop to draw a box that has the length and the width of
the value specified by the user.
Remember, the class name should be Lab7A.
The user input is indicated in bold.
Sample Output #1:
Please enter a value for the size: 4
This is the requested 4x4 box:
****
****
****
****
Sample Output #2:
Please enter a value for the size: 5
This is the requested 5x5 box:
*****
*****
*****
*****
*****
Sample Output #3:
Please enter a value for the size: 8
This is the requested 8x8 box:
********
********
********
********
********
********
********
********
Intentionally Left Empty, please proceed to Next Page
Page 3 of 4
Lab7B: Right-Triangle (Right): Please write a program that asks the user for a value. Based on the
value given to the program by the user please use nested for-loop to draw a right-triangle that has the
length and the width of the value specified by the user. Make sure that this right-triangle’s vertical side
is facing left and its hypotenuse is facing right. Please refer to the below sample outputs for examples.
Remember, the class name should be Lab7B.
The user input is indicated in bold.
Sample Output #1:
Please enter a value for the size: 4
This is the requested 4x4 right-triangle:
*
**
***
****
Sample Output #2:
Please enter a value for the size: 5
This is the requested 5x5 right-triangle:
*
**
***
****
*****
Sample Output #3:
Please enter a value for the size: 8
This is the requested 8x8 right-triangle:
*
**
***
****
*****
******
*******
********
Intentionally Left Empty, please proceed to Next Page
Page 4 of 4
Lab7C: Right-Triangle (Left): Please write a program that asks the user for a value. Based on the
value given to the program by the user please use nested for-loop to draw a right-triangle that has the
length and the width of the value specified by the user. Make sure that this right-triangle’s vertical side
is facing right, and its hypotenuse is facing left. Please refer to the below sample outputs for examples.
Remember, the class name should be Lab7C.
The user input is indicated in bold.
Sample Output #1:
Please enter a value for the size: 4
This is the requested 4x4 right-triangle:
*
**
***
****
Sample Output #2:
Please enter a value for the size: 5
This is the requested 5x5 right-triangle:
*
**
***
****
*****
Sample Output #3:
Please enter a value for the size: 8
This is the requested 8x8 right-triangle:
*
**
***
****
*****
******
*******
********
Instructions:
• Programs must be working correctly.
• Programs must be saved in files with the correct file name.
• If working in Java or C#, class names must be correct.
• Programs must be working and checked by the end of the designated lab session.
• Programs (only .java, .cs or .cpp files) must be uploaded to Gradescope by due date.
Assignment 3 - SU 2020
CSE 1321L: Programming and Problem Solving I Lab
Assignment 4 – 100 points
Iteration
What students will learn:
1) Problem solving
2) Using loops
3) Using logic within loops
4) Nested loops (loops inside of loops)
Overview: If there’s one thing that computers do well, it’s repeating things. In fact, most of
what your computer does is simply “loop” waiting for some kind of input. For this assignment,
you’re going to use loops to solve some basic problems, and we hope you have fun while you’re
doing it. The programs aren’t long but will require you to think critically. Again, start early,
practice, and ask a lot of questions.
Follow the same conventions for class names and file names for your source code. For the Java
folks, remove the “package” statement if you have one. Make sure to follow the FYE
Submission Guidelines. Finally, we don’t mean to lecture, but we want to remind you not to
cheat. This is the core of what a lot of you will be doing for a living, so master it now.
Assignment4A: Let’s Play Bunco! Now that we are able to repeat actions easily with loops,
we can program a wide variety of traditional games like Poker and BlackJack. For this
assignment, we are going to create a simplified version of the dice game Bunco. The real game
involves multiple players rolling dice and trying to get the highest score at the end of the round.
For our assignment, we’ll just have two players – you and the computer.
Each game will run for no more than 6 rounds. The player and the computer will “roll” their two
dice (represented by two integer variables per player). Each dice roll will be simulated by a
random number generator that produces a random number between 1 and 6. Once all four
integer values are generated, a score will be calculated for each player using these rules:
Result Points
Both dice match the current round number 21 points + the sum of the two dice
Both dice match each other, but not the round 5 points + the sum of the two dice
One dice matches the current round number 1 point + the sum of the two dice
Anything else The sum of the two dice
For example, if the player rolls a 1 and 2 on the first round, their score would be 4 (one bonus
point for one dice matching the round number, then the value of the dice added together). If the
computer rolls 2 and 2, then its score would be 9 (five bonus points for both dice matching each
other but not the round number, then the value of the dice added together).
https://ccse.kennesaw.edu/fye/policies.php
https://ccse.kennesaw.edu/fye/policies.php
https://ccse.kennesaw.edu/fye/submissionguidelines.php
https://ccse.kennesaw.edu/fye/submissionguidelines.php
After each round, the player is given the option to stop. If they choose to do so (or if six rounds
have been played), their current score is compared against the computer’s current score and a
winner is declared. Then the player is asked if they’d like to play again, restarting the whole
process.
Hints: Rounds could also be thought of as “loop iterations”. One particular looping structure
could easily provide us with the round number for comparison and a way to limit the number of
rounds played automatically.
Call the file Assignment4A (.java, .cs, .cpp) and the class name Assignment4A.
Two examples of gameplay are shown below. Note that due to using a random number
generator, you may not get the same numerical results – but the formatting and wording should
be the same. User input is indicated in bold.
Sample Output #1:
Let’s play Bunco!
Round 1: You rolled 1 and 5.
Your score is 7. Do you want to stop?: N
Round 2: You rolled 3 and