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.

  1. 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.
  2. Stop now and compile your program. Ensure everything is working as expected. Continue compiling and testing after every step from this point on.
  3. A cout statement can output the result of an operation performed on variables. Output the value of x + y by placing it inside a cout statement.
  4. 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().
  5. Next use cout to display the value of x / y. Try this at least once with values like 1 and 2, and notice you get zero. This is a common logic error.
  6. Using cout, output x % y.
  7. Using cout, output the decimal value of x / y using the appropriate cast.
  8. 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:

  1. Declare time and acceleration variables for two different cars.
  2. Prompt the user to input the amount of time required for the first car to reach 60mph.
  3. Prompt the user to input the amount of time required for the second car to reach 60mph.
  4. Calculate and store the acceleration of both cars.
  5. Output the acceleration (with units) of each car to the user.
  6. Output the ratio of first car's acceleration to the second car's acceleration.
Hint: acceleration = (finalVelocity - initialVelocity) / time.

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 first point: 0 0
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!

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: