Lab 2: Basic C++ Programming

Due: Tuesday, July 6, 2021 (11:59 PM PDT)

Introduction

The lab assignment this week will utilize concepts of input/output and simple control flow (Chapter 2 in the book).

Step 1: Getting Ready

Recall what environment you set up last week in lab 1. Either through a graphical file system explorer or through the terminal:

  1. Navigate to your cs16 directory:
    $ cd cs16
  2. Create and navigate to the lab2 directory:
    $ mkdir lab2
    $ cd lab2

Step 2: Create and Edit your C++ Files

This assignment consists of 3 problems, each of which is described below. The first two are worth 30 points each, and the last is worth 40 points. Each should be solved in its own file and all 3 must be submitted for full assignment credit. These exercises are inspired by ones in the textbook (Chapter 2) but they are not the same, so follow the instructions here carefully.

Important: Do not plagiarize. We will be able to tell!

Important: Follow the directions below carefully or you may lose points. You can probably make these programs using different approaches but we want you to create them according to the directions.

Important: For this lab, you are not allowed to use C++ syntax that has not yet been covered in class. For example, we have not discussed functions or arrays yet, so you are not allowed to use those (or other topics not yet covered) in this lab!

Program 1: descend.cpp

Write a program that takes 3 integer inputs from the user and prints them back in descending order.

Hint: Nested if-else statements can be very helpful here!

Remember: You cannot use techniques we have not yet covered in class to solve this!

A session should look exactly like the following example (including whitespace and formatting), with all manner of different values for the input and output. Note, the first line is the user input and the second line what the program outputs.

-2 8 4
8 4 -2
22 -41 22
22 22 -41

Program 2: block.cpp

Write a program that takes from the user some number of rows and number of columns, and then prints out a block of characters that is based on these 2 parameters. The program should keep asking the user for input, and printing out the result until the user enters zero (0) for each of the input parameters.

A session should look exactly like the following example (including whitespace and formatting), with all manner of different values for the input and output. Each line printed by the program should include a newline at the end, but have no other trailing whitespace (i.e. no extra space characters at the end of the line).

$ ./block
Enter number of rows and columns:
10 10
X.X.X.X.X.X.X.X.X.X.
X.X.X.X.X.X.X.X.X.X.
X.X.X.X.X.X.X.X.X.X.
X.X.X.X.X.X.X.X.X.X.
X.X.X.X.X.X.X.X.X.X.
X.X.X.X.X.X.X.X.X.X.
X.X.X.X.X.X.X.X.X.X.
X.X.X.X.X.X.X.X.X.X.
X.X.X.X.X.X.X.X.X.X.
X.X.X.X.X.X.X.X.X.X.
Enter number of rows and columns:
3 7
X.X.X.X.X.X.X.
X.X.X.X.X.X.X.
X.X.X.X.X.X.X.
Enter number of rows and columns:
0 0

Program 3: pi.cpp

Write a C++ program that approximates the value of the constant pi (\(\pi\)), based on the Leibniz formula for estimating \(\pi\). The formula is shown below and can go on for some arbitrary \(n\)-number of terms:

\[\begin{equation*} 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} - \ldots = \frac{\pi}{4} \end{equation*}\]

The formula works best for high values of \(n\).

The program takes an input from the user for the value of \(n\), which determines the number of terms in the approximation. The program outputs that calculation according to the formula. You must also include a loop that allows the user to repeat this calculation for new values of \(n\) until the user says they want to end the program by issuing an input of 0. Finally, the results must be shown to the 5th decimal place.

The number of terms is assumed to not include the added 1 in the formula. In other words,

Here is a “skeleton” program to help you get started:

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int terms = 1;
    double pivalue = 0;

    // You need to do something about the formatting requirements here!

    // You also need to do a loop here that keeps asking for a number
    // of terms and then approximates pi!

    // HINT: Part of what we want to do is decide the sign of a number.
    // There are multiple ways to do this. One way we can do this is to
    // use cmath for its pow() function, which calculates x raised to
    // the power y when used like: pow(x, y)

    return 0;
}

