Part 1: Bounding box

It's often nice to write programs that produce output that's intended for other programs. You will write a program (note: name the source code file part1.cpp) that reads in three points from the user and prints output that the user can put in a file to give the program "gnuplot" to produce a plot showing the triangle defined by the three user-input points along with its bounding box, i.e. the smallest rectangle aligned with the coordinate axes that contains the triangle.

Gnuplot is a standard tool for creating plots. Its input is simply a text file of points, one per line, given by the point's x-coordinate, followed by a space, followed by its y-coordinate. Gnuplot will draw a mark at each point, and connect successive points with segments, as long as they are not separated by blank lines. Note: this means that drawing a closed figure (like a triangle) requires repeating the first point at the end of the input.

First, let's install gnuplot in your laptop (the lab machines already have the program installed, so skip this if you work on a lab machine):

sudo apt install gnuplot-x11

Here's a sample of running the program and using gnuplot to plot the results. Note: please print the lines defining the bounding box before the lines defining the input triangle. This is for automatic grading purposes, not because gnuplot cares.

sample runexplanation
~$ ./part1
(-1.1,-0.5) (1,3.3) (2.7,-1.1) 
-1.1 -1.1
-1.1 3.3
2.7 3.3
2.7 -1.1
-1.1 -1.1

-1.1 -0.5
1 3.3
2.7 -1.1
-1.1 -0.5


← drawing the 
 bounding box




← drawing the 
triangle


checking your output in gnuplot
  1. In your editor: copy & paste the program output (e.g., the text in black color in the left sample run).
  2. Save it as a file named tmp.
  3. Run gnuplot and give a "plot" command (see below).
    ~$ gnuplot
        G N U P L O T
        ... 
    
    Terminal type set to 'wxt'
    gnuplot> plot "tmp" with linespoints;
  4. A new window will pop up showing gnuplot's output.
Submission:
~/bin/submit -c=IC210 -p=lab03 part1.cpp

Part 2: Binary & Decimal Calculator

Write a program with source file named part2.cpp that works like this:

~$ ./part2
B0100 + B1000 = 
B1100
~$ ./part2
B0100 + D16 = 
D20
~$ ./part2
D1 + D3 = 
B0100
~$ ./part2
D1 = 
B0001

In other words, the program takes a command in one of the two forms given below:

number + number =
number = 

Here, number is binary or decimal. In particular,

Note: assume that the input is always formatted correctly, following the above rules.

As shown in the above sample executions, the program calculates the input equation and prints the output as follows:

Submission:
~/bin/submit -c=IC210 -p=lab03 part1.cpp part2.cpp

Part 3: Going further, Classifying triangles

There are two common ways of classifying a triangle:

This is an acute isosceles triangle.
  1. By describing its interior angles
  2. By describing the lengths of the sides in relation to each other
A well known property of all triangles is that the sum of any two sides will always be greater than the third. Given a set of three lengths it is possible to determine if they can form a triangle by adding two lengths together and comparing them to the third.

A corollary of the Pythagorean theorem's converse is a simple means of determining whether a triangle is right, obtuse, or acute, as follows. Let c be chosen to be the longest of the three sides and a + b > c (otherwise there is no triangle according to the triangle inequality). The following statements apply:

Tip: Think about how to determine the longest side and store it in variable c.

Example executions are shown below:

~$ ./part3 
Enter side lengths: 1 2 3 
Error! these lengths violate the triangle inequality!
~$ ./part3 
Enter side lengths: 3 1.5 2 
This is a obtuse scalene triangle.
~$ ./part3 
Enter side lengths: 2 2 3 
This is a obtuse isosceles triangle.
~$ ./part3 
Enter side lengths: 2 2 2 
This is a acute equilateral triangle.
~$ ./part3 
Enter side lengths: 2 1.5 2 
This is a acute isosceles triangle.

Submission: If you're finished before lab ends, please demo for your instructor. Also submit via the class submit script as follows:

~/bin/submit -c=IC210 -p=lab03 part1.cpp part2.cpp part3.cpp