The lab assignment this week will introduce the use of void functions and command-line arguments.
Recall what environment you set up in lab 1. Either through a graphical file system explorer or through the terminal:
cs16
directory:
$ cd cs16
lab3
directory:
$ mkdir lab3
$ cd lab3
This week you will create 2 files called change.cpp
and calculate.cpp
. Each corresponds with the problems listed below. Each is worth 50 points and should be solved in its own file. Both must be submitted for full assignment credit.
change.cpp
Write a program that tells what coins to give out for any amount of change from 1 cent to 99 cents. For example, if the amount is 86 cents, the output would be something like the following:
86 cents can be given in 3 quarters, 1 dime, 1 penny.
Use coin denominations of 25 cents (quarters), 10 cents (dimes), and 1 cent (pennies) only. Do not use nickel and half-dollar coins.
Your program should use the following function (it doesn’t have to be the only function, but you have to use this one). Your program must use the compute_coins
function declaration shown here:
void compute_coins(int amount);
// Precondition: 0 < amount < 100
// Postcondition: The function prints out the number of
// quarters, dimes, and pennies needed to make the
// amount value.
The program should verify that the value of amount
is between 1 and 99 (inclusive). If this not the case, the function should print out an error message (see example below).
A loop in the program should have the user repeat this computation for new input values until the user enters a zero to quit.
A session should look exactly like the following example (including whitespace and formatting) with all manner of different numbers for the input.
$ ./change
Enter number of cents (or zero to quit):
86
86 cents can be given in 3 quarters, 1 dime, 1 penny.
Enter number of cents (or zero to quit):
99
99 cents can be given in 3 quarters, 2 dimes, 4 pennies.
Enter number of cents (or zero to quit):
1
1 cent can be given in 1 penny.
Enter number of cents (or zero to quit):
101
Amount is out of bounds. Must be between 1 and 99.
Enter number of cents (or zero to quit):
0
Important: Note that the words and the punctuation (commas, especially) can be different for different kinds of input values. They have to match the numbers, so singulars for “quarter”, “dime”, and “penny”, and plurals for “quarters”, “dimes”, and “pennies”. The autograder will not tolerate bad grammar!
Hints: It will be helpful to use lots of if-else statements and you will have to include the <string>
library to make variable message to meet the singular/plural and comma/no-comma requirements.
calculate.cpp
You will write program that mimics a simple calculator that can do one of three operations on two integers: addition, multiplication, or modulo.
The program takes 3 arguments at the command line: an integer, a character, and another integer. All of these arguments must be separated by a space character. The middle character can only be 1 of 3 choices: +
, x
, or %
. The program then returns either the sum, the product, or the modulo of the 2 integers, respectively.
The program should be able to verify that:
For each of these conditions, the program should print out a specific error message (called the “usage” message) and exit. For each condition listed above, the error messages should respectively be:
Hint: You have to use cerr
instead of cout
to output the usage messages. Do not use cerr
for any other outputs the program makes. While cerr
and cout
are alike in that they direct values to standard output, we usually use cout
for the standard output, but use cerr
to show or report errors to the user and the system.
You may use the exit(1)
statement when exiting from giving some of the error messages.
Here is a skeleton program to help get you started.
// calculate.cpp
// By: <your name here>
#include <iostream>
#include <cstdlib>
using namespace std;
// Usage: ./calculate int char int
// char can either be: + x or %
int main(int argc, char *argv[]) {
// PART 1: Check to see if the number of arguments is correct
// Hint: use "if (argc ...)" to check this,
// use cerr to output any messages
// PART 2: Convert arguments into integers (only those that need it!)
// Hint: this means using atoi()
// PART 3: Check for illegal operations like divide by zero...
// use cerr to output any messages
// PART 4: Do the appropriate calculation,
// outputs using both cout and cerr
return 0;
}
This program does not loop back to take inputs again. Your outputs should match the ones in the example run shown below.
$ ./calculate 4
Number of arguments is incorrect.
$ ./calculate 1 + 2
3
$ ./calculate 7 x -5
-35
$ ./calculate 4 - 3
Bad operation choice.
$ ./calculate 14 % 0
Cannot divide by zero.
In order to learn another way to manage our source code and their compilations, we will create a makefile
and put the usual g++
commands in it. Afterwards, whenever we want to (re)compile our programs, the Linux command make
is a lot shorter—so this is a convenience. The use of makefiles will be very useful the more complex our programs and CS projects become. We will discuss make
and makefiles in class soon!
Using your text editor, create a new file called makefile
and enter the following into it:
all: change calculate
change: change.cpp
g++ change.cpp -o change
calculate: calculate.cpp
g++ calculate.cpp -o calculate
Then from the terminal, you can issue a command for either program, like so:
$ make change
Or,
$ make calculate
Or, you can issue one command that will compile both projects mentioned in the makefile.
$ make
If the compilation is successful, you will not see any output from the compiler. You can then run your programs. For example,
$ ./change
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 the relevant files after you make any changes to the C++ code.
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 3 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 both source files to Gradescope (do not upload any other files). If your files do not have those exact names as presented in this lab, the autograder will not grade them. You can resubmit your files as many times as you like before the assignment deadline.