CS 1440 Lab 2

## Activity 2-1: Lab Goals and Logging In

### The goals of this lab are to:

• learn about C++ data types and expressions
• learn to use cout and cin to output and input data
• learn to use if and if..else statements for conditional execution
• learn about C++ logical expressions, to allow more powerful conditional statements
• put all this to good use in working C++ programs

### Activity Procedure

You will need to be logged on to the cs machine to do this lab, so get logged in now. Now change to your 1440 subdirectory and create a new directory for this lab. Call it "Lab2". After you create the Lab2 subdirectory, change to it. The commands to use to do this entire paragraph are:

cd 1440
mkdir Lab2
cd Lab2

Let's start with a short program which asks the user for three grades, then prints their average. Cut and paste this into a program called ave.C. (We recommend an extension of ".C" for C++ programs.)
```// Read and average 3 integers, print the result.
#include <iostream>
int main(void)
{
int x,y,z;
double average;
// prompt the user:
cout << "Enter three grades separated by spaces, then press <Enter>" << endl;
// read and store three integers:
cin >> x >> y >> z;
average = (x + y + z)/3;
cout << "The average is " << average << endl;
return 0;
}```

We use integer variables (named storage locations) for the grades, since most teachers aren't so precise as to use decimal points in individual grades. However, the average of several integers should be a real number (double in C++), so that's how we declare the variable where we store the result. The lines starting with "//" are comments -- these are ignored by the compiler, but very important to human readers. We'll talk more about comments shortly.