EEE146 Ch1 Introduction
EEE146 Ch1 Introduction
PROGRAMMING –I
Course Information
Grading: Midterm 1 (%22.5) & Midterm 2 (%22.5) + Laboratory (%15) + Final (%40)
Syllabus
Input /Output
Output
Output
Input
Input
A Computer System
Everything we had in the previous slide is hardware.
i.e., physical components that implement what is requested by
the software.
APPLICATIONS
(Eg: Word, Excel, Explorer, MSN, C Compiler,
SOFTWARE
OPERATING SYSTEM
(Windows, Linux, MacOS, etc.)
HARDWARE
A Computer System
Program Registers
R1
instr 1
R2
instr 2 .
. Arithmetic &
instr 3 .
Logic Unit
... Rm
instr n
IR
...
Control Unit
How do we write programs ?
This is the
executable code in
We write our "machine language."
programs in We use a compiler
"C++ language" (such as Visual C++, This is the only thing
(which is an English- Dev C++, etc.) to the computer can
like language) translate our program understand and run
from "C++ language" (execute).
to "machine
language"
#include <iostream>
using namespace std; 1110101011001001010
int main() 0010101001010000100
{ 1010010101010100010
cout<<"Hello world!"; Compile & Link 1001000100101001
return 0;
}
(machine code
(source code) or
(object code) executable code)
Statement vs. Instruction
Control Unit
Our first C++ program: Hello World
int main()
{
int a, b, c;
STOP
Algorithm and flowchart
START
example 2
Sum of numbers 1 through 10.
i=1
S1: Start sum=0
Print sum
STOP
Algorithm and flowchart START
example 3
input N
Mean of N numbers
S1: Start s=0 , i=0
S2: Input N
input x
S3: Set s=0, i=0
S4: Input x S=s+x ,i=i+i
STOP
Algorithms
So, make sure that the variable has a valid value before you
perform any operation based on that value.
Variables
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0 0 1 0 0 1 1 0 1 0 1 1 0 1 1 1 213+210+29+27+25+24+22+21+20=9911
The size may change due to the factors listed above, and your
program will not work.
Variables
#include <iostream>
using namespace std; Program
int main() a ...
5
10
{ b ...
3
c ...
7
int a, b, c;
a=10;
b=3;
c=a-b;
a=b+2;
}
Rules for identifier names
While defining names for variables (and also functions, user-defined
types, and constants in the future) you should obey the following
rules:
The first character of a name must be a letter or underscore (‘_’).
The remaining characters must be letters, digits, or underscore.
Only the first 31 characters are significant.
Avoid reserved words such as int, float, char, etc. as identifier
names.
However, it is better to avoid starting identifier names with
underscore.
Also remember that C++ language is case-sensitive.
It is a very good practice to use meaningful names.
Rules for identifier names
Valid:
a, a1, count, no_of_students, B56, b_56
Invalid:
1a, sayı, int, $100
You have to specify the type of a variable when you define it.
There are three standard data types:
Integer (i.e., whole numbers)
Float (i.e., real or floating-point numbers)
Characters
We will discuss user-defined types later in the course.
Integers
Syntax:
int variable_list;
where variable_list is a comma-separated list of variable names.
Each variable name may be followed by an optional assignment
operator and a value for initialization.
The size may change, but the leftmost bit is used for the sign. The
remaining bits represent the value in binary.
Though the size of an int variable may vary, it is always limited, i.e.,
it contains a limited number of bits. Therefore, the maximum and
minimum values that can be represented by an int variable is limited.
Integers
0 0 1 0 0 1 1 0 1 0 1 1 0 1 1 1
Leftmost bit is used for the sign, so 15 bits are left for the value.
So, you have 215=32,768 positive values, ranging from 0 to 32,767.
Similarly, you have 32,768 negative values, this time ranging from -
1 to -32,768.
Syntax:
float variable_list;
Float type is used for real numbers.
Note that all integers may be represented as floating-point
numbers, but not vice versa.
Floating-point numbers
Syntax:
char variable_list;
Character is the only type that has a fixed size in all
implementations: 1 byte.
All letters (uppercase and lowercase, separately), digits, and
symbols (such as +,-,!,?,$,£,^,#, comma itself, and many others)
are of type character.
Characters
Since every value is represented with bits (0s and 1s), we need a
mapping for all these letters, digits, and symbols.
This mapping is provided by a table of characters and their
corresponding integer values.
The most widely used table for this purpose is the ASCII table.
Characters
The ASCII table contains the values for 256 values (of which only
the first 128 are relevant for you). Each row of the table contains
one character. The row number is called the ASCII code of the
corresponding character.
(The topic of character encoding is beyond the scope of this
course. So, we will work with the simplified definition here.)
ASCII table (partial)
ASCII code Symbol ASCII code Symbol ASCII code Symbol ASCII code Symbol
... ... 66 B 84 T 107 k
32 blank 67 C 85 U 108 l
37 % 68 D 86 V 109 m
42 * 69 E 87 W 110 n
43 + 70 F 88 X 111 o
... ... 71 G 89 Y 112 p
48 0 72 H 90 Z 113 q
49 1 73 I ... ... 114 r
50 2 74 J 97 a 115 s
51 3 75 K 98 b 116 t
52 4 76 L 99 c 117 u
53 5 77 M 100 d 118 v
54 6 78 N 101 e 119 w
55 7 79 O 102 f 120 x
56 8 80 P 103 g 121 y
57 9 81 Q 104 h 122 z
... ... 82 R 105 i ... ...
65 A 83 S 106 j
Characters
int main(){
int year, age;
char myName;
cout<<"Enter the year you were born and your initial \n";
cin>> year >> myName;
cout<<"Your initial is: " <<myName ;
age = CURRENTYEAR - year;
cout<<"Your age is: "<<age;
return 0;
}
Enumerated type
We will cover the most basic operators in class. More operators will be
covered in the labs.
Assignment operator (=)
Note that this is not the "equals" operator. It should be pronounced
as "becomes." (Equals is another operator.)
The value of the expression on the RHS is assigned (copied) to the
LHS.
It has right-to-left associativity.
a=b=c=10;
makes all three variables 10.
Assignment and type conversion
a b a && b a || b
true true true true
true false false true
false true false true
false false false false
• The order of evaluation is from left to right
• As usual parenthesis overrides default order
Operators
Instead of writing a=a+b, you can write a+=b in short. Similar with -
=, *=, /=, and others.
Operators
"==" is the "is equal to" operator. Like all other comparison
operators, it evaluates to a Boolean value of true or false, no
matter what the operand types are.
== x == y is x equal to y?
!= x != y is x not equal to y?
! ! exp NOT
Operators
Don’t hesitate to use parentheses when you are not sure about
the precedence (or to make things explicit).
Operator precedence table
Operator Associativity
() [] . -> left-to-right
++ -- + - ! ~ (type) * & sizeof right-to-left
* / % left-to-right
+ - left-to-right
<< >> left-to-right
< <= > >= left-to-right
== != left-to-right
& left-to-right
^ left-to-right
| left-to-right
&& left-to-right
|| left-to-right
?: right-to-left
= += -= *= /= %= &= ^= |= <<= >>= right-to-left
, left-to-right
Operators
1 * 2 + 3 * 5 % 4 1 + 8 % 3 * 2 - 9
\_/ \_/
| |
2 + 3 * 5 % 4 1 + 2 * 2 - 9
\___/
\_/ |
| 1 + 4 - 9
2 + 15 % 4
\______/
\___/ |
| 5 - 9
2 + 3 \_________/
\________/ |
| -4
5
Mixing types