EECS 183 Labs

EECS 183 Lab 2: Your First Program

Lab Due Wednesday, September 15, 2021, 11:59 pm Eastern

In this lab, you are writing your first program in C++. As many aspiring computer scientists have, you will begin by writing the most common first program when learning a new language: Hello World. Then you will create a short, interactive program. While working on the lab, you will learn some of the tools you need to master Project 1 and beyond.

There will also be puppies.

By completing this lab assignment, you will learn to:

Requirements

You will complete the lab in teams of around 4 students. For all labs in EECS 183, each student must submit their code individually to the autograder to receive a grade. If unable to work in a team, you may work independently.

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:

Lab Assignment

Your task for this lab is to create your first program in C++. Your initial program will display the message “Hello World!”, the canonical first programming experience. However, in software engineering, the majority of effort put into commercial software occurs after the initial release to the customers. So, we will then update your new Hello World program to create a Puppy Adoption Program.

Hello World

Once you have started a new project with a source file — the distribution code file lab2.cpp — you will begin writing your “Hello World!” program.

Program Comment

First, start by modifying the block comment at the top of the file.

/**
 * lab2.cpp
 *
 * <#Name(s)#>
 * <#Uniqname(s)#>
 *
 * EECS 183: Lab 1
 *
 * <#description#>
 */

Replace the #Name(s)# and #Uniqname(s)# with your name and uniqname, along with the names and uniqnames of your teammates if you are working in a team. Also, replace the #description# with a (very) brief summary of the lab.

As you can see from the lab2.cpp file, there are only two lines of C++ source code, which appear below the comment.

The following lines will appear in all of your labs and projects for this course (at the top of the file, before the program statements you write) and are included in the distribution code file lab2.cpp.

#include<iostream>
using namespace std;

You do not need to understand precisely how these lines work in order to use them in your programs. You only need to know that they enable you to use programs someone else has already written. The above lines will allow you to write text and receive input from the terminal of your computer. The details of how that is accomplished are abstracted away, allowing you to focus on writing programs that solve the problem at hand.

To get started, you need to write a new function for your program. A function is a group of program statements which accomplish a certain task. Functions are a way we can organize our programs to make them more readable, extensible, and reusable.

Now, we will write a function named main, which is required for all C++ programs. This function is where you will write your program statements for the lab.

Every function you write should end in a return statement. A function ends when all of its statements have executed, or when executing a return statement. The 0 in the statement return 0; is a return value which tells the Operating System (e.g., Windows) that your program ended without an error.

NOTE: The return statement should be the last statement in your main function; all other statements go before it.

int main() {

    return 0;
}

Every C++ program will have exactly one main function. Functions and their components will be explored in depth in future lectures, labs, and projects — they are a major topic in the course. For this lab, you only need to know that you must have a function with the name main, and the above code is how it is written. Add it to your lab2.cpp file now.

NOTE: All of the C++ statements you write for the function must appear between the curly braces {}, which define the scope of the function.

At this point, you will have completed a most basic program in C++. It does not accomplish the task of the lab. However, it is a complete program which you can run with your IDE. Run your program now. To review how to run a program using your IDE, see the guide for Getting Started with Visual Studio or Getting Started with Xcode. When you run your program, there will be nothing printed at the terminal and there should be no error messages.

IMPORTANT: Testing your code as you write it is an essential way to ensure that your code is correct. It will take less time overall when completing your projects.

Once you have a working program, it is time to write the code which will display Hello World!. To accomplish this, write the cout statement, which will have multiple parts:

NOTE: All C++ statements end in a semicolon, like how all English sentences end with a period. Not all lines in your code are statements — e.g. #include<iostream> and int main() { are not. In C++, a statement is a program instruction. Each statement usually appears on its own line.

A statement to print Hello World! to the terminal:

cout << "Hello World!";

Multiple cout statements will continue printing on the same output line unless an endl is used. An endl will start the next output on a new line.

cout << endl;

Printing multiple things can be done with one cout statement by using the insertion operator << before each item to be printed.

cout << "Hello World!" << endl;

Now, you have seen all the C++ source code necessary to write a “Hello World!” program. Add the cout statement and run your program. Confirm that you see Hello World! in your terminal. If you are not seeing Hello World!, verify the following:

NOTE: When using Visual Studio, you may see the terminal momentarily open and then immediately close. When this happens, add a breakpoint to your program. See the section for Running the Program in the Getting Started with Visual Studio documentation.

Once you have tested your program and verified it prints Hello World!, move on to the next part of the lab.

Puppies

In this part of the lab, you will modify your Hello World! program. The term for a group of Pugs is a “Grumble”. Write a program to manage how many Pug puppies are adopted to enjoy the summer outdoors with some playful cheer.

Your task for this part of the lab is to create a program which keeps track of how many Pugs are adopted from your home, where you have successfully raised twenty puppies. Pug-adopting program should do the following:

When approaching a problem or writing a new function, there are five steps you should take. You should not begin by immediately writing statements in your program.

Define the problem

Begin by defining the problem you are tasked with solving. Take time to understand what you are attempting to accomplish. What is the purpose of the program? What do you need to provide the program for it to work? What do you expect to be the effects of running the program? What might be modified?

These questions are answered in the Pug Adoption Program problem statement above. Consider what input your program will take and what it will print, then proceed to creating an algorithm.

Writing an Algorithm

An algorithm is a series of steps used to solve a problem. You will need an algorithm to write the Pug Adoption Program. An algorithm can be written with different languages, but it must contain the steps required to solve the problem. You could write an algorithm using English (or another language) prose. However, it is more helpful to write an algorithm using pseudocode. Pseudocode is an informal description of an algorithm.

