Introduction To Programming 1
Introduction To Programming 1
3
Character Set
• Set of characters used in the language
• Alphabets
• A to Z and a to z
• Digits 0 to 9
• Special Ch , : . “ ! + - * / ? # $ etc
4
ASCII Table
Back
Escape Sequences (partial list)
Escape
Represents
Sequence
\a Bell (alert)
\b Backspace
\f Formfeed
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\' Single quotation mark
\“ Double quotation mark
\\ Backslash
\? Literal question mark
6
Types of Data (simplified view)
• The basic kinds of data that we will mostly use:
– Numeric
• Integers:
• Real (floating point) numbers:
– Character (are enclosed by single quotes in C)
• All letters, numbers, and special symbols
– Ex. ‘A’, ‘+’, ‘5’
– String (are enclosed by double quotes in C)
• Are combinations of more than one character
– Ex. “programming”, “ME 30”
– Logical (also called ‘Boolean’ named after George Boole an English mathematician
from the early 1800’s)
• True or False (1 or 0)
7
Write the Type of Data for each value
I-Integer, F-floating point, S-String, C-character
Data Type
1. 17
2. “George”
3. 2.35
4. 0.0023
5. -25
6. ‘m’
7. 4.32E-6
8. “185.3”
9. 0
10. 1
8
Figure 2-6Standard data types
Constants and Variables
• Constant
– A data element that never changes in your program
• 5, 62.37, 4.219E-6, “record_string”, ‘$’
• i = j + 7; /* which one is the constant? */
• first_letter = ‘a’; /* which one is the constant? */
• Variable
– A data element that can take on different values
• Its name represents a location (address) in memory
– i = j + 7; /* which are variables? */
– second_letter = ‘b’;/* which is the variable? */
• Values are ‘assigned’ using a single equal sign ( = )
– Read the statement: i = j + 7;
11
Data Types and Memory
• Recall that declaring a variable
– sets aside memory and
– indicates the type of data that will be stored
• Considerations in declarations
– What kind of data will you be working with?
• Just whole numbers? Fractions? Characters?
– Is speed important?
• Integer operations are fastest
– How much memory is available?
• Important for embedded systems and microcontrollers
– What is the range of the data?
• From 0 to n, or from –n to +n, etc.
– What kind of precision do you need?
• Precision number of decimal places
12
Hierarchy of
Data Types Data Types
float
unsigned signed
double
long double
char
short
int
long
long long
13
Memory Allocation for Arithmetic Data Types
Signed: -2,147,483,648 to
Signed: -32768 to 32767 2,147,483,647
int Integer 2 4 Unsigned: 0 to 65535 Unsigned: 0 to 4,294,967,295
14
2.6
Constants
Note:
Exponentiation ^ 4^2 16
Multiplication * 16*2 32
Division / 16/2 8
Addition + 16+2 18
Subtraction - 16-2 14
Operator Hierarchy
1. Parenthesis
2. Exponentiation
3. Multiplication/Division
4. Addition/Subtraction
Hierarchy of Operations Example
3 * (6 + 2) / 12 – (7 – 5) ^ 2 * 3 ( ) first
= 3 * 8 / 12 – 2 ^ 2 * 3 ^ next
= 3 * 8 / 12 – 4 * 3 Mult/Div (L to R)
= 24 / 12 – 4 * 3 Mult/Div (L to R)
= 2 – 12 Add/Subtr
= -10
Operators
• Operator: a symbol (or combination of symbols) used to
combine variables and constants to produce a value
(Overland, B. (1995) C in plain English, MIS Press, New York.)
25
Operator Precedence
and Associativity
• All operators have the properties
of precedence and associativity.
• Precedence has to do with which
operations take priority in
groupings of operands around
adjacent operators
• Associativity has to do with which
operations take priority when the
operators in an expression have
the same precedence
• Use parentheses () to specify a
particular grouping order and to
make expressions more readable
26
27
Operator Practice
• Identify the operators, operands, and results
on each line
int i, count, mult, mult2=1, total_sum;
float total_percentage;
i = 1;
mult = total_sum = i;
count = i + 1;
count = count + 1;
total_percentage = total_sum / 100; /* a little tricky! */
mult1 = count * 17.23;
count += 1;
mult2 *= 2;
28
Arithmetic with Mixed Data Types
• Fundamentally, the computer is not able to
arithmetically combine data of different types
– Arithmetic with integers integer results
• int div_int = 8 / 5; /* what is div_int? */
– Arithmetic with floating point data floating point results
• float div_flt = 8.0 / 5.0; /* what is div_flt? */
– Arithmetic with integers and floating point values in the
same expressions ??
• int div_int = 8.0 / 5; /* what is div_int? */
• float div_flt = 8.0 / 5; /* what is div_flt? */
29
Implicit Type Casting
Higher
long double
• In binary operations (e.g., +, -, /, etc.) double
with operands of mixed data types, the float
resultant data type will take the higher unsigned long long
order data type of the two operands1 long long
unsigned long
long
unsigned int
int
unsigned short
short
unsigned char
char
Lower
1
Cheng, Harry H. (2010). C for Engineers and Scientists: An Interpretive Approach, McGraw-Hill, New York. 30
Expressions and Statements
• Expression
– Any combination of operators, numbers, and names that
evaluates to a single value
• 5
• j = 17
• j = i = 10
• i+j
• Statement
– A chunk of code that does something (executes)
• Terminating an expression with a semicolon (;) turns it into a
statement
– We will cover other statements later
31
Practice - operator precedence
■ On paper, determine output of:
int i, j = 3;
i = j + 7;
i = j / 12 + 5;
int k = (j + i * 3);
printf(“%d, %d, %d”, i, j, k);
◻ Output:
32
Practice - operator precedence solution
■ Program Trace:
int i, j = 3; /* j = 3 */
i = j + 7; /* i = 3+7 = 10 */
i = j / 12 + 5; /* i = (3/12)+5 = 9 */
int k = (j + i * 3); /* k=3+27=30 */
printf(“%d, %d, %d”, i, j, k);
◻ Output: 9, 3, 30
33
Reserved Words in C++
You may not use these as variable names
PRINT PRINT
“PASS” “FAIL”
STOP
Example 2
• Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.
Pseudocode:
• Input the length in feet (Lft)
• Calculate the length in cm (Lcm) by multiplying
LFT with 30
• Print length in cm (LCM)
Example 2
Algorithm Flowchart
Print
Lcm
STOP
Example 3
Write an algorithm and draw a flowchart that will
read the two sides of a rectangle and calculate its
area.
Pseudocode
• Input the width (W) and Length (L) of a rectangle
• Calculate the area (A) by multiplying L with W
• Print A
Example 3
Algorithm START
Print
A
STOP
Example 4
• Write an algorithm and draw a flowchart that will
calculate the roots of a quadratic equation
• Algorithm:
Input
• Step 1: Input a, b, c a, b, c
• Step 2: d = sqrt ( )
• Step 3: x1 = (–b + d) / (2 x a) d = sqrt(b x b – 4 x a x c)
• Step 4: x2 = (–b – d) / (2 x a)
x1 =(–b + d) / (2 x a)
• Step 5: Print x1, x2
X2 = (–b – d) / (2 x a)
Print
x1 ,x2
STOP
1 // Fig. 1.2: fig01_02.cpp
Outline
2 // A first program in C++
Comments
3 #include <iostream.h> 1. Comments
Written between /* and */ or following a //.
4 Improve program readability and do not cause the
5 int main() computer to perform any action. 2. Load <iostream>
6 { 3. main
preprocessor directive
7 cout << "Welcome to C++!\n";
Message to the C++ preprocessor.3.1 Print "Welcome to
8 C++\n"
Lines beginning with # are preprocessor directives.
9 return 0; // indicate that program ended successfully
#include <iostream.h> tells the (return 0)
3.2 exit
10 } preprocessor
C++ to include
programs contain onethe
orcontents of the file
more functions, one of
<iostream>, which
which must be main includes input/output
Program
operations (such as printing to the screen).Output
Welcome to C++! Parenthesis are used to indicate a function
int means
Prints the string of characters that main
contained "returns"
between the an integer value.
quotation marks. More in Chapter 3.
return is a way to exit a function
from a function. A left brace begins the body
The entire line, including cout, the{ << operator, theof every function
return 0, in this case, means
string that
"Welcome toand a right brace
C++!\n" } ends
and the it.
semicolon
the program terminated
(;),normally.
is called a statement.