LAB 1.0 SUBMIT YOUR EMAIL ADDRESS
Use your most often used email
account (don¢t use Medgar Evers College¢s account) to send an empty email to the instructor at dzhu@sci.brooklyn.cuny.edu with your full name, course #, section # and your major on the subject
line (see follows).
Subject: John Smith / CS 151-060 or 001 /
Computer Science
LAB
1.1 OPENING, COMPILING AND RUNNING YOUR FIRST PROGRAM
Exercise 1: Logon to your
system based on your professor¢s instructions. Start up Visual Studio IDE.
Exercise 2: Copy and paste firstprog.cpp
program shown below into Visual Studio IDE.
Exercise 3: Compile the
program.
Exercise 4: Run the program.
Exercise 5: Print a copy of both the program and the screen output,
and hand them in.
The
code of firstprog.cpp is as follows:
//
This is the first program that just writes out a
simple message
#include <iostream> // needed to perform C++ I/O
using namespace std;
int main ()
{
cout << "Now is the time for all good
men" << endl;
cout << "To come to the aid of their
party" << endl;
return
0;
}
LAB 1.2
COMPILING A PROGRAM WITH A SYNTAX ERROR
Exercise 1: Copy and paste the program semiprob.cpp shown below.
Exercise 2: Compile the
program. Here we have our first example of the many syntax errors that you no
doubt will encounter in this course. The error message you receive may be
different depending on the system you are using, but the compiler insists that
a semicolon is missing somewhere. Unfortunately, where the message indicates
that the problem exists, and where the problem actually occurs may be two
different places. To correct the problem place a semicolon after the following line
in the program:
cout << "Today is
a great day for Lab"
Most syntax
errors are not as easy to spot and correct as this one.
Exercise 3: Re-compile the
program and when you have no syntax errors, run the program and input 9 when
asked.
Exercise 4: Print out the program and the running result,
and hand them in.
Exercise 5: Try running it
with different numbers. Do you feel you are getting valid output?
The code of semiprob.cpp is as follows:
// This program demonstrates a compile error.
#include
<iostream>
using namespace std;
int main()
{
int
number;
float total;
cout <<
"Today is a great day for Lab"
cout << endl << "Let's start off by typing a number of
your choice"
<< endl;
cin >>
number;
total = number *
2;
cout <<
total << " is twice the number you typed" << endl;
return 0;
}
LAB
1.3 RUNNING A PROGRAM WITH A RUN TIME ERROR
Exercise 1: Copy and paste
program runprob.cpp listed below into Visual
Studio IDE.
Exercise 2: Compile the
program. You should get no syntax errors.
Exercise
3: Run
the program. You should now see the first of several run time errors. There was
no syntax or grammatical error in the program; however, just like commanding
someone to break a law of nature, the program is asking the computer to break a
law of math by dividing by zero. It cannot be done. On some installations, you
may see this as output that looks very strange. Correct this program by having
the code divide by 2 instead of 0. Change the line that reads
"divider
= 0;
" to
"divider
= 2; "
Exercise 4: Re-compile and
run the program. Type 9 when asked for input.
Exercise 5: Print out the program and the screen output, and
hand them in.
Exercise 6: Run the program
using different values. Record the output. Do you feel that you are getting
valid output? The code of runprob.cpp is as follows:
// This program will take a number and divide it by 2.
#include
<iostream>
using namespace std;
int main()
{
float number;
int divider;
divider = 0;
cout
<< "Hi there" << endl;
cout
<< "Please input a number and then hit return" << endl;
cin
>> number;
number =
number / divider;
cout
<< "Half of your number is " << number << endl;
return 0;
}