CSCI 136
Fundamentals of Computer Science II
Spring 2020

Montana Tech of The University of Montana
Computer Science & Software Engineering



Lab Assignment 9

ASSIGNMENT 9

The goal of this assignment is to get experience working with C++. There are a series of small programs - all of them ones that you have already done in Python in CSCI 135, if you took it last semester. Each one was chosen to give you practice working with different C++ constructs. I want you to do all of your work on lumen so that you get practice using Linux and command line compilation.

You will need to know a few things before you begin. First of all, I don't think Linux cares about how you name your programs, but to keep things understandable in your directory, name your source code files with the extension .cpp (for now - later we will look at header files with a different extension).

To compile a C++ program on lumen use:

g++ MyProgram.cpp
The output of this command (if it is successful) will be an executable file named a.out. Now, that's not a very handy name, so in order to have an executable that is named something meaningful, use:
g++ -o MyProgram MyProgram.cpp

What the -o option does is to say put the output (executable file) in a file named... whatever you named it, MyProgram in this case.

When you get compilation errors (and you will), look at the topmost error first and fix it. Often times fixing the first error will alleviate a whole list of subsequent errors. Also, the compiler will try to give you the line and column number of where the error occurred. Pay attention to these. And if you can't find the error on that line, look at lines preceding it.

To get command line arguments into your program, you will need to modify the definition of the main function that we used often in class. Instead, use:

int main(int argc, char *argv[])
The integer argument argc will be the count of the arguments read from the command line and argv will be (just like Python!) an array of strings, one for each argument. If these command line arguments are numeric, you will need to convert them from strings to numbers. Use the stringstream function that we looked at in "class" on the videos (part 2, I believe).

Part 1: Distance Between Two Points
The straight line distance between two points (x1, y1) and (x2, y2) on a Cartesian coordinate plane is given by the equation:



Your program will take in the values of the x1, y1 and x2, y2 points as integers from the command line. It must then calculate the distance between the two points. For example, if the input was 0 0 3 4, your program would calculate and output the distance between (0, 0) and (3, 4) which is 5. Of course, the answer should be something else if the arguments on the command line are different.

In C++, to take the square root of a number, say x, use the function below

  
#include <cmath>

...

sqrt(x)
The output from your program should be formatted exactly as below, with the command line input shown between parentheses and the result of your equation at the end:
The distance between (0, 0) and (3, 4) is 5
Name your program Distance.cpp.

Part 2: while Loops
Write a program named GuessUntil.cpp that generates a random number between 1 and 100. To generate random numbers in C++, you will need to import two files. The rand() function is in stdlib.h and the srand function uses the time function found in time.h. First you need to seed the random number generator with a new number. Using the system time helps guarantee that you will get a different number each time. If you don't do this, you will always get the same "random" numbers. To get a random number in the range of 1-100, for example, use the code below:

  
#include <stdlib.h>
#include <time.h>

...

// seed the random number generator
srand (time(NULL));
// generate a random integer between 1 and RAND_MAX, mod by 100 to get the
// correct range (0-99), and add 1 to get 1-100
int number = rand() % 100 + 1;
The user can enter guesses at the number until they get it right. To do this, we need a way to get user input, not just command line input, into your program. Look at the lecture from Monday to see how to get interactive user input. The user should know what they are expected to type at the keyboard, so you should always print out a line (a prompt) that tell the user what is expected.
Enter a number between 1 and 100: 
    
Your program should use some form of a while loop to continue getting user guesses until they finally guess the number that was randomly generated. You can decide whether a while loop or a do...while loop is more appropriate. At each user guess, you should let the user know if their guess was too high or too low. When the user finally guesses the correct number, print out:
You got it! 

Part 3: for Loops
Rewrite your previous program so that the user only gets three guesses, but this time only guess a random number between 1 and 10. Name this program GuessThree.cpp. Use a for loop in this program. Each time the user guesses wrong, you should print out whether the guess was too high or too low. If they get it right you should print out "You got it!"

If the user guesses correctly before their three guesses are used up, you should exit the loop without using any type of break command.

Here is an example output when the user guessed on the second try:

Enter a number between 1 and 10: 5
Your guess was too low.
Enter a number between 1 and 10: 7
You got it!
Hint: You can use any statement that evaluates to a boolean value in the condition part of a for loop, including compound statements that use && and ||.

Grading Each of the three programs is worth 10 points each, for a total of 30 points. For each program, you will be graded according to the following criteria:

Grade ItemDistanceGuessUntilGuessThreePoints Earned
Program Compiles
2
2
2
Program Runs
2
2
2
Header Comment
2
2
2
Program Runs Correctly
(including correctly formatted output, and uses the constructs called for in the description)
4
4
4


Submission. Submit all three programs Distance.cpp, GuessUntil.cpp, and GuessThree.cpp via Moodle. Be sure each submitted source file has the required header with your name, and a description of the program. Also be sure to submit the .cpp source files and not the executable files! Note: in order to do this, you will likely need to use winscp to copy files from lumen to your local machine.

Page last updated: December 28, 2020



Page last updated: December 28, 2020