Write an algorithm for the problem using the pseudocode from lecture as a guide. It is important to note that there is no universal pseudocode, and yours may look a little different.

Start by breaking down the program description.

From the Hello World! program, you can see that you will use a cout statement to print this message. But for now, just write the step in the algorithm as pseudocode.

1. Print "Pug Adoption Program"

The next part of the program description was:

There is more than one task in this statement. Add them to your algorithm.

1. Print "Pug Adoption Program"
2. Print "How many Pugs will be adopted today? "
3. User enters the number of Pugs

Unfortunately, this is still too vague to describe well enough how to solve the problem. How will the user enter the number of Pugs? What will we use to represent that number? Reviewing the algorithm to count the number of people in a room, you see the statement Let N = 0, which can be used as an example. Update our pseudocode to be more descriptive of what steps you will take in your algorithm.

1. Print "Pug Adoption Program"
2. Print "How many Pugs will be adopted today? "
3. Let numPugs = 0
4. Input the numPugs

The above pseudocode describes the steps to adopt the puppies. Now, revisit the last line of the program description to finish the algorithm.

As before, this line contains multiple tasks. Now, write your pseudocode for the remaining tasks, which include:

Your algorithm should now contain all of the steps necessary to solve the problem. You can also see how it can be more readily translated into C++ source code for your program.

How to Test

Before writing the C++ code for the program, first stop and consider how you will test the program you write. What do you expect the effects of your program to be? If your program takes input, what will the results be for a range of inputs? For instance, given the original Grumble of 20 Pugs, what would you expect the program to print if you enter 12 for the number of Pugs to adopt? For this program, you can assume the number of Pugs to adopt entered will always be between 0 and 20 (inclusive) so you do not adopt out more Pugs than you have.

NOTE: It is important to consider how to test your program before writing the code using your IDE. This method is called test-driven development and is the best approach to writing programs.

In this case, we would expect the prompts we have indicated to appear in the terminal, and if we input 12 Pugs to adopt, the effects of the program should be to tell us there are 8 Pugs remaining.

Translate your Algorithm to C++

Starting with the Hello World! program you have already written, add your C++ code. Here’s what the first few lines from your algorithm might look like in C++.

NOTE: Remove the cout statement for Hello World and put your new code in its place, but before the statement return 0;.

cout << "Pug Adoption Program" << endl;
cout << "How many Pugs will be adopted today? ";
int adoptedPugs = 0;

The first two statements are similar to how you printed Hello World!. The third statement is declaring a variable to hold the number of Pugs adopted. A variable represents a memory location used to store data. The location is like a “box” that can store a value. The statement int adoptedPugs = 0; defines a new variable named adoptedPugs and gives it an initial value of 0.

IMPORTANT: Stop at this point and run your program to test what you have completed. You should see the prompts printing in the terminal, and your program should run without errors.

Once you have tested your program, move on with the rest of your algorithm.

C++ Input

In C++, reading input is achieved using a statement as such, which we will use in our program:

cin >> adoptedPugs;

The statement reads a user-entered value and stores the value into the given variable.

IMPORTANT: For the statement cin >> adoptedPugs; to work, the variable declaration int adoptedPugs = 0; must always come before the cin statement. In C++, all variables must be declared before they can be used. This is true for all variables you will use in your programs.

Now, test your program by running it. Before you move on, you should be able to run your program without errors, see statements printing to the terminal, and be able to type input to the program. Once your program runs and you can enter a number, move on to writing the remainder of the program from your algorithm.

Sample Output

The input to the program is given in bold red in these sample runs.

Sample Run 1

Pug Adoption Program
How many Pugs will be adopted today? 12

Pugs remaining: 8

Sample Run 2

Pug Adoption Program
How many Pugs will be adopted today? 6

Pugs remaining: 14

Sample Run 3

Pug Adoption Program
How many Pugs will be adopted today? 0
Pugs remaining: 20

Autograder

The autograder is used in EECS 183 to submit your labs. It is also used to determine grades earned on the labs. For all labs in the course, you must submit your solutions to the autograder web interface. Each assignment will have its own autograder webpage.

How to Submit

Note: If you’re using Xcode and don’t know how where exactly lab2.cpp is located on your disk, right click (or click while holding down the Control key) on the file in the Navigator area on the left side of the Xcode window and choose Show in Finder.

If you’re using Visual Studio and would like to know where lab2.cpp is on your disk, right click on lab2.cpp in the tab above the Code pane and choose Open Containing Folder.

IMPORTANT: Late submissions for labs will not be accepted for credit.

For all projects and labs in the course, we grade your highest submission score. In future projects, you will be allowed a limited number of submissions with feedback per day.

In projects in EECS courses at Michigan, you are allowed a very limited number of submissions with feedback, typically four per day. However, for this lab, you are allowed ten submissions per day with feedback. Once you receive a grade of 5 of 5 points for lab2.cpp from the autograder, you will have received full credit for this lab.

Autograder Feedback

You can see the grade and feedback from your submissions by selecting My Submissions in the upper-left corner. Selecting a submission from the left tab will show the tests and feedback for the lab. When your submissions fail one or more tests, feedback about the test will be displayed. The autograder will show a line-by-line difference between the exepcted output for a correct solution and the output of your program.

IMPORTANT: Differences in blank lines will not cause your program to fail tests for this lab. There must be another reason for the test to fail — look closer.

If your output is displayed on the same line as the input prompt, add a statement to print a new line immediately after using cin.

For example, if your output looks like this on the autograder:

Then add the following additional cout statement after the cin statement.

cin >> adoptedPugs;
cout << endl;