WarmUp Lab

September 1997 - Fell

Overview

This laboratory is intended as a "warm up" to the study of computer science using the C++ language as the underlying programming language. In this lab, you will learn about certain constructs which are fundamental to programming such as input and output, strings of printable characters, numbers and arithmetic, and simple graphics.

C and C++ Program Structure
In C and C++, there is one file which contains the main program . It is WarmUp.cp for this Lab.

The necessary C and C++ library routines for this course are accessed by placing the following

#include
line near the top of a program file:
	#include "CHeaders.h"			// access to standard libraries

Similarly, to access our software tools for windows, graphics, and robust input and output, the following three lines must be placed near the top of the file as well:
	#include "SWindows.h"			// set up for windows

	#include "Graphics.h"			// graphics functions

	#include "IoTools.h"			// tools for safe I/O


All text on a line that follows a pair of slashes // is considered a comment for human readers and is ignored in the compilation process.

The main Program

Attached to this laboratory description, is a copy of the version of WarmUp.cp that you will find on the network. Look at the function in WarmUp.cp called main. The framework of main without its contents or comments is:
void main() {

}

The pair of parentheses following the name main says that main is a C++ function. The keyword void says the function main is not expected to return a value.

Most of the work done in main is accomplished by calling other functions. The first call is

BigSquarePair();
This executes a function in SWindows that opens a 400x400 square Drawing window for graphics on the left and a 240x400 Text window for user interaction on the right.

The next three calls transfer control to the three tasks that are at the heart of the lab:


Credits();		// identify yourself as the programmer
NumbersTask();		//reading, writing, and arithmetic
SimpleGraphicsTask();	//simple graphics
The work of the program is divided into separate functions for each task to avoid excessive complication in the main function. The code written for each task can then be written without reference to or interference from the code in the other tasks.

The main function ends with a call to

PressReturn
to cause the program to pause until the user presses the return key. This is necessary to hold the Drawing window open until the program terminates.

The Tasks of this Laboratory
To help you to find the places in the code where you will have to add or modify code, we have placed special comments which start with:

Credits - Output of String Constants
Before we discuss Credits, notice that the very last line of main is the statement:

cout << "Select Quit from the File Menu" << endl;

In Credits, use the same technique to create three lines of output with your name, your student ID number, and a blank line for extra space. The output should look like:
Written by Bill Gates
ID: 123-45-6789

Of course, use your own name and student ID number not those of Bill Gates. In the function Credits, you will see the following comments to remind you of the work to be done at that stage:
	//  ? ? 	(1) add a message with your own name

	//  ? ? 	(2) add a message with your student-id number

	//  ? ?  	(3) add an extra blank line

You should save your file and run the program as soon as you have finished the Credits function. It this way, you can check for and correct errors before moving onto the next function. Do the same with the other tasks as well.

Henceforth, in all laboratory assignments, you should create a Credits function that is called at the beginning of main to identify you as the programmer.

NumbersTask - Integer Input and Arithmetic Output
In NumbersTask, you will read the values of two integer ( int) variables x and y typed in by the user and you will then compute and display the five arithmetic operations:

x+y  x-y  x*y  x/y  x%y.
A typical user session should look like:

	Test arithmetic operators
	Enter x:  31
	Enter y:  5
	x = 31
	y = 5
	x + y = 36
	x - y = 26
	x * y = 155
	x / y = 6
	x % y = 1
	
	Press Return To Continue 
The user input is shown in italics. You enter values of x and y and then the program computes and displays the five possible arithmetic operations involving x and y.

The first steps in NumbersTask are the variable definitions that request storage for two int variables:


	int x;				// used for arithmetic
	int y;				// used for arithmetic

The value of x is requested in a prompt-response interaction. Your first task is to request the value of y in the same manner. The program then echoes the x and y values to the user.

You must next do the arithmetic computations. A sample is given in the file as a comment:


	// cout << "x + y = " << x + y << endl;
By removing the comment slashes, you have the first line of the test code. You must make four similar lines of the code for the operators - * / %.

By the way, the reason why we need to comment the line with the operation x + y is that without the code to read the value of y (that you will supply) the compiler detects the fact that y is an uninitialized variable and issues a warning. If you want to see this, remove the comments before you enter the code to read y.

Run your program several times with different values for x and y and look at the results.

SimpleGraphicsTask
In SimpleGraphicsTask, a red rectangle, blue oval, and white line are already drawn. Your task is to read the code and then write the code to draw the shapes requested: another white line, a green rectangle, and a magenta oval.

Turn in: A diskette with your project (warmup.mu) and your source code (warmup.cp), and a printout of your source code.

This work is due at your next class.


Last Updated: September 28, 1997 9:41 pm by
Harriet Fell
College of Computer Science, Northeastern University
360 Huntington Avenue #161CN,
Boston, MA 02115
Internet: fell@ccs.neu.edu
Phone: (617) 373-2198 / Fax: (617) 373-5121
The URL for this document is: http://www.ccs.neu.edu/home/fell/COM1100/PROG/WarmUpNewLab.html