COMP 1671: Introduction to Computer Science

Lab 2 : Basics

Agenda :

  1. Setup laptops to print to the lab printer. There's a different procedure for those who have 2000 or XP on their laptops, and for those who have 95, 98, or ME on their laptops.
    Instructions for Windows 2000 or XP
    Instructions for Windows 95, 98, or ME
  2. Write a program to guess a number
  3. Write a program to find the area of a rectangle

Part 1 :

Setup laptops to print to the lab printer.

Part 2


We shall write a program to guess a number.  The interaction is intended to be as follows.

> guess
Think of a number.
Add 7 to it.
Give me the result : 10
The number you thought of was 3
In the above, guess is the name of the program and all program output is italicized. What you type is in boldface.

We start out by opening a file in which to write the "source code" or program. Type the following in the Cygwin window.

gvim  guess.cpp
To guess the right number, the program has to do the following steps in addition to those is Part 1.
  1. Ask user to think of a number and add seven to it.
  2. Prompt the user for the result.
  3. Input the result.
  4. Calculate and print original number.
Add these as comments in the guess.cpp file. Then add the actual code to do it. Your file should now look (somewhat) as shown below.
/* Program to guess a number
 * Assignment : Lab 2 Program 1
 * Author : your name
 * Date : mm-dd-yyyy    */

#include <iostream>

using namespace std;

int main()
{

    // Ask user to think of a number and add 7 to it.
    cout  <<  "Think of a number"  <<  endl;
    cout  <<  "Add "  <<  7  <<  " to it."  <<  endl;

    // Prompt the user for the result
    cout  <<  "Give me the result : ";

    // Input the result
    int result;
    cin  >>  result;

    // Calculate and print original number
    cout  <<  "The number you thought of was "  <<  result - 7  <<  endl;

    return 0;
}

Now save the file and exit the editor. Compile the program by typing the following.
g++  guess.cpp  -o  guess
Run the program by typing
guess

Part 3:

Write a program to compute the area of a rectangle. Your program should be named
rectarea. A sample interaction is shown below.
>rectarea
Enter the length : 10
Enter the width  : 5
The area of the rectangle is 50
Show the demo to the TA. Print out your source code and hand it in to the TA.