lab2-first-program

EECS 183 Lab 2: Your First Program

Lab attendance Due at your scheduled lab time and day, Thursday or Friday, January 28th or 29th

Lab assignment Due Monday, February 1, 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 the course beginning with Project 1.

There will also be puppies.

By completing this lab assignment, you will learn to:

Prerequisite

Attend the discussion with your lab instructor at your assigned lab time. Check here for the lab section Zoom link

Requirements

You will complete this lab in small groups of about 4 students. For all labs in EECS 183, to receive a grade, every student must individually submit the Lab Submission.

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 program 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.

Lab Assignment

Hello World

Once you have started a new project with a source file the distribution code, you will need to begin writing your new program, which will be a “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 group if working as 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 it enables 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, so that you can write your program to solve the problem at hand. This will appear in all of your labs and projects for this course and are included in the distribution code file lab2.cpp.

To get started, you need to write a new function for your program. Functions are a major topic of discussion in EECS 183, but for now you only need to know enough about functions to be able to get started with your first program. A function is a group of program statements which accomplish some task. Functions are a way we can organize your programs to make them more readable, extensible, and reusable.

For now, you need to 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: All of your other statements in your main function must come before the return statement.

int main() {

    return 0;
}

Every C++ program will have exactly one main function. Functions and their components will be explored in depth in EECS 183 in future lectures, labs, and projects. 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 will write for your function must appear between the curly braces {} which defines 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. This will have multiple parts.

NOTE: All C++ statements end in a semi-colon. However, not all lines that are written are statements such as #include<iostream> and int main() {. In C++, a statement is a program instruction. Each statement usually appears on its own line. Each program statement ends with a semicolon “;”, like each English sentence ends with a period.

A statement to print Hello World! to the terminal:

cout << "Hello World!";

Multiple cout statements will continue printing on the same output line. However, using 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 << for 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 called 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 writing statements in your program.

Define the problem

Begin by defining the problem you are tasked with solving. 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?

These questions are answered in the 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 language, but it must contain the steps required to solve the problem. You could write an algorithm using English (or any) 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 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? 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 that the requirements for the number you enter to adopt 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 value entered for 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 the statement cin >> adoptedPugs The statement reads a user-entered value and stores the value into the given variable. For this program, the user input would be the following.

cin >> adoptedPugs;

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 and see the 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 submit your solutions to the autograder web interface. Each assignment will have its own web page.

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 submission 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 receive a limited number of submissions with feedback per day.

In projects in courses at Michigan, you receive very limited submissions with feedback per day, typically four times per day. However, for this lab, you will receive 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 shows the output displayed on the same line as the cin statment, add a statement to print a new line immedaitely after using cin.

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

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

cin >> adoptedPugs;
cout << endl;