Notes
Outline
COM1100
Fundamentals of Computer Science –Winter 2000
Lecture 3
01/11/99
Announcement -- 1
Please visit course web site periodically.
   www.ccs.neu.edu/course/com1100
   COM1100_Winter_Announcement.html
Lecture notes can be found at :
 www.ccs.neu.edu/course/com1100/LectureNotes
Not all the lecture notes will be posted
   Try to take notes.
Announcement -- 2
Sequence info can be found at :
 www.ccs.neu.edu/course/com1100/Sequence
Talk to the recitation instructor directly if you need to switch the sequence
You can go to sequence 8 for recitation
   but go to sequence 9 for lab
Announcement -- 3
If you are not a computer science major, you can take the course for Pass/Fail
Send me email
Name
ID
Major
If I did not get your email, I will assume you want a graded credit.
If you are a computer science major, you have to take it for graded credit.
Programming Tool
Microsoft Visual C++ 6.0
Service pack 3
It is for Visual Studio (C++) 6.0
Both Windows 95/98 and NT need it
Download Winzip 7.0 evaluation version
WinZip brings the convenience of using Zip files and other archive and compression formats.
http://www.winzip.com/
Reading material this week
Textbook
Chapter 1 : Introduction
Chapter 2 : Fundamental Data Types
Package 1 – Patterns
Name Use Pattern - Part 1 - Variables and Objects
Read-Process-Write Pattern
Reading Data Pattern
Review of last lecture
First C++ program
cout <<
cin >>
Today’s lecture
Identifier
Variable declare and use
Assignment statements
Basic Data Type
Arithmetic operations (?)
First C++ program
1 #include <iostream>
2 using namespace std;
3 int main()
4 {
5   cout << "Hello, World\n";
6
7 return 0;
8 }
Second C++ program
1 #include <iostream>
2 using namespace std;
3 int main()
4 {
5   int num1, num2;
6 cout << “Please enter two numbers: “;
7   cin >> num1 >> num2;
8   cout << “The sum is : “ << num1 + num2;
9 return 0;
10 }
int num1, num2;
Declared two integer variables num1, num2
num1 is the name for an integer variable
num2 is the name for another integer variable
num1 and num2 are called identifiers.
C++ Identifiers -- 1
Identifier give names to variables and functions.
Identifier can be made up of any combination of letters, digits, or underscores(_).
The first character of the name must be a letter or underscores(_).
 Identifier beginning with underscore normally reserved for internal compiler use and for declaring system variable. So it is not recommended.
C++ Identifiers -- 2
Only letters, digits, or underscores may follow the initial letter.
Blank spaces are not allowed
special characters (*, /, %, (), ?, &, !) are not allowed
Use the underscore to separate words in a name consisting of multiple words
C++ Identifiers -- 3
Can not be one of the keywords
 A keyword is a word that is set aside by the C++ language for a special purpose and can only be used in a specified manner.
Some examples:
do if case double return
else int short char long
unsigned sizeof const float
void continue for
switch while
All keywords in C++ are lowercase.
C++ Identifiers -- 4
Can not consist of more than 31 characters
Case-sensitive (upper and lower case letters are different)
test and TEST are two different identifiers for variable.
int main() and int Main() are two different identifiers for function.
C++ program can only be executed from function int main()
Examples of C++ Identifier -- 1
Some valid identifier
Count
Test32
DeptNo
Test_Score
_varX
FEET_PER_MILE
Examples of C++ Identifier -- 2
Some invalid identifier
2ndQtrSale
  // can not start with digit
 hi!there
// ! not valid
 top…ten
//. can not be used
 case
// keyword can not be used
 Dept  No
// Blank space can not be used
Variable
All integers, floating-points, strings and other values used in a program are stored in and retrieved from the memory unit. Conceptually, individual memory locations in the memory unit are arranged like the rooms in a large hotel. Like hotel rooms, each memory location has a unique address (“room number”).
 A variable is simply a name (or an identifier) chosen by the programmer that is used to refer to computer storage location. The term variable is used because the value stored in the variable can change, or vary.
