CS 1440 Lab 2
Cover sheetPrevious ActivityNext Activity

Activity 2-1: Lab Goals and Logging In

The goals of this lab are to:

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.
int main(void) 
        int x,y,z;
        double average;
        // prompt the user:
        cout << "Enter three grades separated by spaces, then press :" << 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.