HOMEWORK 02 - ARITHMETIC, CARS, and TRIANGLES
Concepts
The goal of this homework assignment is to provide you with more experience on programming variables, constants, basic operators, and arithmetic expressions.
Get Started
To get started, create an empty project called HW02 and copy the code below for your main.cpp.
/* CSCI 261 HW02: Arithmetic, Cars, and Triangles
* Author: XXXX (_INSERT_YOUR_NAME_HERE_)
*
* Add more complete description here...
*/
#include <iostream> // For cin, cout, etc.
using namespace std; // For standard namespace
int main() {
// PART I: INSERT YOUR CODE BELOW HERE
// PART I INSERT YOUR CODE ABOVE HERE
// PART II: INSERT YOUR CODE BELOW HERE
// PART II INSERT YOUR CODE ABOVE HERE
// PART III INSERT YOUR CODE BELOW HERE
// PART III INSERT YOUR CODE ABOVE HERE
return 0; // signals the operating system that our program ended OK.
}
There are three short programming assignments with
this homework assignment. You should put your
solutions to all three of these short assignments
in one main()
(i.e., in your HW02 Solution/Project). Please
use comments above to see where code from each assignment begins/ends.
Before moving on to the three parts, take time to update the header comments at the top of your main.cpp file. Also, make sure everything runs fine before moving on to the next step.
Instructions - PART I: Aritmetic
The purpose of this section of the lab is to illustrate problems with integer division.
- Create three variables of type
int
called x, y, and z. Initialize them all to zero. Use cin and cout statements to prompt the user for and accept values for each variable. Use cout to echo these back to the screen to ensure there were no errors up to this point. - Stop now and compile your program. Ensure everything is working as expected. Continue compiling and testing after every step from this point on.
- A
cout
statement can output the result of an operation performed on variables. Output the value ofx + y
by placing it inside acout
statement. - Now add two more
cout
statements. One should output the value of(x + y * z)
, the other((x + y) * z)
. Are these the same? Why or why not? Put your thoughts as a comment in your main(). - Next use
cout
to display the value ofx / y
. Try this at least once with values like 1 and 2, and notice you get zero. This is a common logic error. - Using
cout
, outputx % y
. - Using
cout
, output the decimal value ofx / y
using the appropriate cast. - Using
cout
, calculate and display(x^2 + 2y - 6xz)
to the screen. Remember you will have to convert this to standard C++ syntax.
Feel free to comment out your code above before moving to Part II. If you do, be sure to uncomment before submission next Wednesday.
Instructions - PART II: Car Speeds
In this part of the homework assignment, you are to calculate the acceleration of two cars. Pseudocode (which is an informal high-level description of a computer program) follows:
- Declare time and acceleration variables for two different cars.
- Prompt the user to input the amount of time required for the first car to reach 60mph.
- Prompt the user to input the amount of time required for the second car to reach 60mph.
- Calculate and store the acceleration of both cars.
- Output the acceleration (with units) of each car to the user.
- Output the ratio of first car's acceleration to the second car's acceleration.
Be sure you have Part II working to your satisfaction before moving on to Part III. You are welcome to comment out your solution to Part II, as you are working on Part III. Again, if you do this, be sure to uncomment before submission next Wednesday.
Instructions - PART III: Triangles
Write code to read six real values from the user; these six values represent the (x,y) coordinates of three points. Once the values are entered, compute and print the sides of the triangle that are defined by the three points entered. Then, calculate and print the triangle's perimeter and area. An example run of this program might be:
Enter coordinates for the second point: 2.5 3
Enter coordinates for the third point: 5 0
Sides of the triangle are 3.91; 5.00; 3.91
Perimeter of the triangle is 12.81
Area of the triangle is 7.50
Requirements: you need to use the cmath
library
and I/O manipulators in your solution to this homework assignment.
We will cover output manipulators in class on Monday.
Hints
We won't always provide you with hints, but here are a few to get you going on this part of the homework assignment. You're welcome!
- You need to square some numbers to compute the sides of a triangle from three coordinates; one easy way to square numbers is via the C++ function pow (defined in
cmath
). - You also need to take the square root of some numbers, which can be done using the C++ function sqrt (also defined in
cmath
). - Use Google and the magical Internet thingy to find a formula for calculating the area of a triangle given the length of its sides.
- Use the
iostream
manipulators so that all double values are displayed with two decimal places; see Section 8.3 of our zyBook to learn more about output manipulators.
Submission
You need to submit your solution of this homework (HW02) with three lab assignments: Lab02A, Lab02B, and Lab02C. Detailed generic instructions for submitting homework assignments are available. For homework due this Wednesday, follow these specific steps:
- create a directory called week02.
- within week02, create four subdirectories: Lab02A, Lab02B, Lab02C, and HW02.
- within your new week02/Lab02A directory, copy in your main.cpp file from your Lab02A solution.
- within your new week02/Lab02B directory, copy in your main.cpp file from your Lab02B solution.
- within your new week02/Lab02C directory, copy in your main.cpp file from your Lab02C solution.
- within your new week02/HW02 directory, copy in your main.cpp file from your HW02 solution.
- compress the week02 directory (see Step 3 here for details).
- submit the week02.zip file to Blackboard (see Steps 5-10 here for details).
- after you submit, download the file and double check it contains all that you think it contains!