Rev A Lecture 1
a) Word Symbols or reserved words
e.g. int, double, return
Always lower case and cannot be used for anything else
Subset of these reserved words are descriptions of values stored and displayed, called data types. They also define the amount of memory allocated because of their size.
1- Integral or whole numbers are characterized by a couple of types. The main ones we use are as follows:
The int type is mainly used to define whole numbers
The char type is mainly used to define the character set used in the compiler. Visual C++ uses the ascii character set. Therefore to represent an a you use ‘a’ which is stored as 97 in decimal or as 1100001 in binary bits. The first 32 ascii characters are non printable and are represented by an escape sequence such as new line ‘\n’ (replaces endl). Since the characters are equal to integral numbers there is a collating sequence to them. ‘A’ (65) is less than ‘a’ (97).
The bool type is use in logic for true and false represented in memory by a value of 1 or 0.
2- Decimal numbers are also represented by a number of types depending upon the size. C++ will have the scientific notation of taking a decimal number to the power of 10.
e.g. 851.67 is equal to 8.5167E2 or e2.
The double type is mainly used and is a default.
The float is sometimes used and is normally smaller
3- String types are used when more than one character is required to produce words and are symbolized by being enclosed in quotation marks.
e.g. “This is considered a great lecture.”
Certain strings when used with special escape sequences have special meanings. Malik book, page 76, table 2.4.
e.g. cout<<This is a good book \n”; The \n operates like endl and is considered a new line character.
The back slash character is also used in a string for some reserved characters so they can be printed out.
e.g. cout<<The \’ will be printed out as a single quotation mark.
4- Converting a data type can be done through C++.
The form static_cast is used. This is called explicit casting.
e.g. static_cast<int>(4.8) is 4
e.g. static_cast<int>(’a’) is 97
b) Special Symbols
e.g. + - ; <= !=
Unary operator or binary
Arithmetic order of precedence
1- *, /, %
2- +, -
3- Same level then left to right
4- Parenthesis in operations () clarifies order of precedence
Operations result in the data type used. When there is mixed expressions then everything is reverted to decimal. This is called implicit casting.
5- Exercise 1
A)
Order of Operations
a)
16 + 3 - 2
b)
20 + 4/2
c)
(20 + 4)/2
d)
5 + 6 * 3/2 -1
B)
Trace:
//Second
C++ program
//This
is the source code saved as a .cpp file
#include<iostream>
using namespace std;
int main()
{
cout
<< 7 + 3;
cout
<< 8/2*4;
cout
<< 6 + 18 - 14 / 2 + 87;
cout
<< 4.2 + 6.6/3 - 1 << endl;
cout << -45 % 2 + 3 << endl;
cout
<< (67 % 6) + (9 % 10);
return 0;
}
C)
Evaluate Mixed Expressions
a)
4.5 -
3.2/2 + 1
b)
7.8 +
15/2 - 1.2
c) Identifiers
Variables, constants or functions
Uses letters, digits or underscore and can only begin with letters or underscore
Uppercase identifier is different than lower case.
Memory locations are given names so they can be accessed from various parts of the program to obtain the values placed there. They are also very importantly defined as to data type. This tells how much memory needs to be set aside for storage.
1- A named constant is a memory location whose content is not allowed to change during program execution.
Form is const data type name
e.g. const double SalesTax=0.06;
Placed at head of program right before main() function
2- A variable is a memory location whose value can change during program execution. You must declare them before they are used!
Can add data into variables in two ways.
A) Assign as data type variable = value of expression;
or data type variable;
variable = value of expression;
e.g. double balance = 451.35;
B) Read in as
Data type variable1,variable2,variable3,……
Then cin>>variable1>>variable2>>variable3>>……
The cin causes the computer to get the input from the standard input device which is the keyboard to the computer.
C) Strings
To use strings as variables you need to add the preprocessor header file at the beginning of the program as #include <string>.
Data is assigned to variables with quotation marks.
e.g. name = “John Doe”
Data is read in to variable without quotation marks.
e.g. string firstName;
string lastName;
cout<<”Enter your first name”;
cin >>firstName;
On the prompt for first name John should be entered
d) Increment and decrement operators
e.g. count = number + 1;
Therefore whatever number is on the right side the variable count is assigned a value of number plus one and the variable number remains the same. Since increment and decrement are used often a short cuts are used.
Pre-increment ++variable
Post-increment variable++
Pre-decrement --variable
Post-decrement variable –
e) Compound assignment statements
Thus variable = variable / expression
Can be written as variable /= expression
Same is true for other arithmetic operators
Exercise 2
A)
int main()
{
int x,y;
int sum;
int product;
char char1,char2;
x = 15;
y = 23;
sum = x + y;
cout <<
" x is = " << x <<
endl;
cout <<
" y is = " << y << endl;
cout <<
" sum is = " << sum << endl;
cout <<
"product is = " << x * y << endl;
char1 = 'a';
char2 = 'b';
sum = char1 + char2;
cout <<
"sum is = " << sum << endl;
return 0;
}
B)
Write a program that will convert Celsius degrees to
Fahrenheit. Complete the missing
statements:
//This program inputs a
Celsius temperature
//and outputs both the
Celsius temperature and its equivalent
//Fahrenheit
temperature.
#include<iostream>
using namespace std;
int main()
{
double celsius;
double faren;
cin >> celsius;
//compute the equivalent fahrenheit
cout <<
"Celsius: " << celsius <<
" degrees" << endl;
// print out Fahrenheit now
return 0;
}
C)
Evaluate
If x = 7, y = 5 and z = 15 before each of the following statement is executed,
what are the values after execution?
D) x += 60 -z;
E) y++;
F) z += y - 2;
z += z - 2;
D)
#include <iostream>
#include <string>
using namespace std;
/********************************************/
// program from chapter 2 of the Malik Book
// Date:
// Programmer:
/********************************************/
int main ( )
{
int firstNum, secondNum;
double z;
char ch;
string name;
firstNum = 4; //line 1
secondNum = 2 * firstNum + 6; //line 2
z = (firstNum + 1) /2.0; //line 3
ch = 'A'; //line 4
cin >> secondNum; //line 5
cin >> z; //line 6
firstNum = 2 * secondNum + static_cast <int> (z); //line 7
cin >> name; //line 8
secondNum = secondNum + 1; //line 9
cin >> ch; //line 10
firstNum = firstNum + static_cast<int>(ch); //line 11
z = firstNum - z; //line 12 return 0;
}
Data inputs are 3 11.4 Don r
Give values being stored in the following variables after execution of the following
Line number firstNum secondNum z ch name
Before line 1
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line 11
Line 12
E)Give values for i, j, and ans
#include <iostream>
using namespace std;
/*****************************************/
//Illustrating assignment and operations
//Date:
//Programmer:
/*****************************************/
int main ( )
{
int i = 2, j = 99;
int ans ;
i = i + 1;
j = j + 1;
// increment and decrement examples
i ++; // i=
j ++; // j=
i = 5;
j = 9;
i = i - 1;// i=
j = j - 1;// j=
i --; // i=
j --; // j=
// more increment and decrement examples
ans = i + j; // i= j= ans=
ans = i++ + j; // i= j= ans=
ans = ++i + j; // i= j= ans=
ans = i-- + j; // i= j= ans=
ans = --i + j; // i= j= ans=
ans += 2; // i= j= ans=
ans *= 2; // i= j= ans=
ans /= 2; // i= j= ans=
ans += j + 2 * i; // i= j= ans=
return 0;
}
F).
Write a C++ program that
1) prompts the user to input 3 decimal
numbers (with a fraction part)
2) Prints the three decimal numbers.
3) Converts each decimal number to the nearest
integer.
4) Prints the three converted numbers.
5) Computes and prints the sum and
average of the three integers.