# Types and Expressions I: Computing Slopes

## The Problem

Write a program that reads values x1,y1,x2,y2 and prints out the slope of the line passing through (x1,y1) and (x2,y2). The slope is given by:
```    slope = (y1 - y2) / (x1 - x2)
```
For the purists among you, you may assume that the line is not vertical.

## A Solution

 We already know the basic structure here, just our usual "main" skeleton. ```int main() { return 0; }``` Let's add in comments outlining the basic process for solving this problem. ```int main() { // Read first point // Read second point // Compute the slope // Print out slope return 0; } ``` Reading in a point means reading in a value for x1 and y1. First let's read in x1. First we declare variable for the value, then we ask the user for the value, then we read the value! ``` // Read first point double x1; cout << "Enter x1: "; cin >> x1; double y1; cout << "Enter y1: "; cin >> y1; ``` This is easily extended for the other values. ```int main() { // Read first point double x1; cout << "Enter x1: "; cin >> x1; double y1; cout << "Enter y1: "; cin >> y1; // Read second point double x2; cout << "Enter x2: "; cin >> x2; double y2; cout << "Enter y2: "; cin >> y2; // Compute the slope // Print out slope return 0; } ``` To compute the slope, we first declare a variable to contain the slope, and then we compute its value. ``` // Compute the slope double slope; slope = (y1 - y2) / (x1 - x2); ``` All we have to do now is print out the answer, which is old hat. ``` // Print out slope cout << "The slope is: "; cout << slope << endl; ```

And now we have a complete solution!

Christopher W Brown