Title: Exam 1 Review
1Review forExam 1
2Computer hardware
3Data Representation
- Computer uses a binary (base 2) system with two
unique digits 0 and 1, called bits (short for
binary digits) - byte - 8 bits grouped together as a unit
- ASCII Provides different combinations of 0s and
1s to represent 256 (28) individual characters
- Numbers
- Uppercase and lowercase letters
- Punctuation marks
4Editor and C compilers
- A compiler is a translation program
- convert a program written in a higher-level
language (source code) into machine language
(machine code) - Source code is usually in text format that can be
edited using a text editor
C source code (.cpp)
Compiler (g)
Machine code (executable)
5A First Program (convert feet to inches)
// Program height.cpp // Convert feet to
inches // Author(s) Im A. Programmer // Date
09/01/2004 include ltiostreamgt using namespace
std int main () int Feet, Inches
Feet 6 Inches Feet 12 cout ltlt
Height is " ltlt Inches ltlt "in. ltlt endl
return 0 // end main
For peoples benefit
Commentmarker
input output librarydefinition file
Compiler directive
main ( ) Function says where program starts
running
Variable declarations
Every statementends with a semicolon.
6The Basics
- Naming Rules
- Keywords
- Identifiers Variables
- Letters (upper and/or lower case) a, b, x, y,
- Digits 1, 2, 0,
- A name cannot start with a digit
- Underscores _
- A name should not start with an underscore
- Variable Declarations
- Simple int I int J, K
- Initialized float Length 0.0
- Constant const int NBR 256
- Console I/O
- Input cin gtgt I gtgt J gtgt K
- Output cout ltlt Your score is " ltlt score ltlt endl
7Data Types
- Numeric Data Types
- Integer int, short, long
- 45 -69745 22L
- Floating-point float, double, long double
- 0.0F -58.0 3.14159L 22.6E45 44.8E-9
- Character Data Type
- char
- 'a' ''
- '\n' '\''
- Compatible with numeric types (ASCII encoding)
- Boolean Data Type
- bool
- true, false
- Compatible with numeric types
8Numeric Operators
- Arithmetic
- , -, , /,
- Assignment
- Operators
-
- - /
- Process
- Calculate value of expression to right of
assignment operator - Store or include value into variable to left of
operator, with type conversion if necessary - Swapping two variables
- Temp Var1Var1 Var2 Var2 Temp
9More Numeric Operators
- Assignment Conversions
- From int to double extend with .0
- From double to int chop off fraction
- Integer Division
- Integer division produces integer result
- (Number (5 / 9)) is the same as (Number 0)
- Modulus Operator
- Gives the integer remainder of dividing the two
integer operands - 5 2 5 / 2 2 R 1 so (5 2) yields 1
- 12 4 12 / 4 3 R 0 so (12 4) yields 0
10Logical Expressions
- Relational Operators
- !
- lt lt gt gt
- Logical Operators
- !
- Precedence
- Highest parentheses
- Next arithmetic
- Next relational
- Next logical ( is higher than )
- Score1 80 Score2 75 Age 35
- if (Score1 gt 90 Score2 gt 85 Age gt 30)
- Lowest assignment
I gt 0 I lt 0
I gt 0 I lt 10) ! (I lt 0 I gt 10)
NO short-circuit here
11Basic if Groups
- One-Way
- if (Value lt 0)
-
- Value -Value
-
- Two-Way
- if (B ! 0)
-
- C A / B
- else
-
- C 0
-
false
if
true
true
false
if
12Multiple Selections if else if else
- if ((Grade 'A')
- (Grade 'B')
- (Grade 'C'))
-
- cout ltlt "Pass." ltlt endl
-
- else if (Grade 'D')
-
- cout ltlt Maybe." ltlt endl
-
- else if (Grade 'F')
-
- cout ltlt "Fail." ltlt endl
-
- else
-
- cout ltlt "Error!" ltlt endl
-
false
if
true
false
else if
true
false
else
true
13Multiple Selections
- switch
- switch (Grade)
-
- case 'A' case 'B' case 'C'
- cout ltlt "Pass." ltlt endl
- break
- case 'D'
- cout ltlt Maybe." ltlt endl break
- case 'F'
- cout ltlt Fail." ltlt endl break
- default
- cout ltlt "Error!" ltlt endl
- if else-if else
- if ((Grade 'A')
- (Grade 'B')
- (Grade 'C'))
-
- cout ltlt "Pass." ltlt endl
-
- else if (Grade 'D')
-
- cout ltlt Maybe." ltlt endl
-
- else if (Grade 'F')
-
- cout ltlt "Fail." ltlt endl
-
- else
-
- cout ltlt "Error!" ltlt endl
14Coding Style
- Comments
- At top of program, to document name, purpose,
author, date written / modified, etc. - Before blocks of code, to say what they do
- On declarations, to tell purpose of variable
being declared - Indentation
- All code within a block or control construct is
indented - If blocks are nested, then so is the indentation
- Braces
- use them around the body of main function
- use them around the body of a control construct
15Linux commands, g compiler, etc
- ls list files in working directory or other
directories - mkdir create a directory
- cd change working directory
- cp copy a file to another file
- mv rename a file or move it to another directory
- rm delete a file or files
- rmdir remove empty directory
- To compile g myfile.cpp
- To run ./a.out