EECS 183 Labs
EECS 183 Lab 5: Loops
Lab due on your scheduled lab day (Sept 24 - 30)
Lab accepted for full credit until Tuesday, October 1, 2024, 11:59 pm Eastern
Direct submission link
In this lab, you are writing code and solving practice exam questions to master the use of loops.
By completing this lab assignment, you will learn:
- To understand how loops work in C++.
- To practice writing answers to exam questions and receive early self-assessment of preparedness for the exams.
You should complete this lab in small groups of about four students. For all labs in EECS 183, to receive a grade, every student must individually submit the Lab assignment.
Starter Files
You can download the starter files using this link.
The IDE setup tutorials for Visual Studio and XCode include a video about how to set up a project using the starter files. You can access the tutorials here:
After downloading and unzipping, you will find the following files in the starter-files folder:
loops.cpp
: source code file you will use for the Loops exercise.loops.h
: header file you will use for the Loops exercise.test.cpp
: source code file you will use for the Loops exercise.
IMPORTANT: For this lab, you must include all three files in a single project in Xcode or Visual Studio.
Lab Assignment
Tasks to Complete
- To complete this portion of the lab, you need to do the following steps:
- Start a new project with your IDE using the starter files according.
- Stub all of the functions in
loops.cpp
- see Function Stubs for details. - Write your test cases in
test.cpp
to test the three print functions you will implement inloops.cpp
. - Write the implementations for the functions in
loops.cpp
:printRectangle()
printRight()
printRightJustified()
printIsosceles()
- Test your functions using the test cases you have written.
- Submit your
loops.cpp
andtest.cpp
to the autograder.
Multiple Files
-
Most programs in the real world are written in more than just one file so as to break down the functionality into smaller parts and to keep the program’s organization clean. As you start writing more complex and more involved (and more exciting!) programs, you too will work with multiple files.
-
For Project 1, you worked with just one file, such as
focaccia.cpp
. It had amain()
function, where the execution began, and then some other functions that were called frommain()
or from other functions. But as a program gets more complicated, you can imagine that the file would just get longer and it would be much more difficult to keep it organized, let alone test the program. -
And so a common practice is to put (at least some) functions into separate files. The functions declarations (aka prototypes) go in what are known as header files that end in
.h
, and the function definitions (aka implementations) go in.cpp
files. Each.cpp
file will#include
the.h
files that contain the declarations for the functions it implements or calls. For example, in this lab you will find#include "loops.h"
in theloops.cpp
file.
Function Stubs
A function stub is a temporary substitute for a function that needs to be implemented. In all projects and labs so far in EECS 183, we have provided all of the function stubs for you. Starting with this lab, you will need to create function stubs for all of the functions you will implement.
- For example, here is a function stub for
printRectangle()
:
void printRectangle(int rows, int cols) { // TODO - implement return; }
- If you submit to the autograder without all function stubs or completed definitions, you will see an error for any missing function stub or definition. If you see an error message like
undefined reference to printRectangle(int, int)
it is a missing or incorrect function stub for the function named.
Testing
-
We have started the function
testPrintRectangle()
for you. -
You will add more test functions, such as
testprintRight()
. Do not forget to call your test functions withinint main()
oftest.cpp
.
Bugs To Expose
There are a total of 4 unique bugs to find in our implementations of loops.h
functions. Your tests will need to expose all of the bugs to receive full points for the lab. The autograder will tell you the names of the bugs that you have exposed, from the following set:
- PRINT_ISOSCELES
- PRINT_RECTANGLE
- PRINT_RIGHT
- PRINT_RIGHT_JUSTIFIED
printRectangle()
IMPORTANT: Differences in whitespace do matter for this function.
- This function prints a series of
*
that form a rectangle
/** * Requires: rows must be > 0, * cols must be > 0 * Effects: prints a rectangle of *'s * prints an endl at the end of each row * */ void printRectangle(int rows, int cols);
- For example…
printRectangle(3,2); cout << endl; <== prints a blank line printRectangle(1,3); cout << endl; <== prints a blank line
- Should print…
** ** ** <== blank line *** <== blank line
- Notice that…
- There should be no spaces at the end of lines.
printRectangle()
should print no blank lines.- The blank lines in the example above are done through the testing code.
printRight()
IMPORTANT: Differences in whitespace do matter for this function.
- Prints an isosceles right triangle of
*
/** * Requires: n must be > 0 * Modifies: nothing * Effects: prints a right triangle * prints an endl at the end of each row * */ void printRight(int n);
- For example…
printRight(3);
- Should print…
* ** ***
- Notice that…
- There are no spaces at the end of lines.
printRight()
prints anendl
at the end of each row.
printRightJustified()
IMPORTANT: Differences in whitespace do matter for this function.
- This function prints an isosceles triangle that is right justified.
/** * Requires: n must be > 0 * Modifies: nothing * Effects: prints a right justified triangle * prints an endl at the end of each row * */ void printRightJustified(int n);
- For example…
printRightJustified(3);
- Should print…
* ** ***
- Notice that…
- There are no spaces at the end of lines.
printRightJustified()
prints anendl
at the end of each row.
printIsosceles()
IMPORTANT: Differences in whitespace do matter for this function.
- This function prints an isosceles triangle that is not right or justified.
/** * Requires: n must be > 0 * Modifies: nothing * Effects: prints an isosceles triangle (pointing up) * where parameter n is the height * prints an endl at the end of each row * **/ void printIsosceles(int n);
- For example…
printIsosceles(3);
- Should print…
* *** *****
- Notice that…
- There are no spaces at the end of lines.
printIsosceles()
prints anendl
at the end of each row.
How to Submit
- When ready to submit to the autograder, visit https://autograder.io/web/project/2677. You will submit your
loops.cpp
andtest.cpp
files which should contain your solution for this lab as one program.
IMPORTANT: For all labs in EECS 183, to receive a grade, every student must individually submit the Lab Submission. Late submissions for Labs will not be accepted for credit. For this lab, you will receive ten submissions per day with feedback.
- Once you receive a grade of 10 of 10 points from the autograder you will have received full credit for this lab.