Variable ~ Memory location
For each variable name the program uses, the computer keeps track of the actual memory address corresponding to that name.
In our hotel room analogy, this is equivalent to putting a name on the door of a room and referring to the room by this name, such as BLUE room, rather than using the actual room number.
Variable Declaration
Syntax:  data_type variable_name;
int x;
double y;
string myName;
When you declare a variable,
The system assigned a space in the memory that will be used to store the value of the variable
The memory location can be referred by the name of the variable in the future
The content of the memory location may be garbage – whatever information has been stored there last time
Variable Definition -- 1
Initialize the value of variable x when it is declared
data_type variable_name = initial_value
 Examples
int x = 5;
double y = 7.896;
string myName = “Yuhong Yin”;
Variable Definition -- 2
Change the value of a variable anywhere in the program using assignment statement
Examples
int x;  // variable declaration
x = 9;
x  = 7 + 8;
string herName; // variable declaration
herName = “ Elice Brown”;
Variable Definition -- 3
Read user input from the keyboard or input from a file
The basic C++ statement that read the input stream from the keyboard (the sequence of keystrokes typed in followed by the return key) is following (using cin >>):
int x;
cin >> x;
string myName;
cin >> myName;
Assignment statement
Syntax: variable = expression;
Evaluate the RHS
Put into LHS
Assignment statement tells the computer to assign (store) a value into a variable.
Assignment always have an equal ( = ) sign and one variable name immediately to the left of this sign. The value on the right of the equal sign is determined first and this value is assigned to the variable on the left of the equal sign.
Invalid Assignment
Note :
7 + 8 = x;   // Wrong !
7 + 8 is not an identifier for any variable
LHS of the assignment will not be evaluated
Using a Variable
Once an identifier has been properly declared, initialized and assigned, it can be used in computations, used in output and other operations appropriate for its type.
Variables of int and double type can take part in arithmetic expressions, can be printed, and can be assigned a new value.
Besides reading and writing string, we can manipulate them in a number of ways.
Garbage in, Garbage out
If an identifier has not been initialized before it is used, the compiler may notice the error and notify the programmer, or it may ignore the fact and use the garbage that appears in the memory space assigned to the identifier.
Garbage in, Garbage out
int m=7,n=m, q;
cout << "m = " << m << endl; // m = 7
cout << "n  = " << n  << endl; // n = 7
q = q + n;  // q doesn’t have any value
cout << "q  = " << q  << endl; // q = -858993453
Variable summary
Declare
Initialize
Use
Destroy (Automatically done by the computer)
C++ Basic Data Type
Basic data types are character, integer and floating point
char
int
double
Exact size and capacity of a type varies with the machine and operating system
C++ Data Type -- char
Used to store characters
Characters include the letters of the alphabet (both uppercase and lowercase), the 10 digits 0~9, and the special symbols (+ $ &) enclosed by single quote.
Examples
‘A’ ‘$’ ‘7’ ‘q’
C++ Data Type -- int
Used to store whole numbers (no decimal or fraction), come in three sizes:
short
int
long
Examples
13 567 -9 -13
C++ Data Type – floating point
Used to store numbers requiring fraction or decimal, come in 3 sizes or precisions
float
double
long double
Also called real number
Values stored is often not exact (like 2/3 in decimal, have to stop somewhere 0.666667)
Special symbols, such as the dollar sign and the comma, are not permitted in real number
5,326.25 $10.99
Most often use double instead of float
signed or unsigned
Character and integer types can be declared as either signed or unsigned
unsigned are never considered to hold a negative, and so they can hold a large positive value
signed is the default and not required to specify.
Examples
unsigned char ch;
unsigned int x;
 x = 5; // OK
 x = -5; // Wrong
int x and signed int x are equal
x = 5; // OK
x = -5; // OK
Date types – size & capacity
sizeof operator
       C++ provides a special operator sizeof which gives the size in bytes of any data type
cout << "unsigned char = " << sizeof(unsigned char) << endl;  // unsigned char = 1
cout << "char = " << sizeof(char) << endl;     // char = 1
cout << "short = " << sizeof(short) << endl; // short = 2
cout << "int = " << sizeof(int) << endl; // int = 4
cout << “float = " << sizeof(float) << endl; // float = 4
cout << “double = " << sizeof(double) << endl;
  // double = 8
Today’s lecture
Identifier
Variable declare and use
Assignment statements
Basic Data Type
Arithmetic operations (?)
Thursday’s lecture
Arithmetic operations (?)
Programming style
Safe I /O operations
Visual C++ IDE