The program should print a string of text to the terminal before getting each piece of input from the user. A session should look like the following example (including whitespace and formatting), with all manner of different values for the input and output. Note that each line printed by the program should include a newline at the end, but have no other trailing whitespace (i.e. no extra space characters at the end of the line). When the user enters 0, there is no approximation given — the program just ends there.

Enter the number of terms to approximate (or zero to quit):
5
The approximation for Leibniz's Formula is 2.97605 using 5 terms.
Enter the number of terms to approximate (or zero to quit):
1000
The approximation for Leibniz's Formula is 3.14259 using 1000 terms.
Enter the number of terms to approximate (or zero to quit):
0

Step 3: Compile your Code

To compile your programs, we will use the g++ command as we described in previous labs. The following 3 commands will compile the 3 source files:

$ g++ -std=c++11 -o descend descend.cpp
$ g++ -std=c++11 -o block block.cpp
$ g++ -std=c++11 -o pi pi.cpp

If compilation is successful, you will not see any output from the compiler. You can then use the following commands to run your programs:

$ ./descend
$ ./block
$ ./pi

If you encounter an error, use the compiler hints and examine the line in question. If the compiler message is not sufficient to identify the error, you can search online to see when the error occurs in general.

Remember to re-compile after you make any changes to your C++ source code.

Step 4: Set up your GitHub account

You are to set up a GitHub account and link it to the class “organization” of use in future lab assignments.

Join the course GitHub organization

  1. If you don’t already have a GitHub account, create one on the “free” plan.

  2. If you don’t already have your @umail.ucsb.edu email address associated with your GitHub account, go to your GitHub account settings, add that email, and confirm it. It’s important that you use your @umail.ucsb.edu and not your @ucsb.edu email—the sign up tool won’t work otherwise.

  3. Visit our GitHub sign up tool at https://ucsb-cs-github-linker.herokuapp.com, login with your GitHub account, click “Home”, find this course (listed as cs16-m21-sisco), and click the “join course” button. That will automatically send you an invitation to join the course organization on GitHub.

  4. There should be a link to the invitation for GitHub organization for this course https://github.com/orgs/ucsb-cs16-m21-sisco. Click on the invitation link and accept. You can also go straight to https://github.com/orgs/ucsb-cs16-m21-sisco and see the invitation there (if you are logged in).

Create a repo in our class organization

For this lab and all subsequent programming assignments, you should start by creating a repo in the ucsb-cs16-m21-sisco organization. Follow these steps:

  1. Navigate to your dashboard on https://github.com. From the left drop-down menu, select the class organization.

  2. Click on the green “New repository” button.

  3. Type the name of your repo following the naming convention lab02_your-github-username. For example, if your GitHub username is jgaucho, you should name your repo lab02_jgaucho. If you are working with a partner, include your partner’s GitHub username in the name of the repo, e.g., lab02_jgaucho_cmapache.

  4. Important: Select the “Private” visibility option so that other students in the org cannot view your code.

  5. Add the “C++” .gitignore option from the drop-down menu and click on “Create repository”.

Upload your lab files into your GitHub repo

Upload the files in your lab02 directory to the new GitHub repo you just created. To do this, you would normally be physically present on a lab machine or in CSIL. However, in this online education environment you will do this from your home computer.

  1. In your web browser, navigate to your lab02 repo on GitHub. Click on the “Upload files” button.

  2. Now, select the 3 .cpp files from your local file system and upload them. Then, press the green “Commit new files” button.

  3. Navigate back to your repo to see that the files you uploaded are correctly listed. Click on it and you should see your code on GitHub’s web interface.

Step 5: Submit your programs for grading

Once you are satisfied your programs are correct, then it’s time to submit them. While working with others is OK, you still must submit your own lab. Even if you’re working with another person, do not copy each other’s code.

Log into Gradescope and select CMPSC 16 under Summer 2021, and navigate to the Lab 2 assignment. Then click on the “Upload Submission” button on the bottom right corner to make a submission.

You will be given the option of uploading files from your local machine or submitting code from a GitHub repo. Follow the steps to upload your 3 source files to Gradescope (do not upload any other files). If your files do not have those exact names, the autograder will not grade them. You can resubmit your files as many times as you like before the assignment deadline.