Title: CS1005 Objectives
1// A simple C program include
ltiostreamgt using namespace std int main ()
cout ltlt Welcome to CS1005! \n return
0
2General comments
- Use whitespace to make it more readable
- Common programming errors
- Dont reinvent the wheel
- Maximum warnings on compiler
3What not to do with Whitespace
include ltiostreamgt using namespace stdint
main()coutltlt"Welcome to CS1005!\n"return 0
4// This program adds two numbers and displays the
result // on the screen include
ltiostreamgt using namespace std int main ()
int result result 2 3
cout ltlt The sum of 2 3 is ltlt result
ltlt endl return 0
5Data Type
- Determines how much memory is needed to store
this information - Determines what sort of information the data type
represents - Determines which operations are legal on this
information
6Rules for Identifiers
- Must start with a letter
- May only contain letters, digits and underscore
character - Must not be a keyword
- C is case sensitive.
- C allows any length
- Our standard - start with small letter,
capitalize first letter of each word (avgTemp) - Should be meaningful. Reflect its purpose
(avgGrade, worstGrade, bestGrade)
7Arithmetic Operators
C Operation Arithmetic Operator Algebraic Expression C Expression
Addition a b a b
Subtraction - a - b a - b
Multiplication ab or a x b a b
Division / a/b or a or a b a/b
Modulus a mod b a b
8Rules of precedence for Arithmetic Operations
Operator Operation Order of evaluation
( ) Parentheses Evaluated first. If the parentheses are nested the innermost pair is evaluated first. If there are several pairs on the same level they are evaluated left to right.
/ Division, mulitiplication, modulus Evaluated second. If there are several they are evaluated left to right.
- Addtition, subtraction Evaluated last. If there are several, they are evaluated left to right
9Exercise 1 int num1, num2, num3 num1 3
num2 num1 num1 num2 1 num3 num2 num1
- 6 What are the values for num1, num2 and num3
after this program executes?
Exercise 2 int a, x, b, c, y a 2 x 5 b
3 c 7 y a x x b x c What is y
after this program executes?
10Relational Operators
Standard algebraic expression C operator Example of C expression Meaning
gt gt a gt b a is greater than b
lt lt a lt b a is less than b
gt a gtb a is greater than or equal to b
lt a lt b a is less than or equal to b
a b a is equal to b
! a ! b a is not equal to b
11Logical Operators
Operator Name Example True if
! Not !x x is false
And x y x and y are both true
Or x y x or y is true
12Precedence chart
Operator Type
() parentheses
! not
/ multiplication, division, modulus
- addition, subtraction
lt gt lt gt relationals
! equality
and
or
13Data Type
- Determines how much memory is needed to store
this information - Determines what sort of information the data type
represents - Determines which operations are legal on this
information
14Determining Size and Range
// Determining the size and range of the the
signed long integer type. include ltiostreamgt
using namespace std int main() cout ltlt
"signed long type is " ltlt sizeof (signed long) ltlt
"bytes\n" cout ltlt "largest value of signed
long type is " ltlt LONG_MAX ltlt endl cout ltlt
"smallest value of signed long type is " ltlt
LONG_MIN ltlt endl return 0
15Floating Point Data Types
Type Typical range Typical precision
float 10 e -38 to 10 e 38 6 digits
double 10 e -308 to 10 e 308 15 digits
long double 10 e -4932 to 10 e 4932 19 digits
16Things to remember about floating point data types
- Mantissa is limited in size so value may not be
exact - Arithmetic is faster using integer types than
floating point types - Dont use equality operators
- Modulus operator not valid for floating point
- Dont use floating point s for loop control
17Mixing float and integer types
include ltiostreamgt using namespace std int
main() float average int
numberOfGrades 2, totalOfGrades 9
average totalOfGrades / numberOfGrades
cout ltlt "The average is " ltlt average ltlt endl
return 0
18Data Types Summary
Given the following declarations, determine the
value of the variable on the left-hand-side of
each assignment statement. int int1, int2,
int3 float f11.0, f22.5, f35.0 double d1,
d2 char ch1, ch2 int1 5 d2 5.0 ch1
'5' int3 f2 int2 int1 / int3 d1 f3 -
-f1 6 / int3 8.0 (int1 - d2) ch2 ch1 -
2 int3 'a' - 'A' ch1 'W' int3