Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
24 views61 pages

Slot03 04 BasicComputation

This document covers the fundamentals of basic computations in programming, focusing on variables, data types, and memory operations. It explains the importance of data representation, including integral and floating-point types, and how to declare and manipulate variables in C. Additionally, it discusses the use of literals, constants, and expressions, along with input/output operations in programming.

Uploaded by

fcbalong222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views61 pages

Slot03 04 BasicComputation

This document covers the fundamentals of basic computations in programming, focusing on variables, data types, and memory operations. It explains the importance of data representation, including integral and floating-point types, and how to declare and manipulate variables in C. Additionally, it discusses the use of literals, constants, and expressions, along with input/output operations in programming.

Uploaded by

fcbalong222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 61

Slots 03-04

Basic Computations
Variables
Basic Memory Operations
Expressions
Objectives

A problem needs to be represented by data. After studying


this chapter, you should be able to:

• Understand what is a data type

• Declare constants and variables of a program


• Express operations on data

Basic Computations 2
Contents

• Variables and Data types • Expressions


– Data Types – Arithmetic
– Integral Types – In-Class Problem
– Floating-Point Types – Statistics: Which operators should be
– Declarations used?
• Basic Memory Operations – Relational
– Literals – Logical
– Constants – Bit operators
– Assignment Operator – Shorthand Assignment Operators
– Output – Mixing Data Types
– Input – Casting
– Precedence

Basic Computations 3
Review

• Computer program: A set of instructions that computer hardware will execute.


• Issues for a program/software: Usability, Correctness, Maintainability, Portability
• Computer software: A set of related programs
• Steps to develop a software: Requirement collecting, Analysis, Design, Implementing,
Testing, Deploying, Maintaining
• Data: Specific values that describe something
• Information: Mean of data
• Fundamental Data Units: Bit, Nibble, Byte, KB, MB, GB, TB
• Data Representation: Number systems: 2, 10, 8, 16
• Program Instructions: <opcode, operand1, operand 2>
• Programming Languages: Machine language, Assembly, High-level languages

Basic Computations 4
Introduction

• Instruction: A task that hardware must perform on data.


• Data can be: constants, variables.
• Constants: Fixed values that can not be changed when the
program executes.
• Variables: Values can be changed when the program execute.
• Data must be stored in the main memory (RAM).
• 2 basic operations on data are READ and WRITE.
• Numerical data can participate in expressions.

Basic Computations 5
1- Variables and Data Types

A variable is a name referencing to a 0000 1001


memory location (address) 1100 0011
c
 Holds binary data
 2 basic operations: set value, get
value.
 When the program is compiled, the b
compiler will determine the position
where the variable is allocated. a
Questions:
(1) Where is it?  It’s Address
(2) How many bytes does it occupy? 
Data type

Basic Computations 6
Variables and Data Types…

Data Types:
• Typed languages, such as C, subdivide the universe of data values
into sets of distinct type.
• A data type defines:
– How the values are stored and
– How the operations on those values are performed.
• Typed languages defined some primitive data types.

Basic Computations 7
Variables and Data Types…
C has 4 primitive data types:
Type Length Range
int Word -32,768 to 32,767 (16 bit)
(length of CPU -2,147,483,648 to 2,147,483,647 (32 bit)
register)
char byte -128 to 127
float 4 bytes 3,4 * 10-38 to 3,4 * 1038
double 8 bytes 1,7 * 10-308 to 1,7 * 10308

Basic Computations 8
Variables c:2293623
and Data‘A’Types…
Where are variables stored and i:2293616 1
how many bytes do they occupy? 1000
l:2293612

f:2293608 0.5

12.809
d:2293600

The operator & will get the address of a variable or code.


The operator sizeof(var/type) return the size (number of byte) occupied by a
variable/type
Basic Computations 9
Variables and Data Types…

Qualifiers:
• We can qualify the int data type so that it contains a minimum
number of bits.
• Qualifiers:
– short :at least 16 bits
– long: at least 32 bits
– long long: at least 64 bits
• Standard C does not specify that a long double must occupy a
minimum number of bits, only that it occupies no less bits than a
double.

Basic Computations 10
Variables and Data Types…
Representation of Integral Values:
• C stores integral values in equivalent binary form.
Each CPU family
• Non-Negative Values: has it’s own way to
– Intel uses this little-endian ordering. store data . So,
compilers must
– Motorola uses big-endian ordering. have a suitable way
for copying data
09 03 from this variable
103 Value is 103 to other. And due
F5 stored: E1 to this reason also,
102 102
09F5E103 each compiler can
E1 F5
101 101 run well only on a
03 09
100 100 specific family of
CPU only. A
Least significant is stored Most significant is stored
supplier may
in lowest byte in lowest byte
supply some
versions of their
compiler for some
Basic Computations CPU families. 11
Variables and Data Types…

Exercises:
• Convert the following decimal integers to binary:
63 __________________________________
219 _________________________________
• Convert the following binary notation to decimal:
• 0111 0101 ___________________________
• 0011 1011 ___________________________
• 01011011 ___________________________

Basic Computations 12
Variables and Data Types…

Negative and Positive Values:


• Computers store negative integers using encoding schemes:
– two's complement notation,
– one's complement notation, and
– sign magnitude notation.
• All of these schemes represent non-negative integers identically.
• The most popular scheme is two's complement.
• To obtain the two's complement of an integer, we
– flip the bits ( 1-complement)
– add one  2-complement
• For example:

Basic Computations 13
Variables and Data Types…

Exercises (Use signed 1-byte integral number):


• What is the two's complement notation of
-63____________________________
-219___________________________
• Convert the following binary notation to decimal:
1111 0101______________________
1011 1011______________________

Basic Computations 14
Variables and Data Types…

Unsigned Integers:
• We can use all of the bits available to
store the value of a variable.
• With unsigned variables, there is no need
for a negative-value encoding scheme.

Basic Computations 15
Variables and Data Types…
Cultural Symbols (characters):
• We store cultural symbols using an integral data type.
• We store a symbol by storing the integer associated
with the symbol.
• Over 60 encoding sequences have already been
defined.

We use the ASCII encoding sequence throughout this course

Basic Computations 16
Variables and Data Types…

The ASCII
table for
characters

Basic Computations 17
Variables and Data Types…

Exercises:
• What is the ASCII encoding for
'0' ___________________________________________
'a' ___________________________________________
'A' ___________________________________________

• What is the EBCDIC encoding for


'0' ___________________________________________
'a' ___________________________________________
'A' ___________________________________________

• Convert the following binary notation to an ASCII character:


0110 1101 _____________________________________
0100 1101 _____________________________________

• Convert the following decimal notation to an EBCDIC character:


199 ______________________
35 __________________________

Basic Computations 18
Variables and Data Types…
Representation of Floating-Point Data :
• Computers store floating-point data using two separate
components:
– an exponent and
– a mantissa (phần định trị)

1.2345 = 1.2345 *100


123.45 = 1.2345 *102
1234.5 = 1.2345 *103
0.0012345 = 1.2345 *10-3

Give your comment about position of the point mark and it’s mantissa

Basic Computations 19
Variables and Data Types…

IEEE 754, 32 bits float:

Basic Computations 20
Variables and Data Types…
IEEE 754, 64 bits double :

Basic Computations 21
Variables and Data Types…
Limits on float and double data type in the IEEE standards :

Basic Computations 22
Variables and Data Types…
Variable Declarations in C:
data_type identifier [= initial value];
• For example:
char section;
int numberOfClasses;
double cashFare = 2.25;
Naming Conventions: Name is one word only
– must not be a C reserved word
– Some compilers allow more than 31 characters, while
others do not. To be safe, we avoid using more than 31
characters.
Letter or
Letters/digits/ ‘_’
‘_’

Basic Computations 23
Variables and Data Types…
Exercises:
Which of the following is an invalid identifier?
whale giraffe's camel_back 4me2
_how_do_you_do senecac.on.ca digt3 register
• Select a descriptive identifier for and write a
complete declaration for:
– A shelf of books__________________________
– A cash register___________________________
– A part_time student_______________________
– A group of programs______________________

Basic Computations 24
Variables and Data Types…

Some operations on variables:


– assign a constant value to a variable,
– assign the value of another variable to a variable,
– output the value of a variable,
– input a fresh value into a variable's memory location.

constant
AnotherVar
anotherVar
Monitor
Keyboard variable
File
File Netwwork
Network

Basic Computations 25
Questions as Summary

• What is a variable?
• What is a data type?
• Characteristics of a data type are …… and …
• The size of the int data type is …. Bytes.
• Choose the wrong declarations:
int n=10;
char c1, c2=‘A’;
int m=19; k=2;
char c3; int t;
float f1; f2=5.1;
• Explain little-endian ordering and big-endian ordering.

Basic Computations 26
2- Literals
• Constant values are
specified directly in
the source code.
• They can be
– Character literals
(constant characters)
– String literals(constant
strings)
– Number literals
(constant numbers)

Basic Computations 27
Literals: Characters, Strings
4 ways for representing a character literal:
• 04 ways:
– Enclose the character single quotes - for example 'A',
– Decimal ASCII code of the character : 65 for 'A‘
– Octal ASCII code of the character: 0101 for 'A',
– Hexadecimal ASCII code of the character: 0x41 for 'A',

Assign value to a variable:


The operator =

Basic Computations 28
Literals: Escape Sequences
• Pre-defined literals for special actions:

Basic Computations 29
Literals: Escape Sequences…
Error!
Why?

Modify
then run it

Change \ to \\ then run it


Basic Computations 30
Literals: Numbers
• The compiler will convert directly numeric literals
(constants) to binary numbers and put them in the
executable file.  How long of binary constants?  They
depend on their data types specified by programmers.
• Default: Integral value  int, real number  double
• Specifying data type of constants: Suffixes after
numbers.

Basic Computations 31
3- Named Constants
• Use the pre-processor (pre-compiled directive)
#define or the keyword Compiler
const will allocate memory location for
constants that are declared using the keyword
const

Basic Computations 32
Named Constants…
Attention when the directive #define is used:
• The compiler will not allocate memory block for values but all pre-
defined names in the source code will be replaced by their values
before the translation performs (The MACRO REPLACEMENT)
• A name is call as a MACRO.
(… , PI*3*3)

(… , =3.14;*3*3)

Basic Computations 33
Fill the blank

• 3 ways to specify a constant in a source program: use


a literal, use the keyword…., and specify a macro
using the directive …..

Basic Computations 34
4- Input/Output Variables

Monitor is
a character 3 3
device
Binary
code of 3 Convert
for 00110011
ASCII code calculating
of the digit 00110011 00000011
3 convert
00110011

Keyboard is 3 3
a character
device

Data inputted from keyboard are ASCII codes


Data printed out to monitor are ASCII codes Conversion rules are pre-defined in C

Basic Computations 35
Input/Output Variables…
Conversion
Specifiers

Basic Computations 36
Input/Output Variables…
4210784 n

main
4199056

2293620 m

Format string

scanf( “%d%d”, &n, &m) 


scanf( “%d%d”, 4210784, 2293620)
means that get keys pressed then
change them to decimal integers and
store them to memory locations
4210784, 2293620.

Basic Computations 37
Input/Output Variables…
Input a value to a variable:
scanf (“input format”, &var1, &var2,…)
Output the value of a variable:
printf (“output format”, var1, var2,…)

The function scanf receive the BLANK or


ENTER KEYS as separators.

Format string
Data holders

Basic Computations 38
Questions

• Explain means of parameters of the scanf(…)


and the printf(…) functions.

• Use words “left” and “right”. The assignment


x=y; will copy the value in the ….. side to the
….. Side.

Basic Computations 39
Exercises
1- Develop a C program in which 2 integers, 2 float numbers
and 2 double numbers are declared. Ask user for values
of them then print out values and addresses of them.
Write down the memory map of the program.
2- Run the following program:
Why user do not have a
chance to stroke the
ENTER key before the
program terminate?

Modify and re-run:


getchar();
getchar();

Basic Computations 40
5- Expressions

• Expression is a valid association of constants, variables,


operators and functions and returns an only result.
• Examples:
 32-x+y/6 16.5 + 4/sqrt(15) * 17 – 8
 45 > 5*x y = 17 + 6*5/9 –z*z
• Hardware for calculating expressions: ALU
• Operations that can be supported by ALU: Arithmetic,
relational and logic operations.

Basic Computations 41
Expressions: Arithmetic Operators
Op. Syntax Description Example
+ +x leaves the variable, constant or y = +x ;  y = x;
expression unchanged
- -x reverses the sign of the variable y= -x;
+ - x+y x-y Add/substract values of two z= x+y; t = x-y;
operands
*/ x*y x/y Multiplies values of two operands z= x-y;
Get the quotient of a division z = 10/3;  3
z = 10.0/3;  3.3333333
% x%y Get remainder of a integral division 17%3  2
15.0 % 3  ERROR
++ ++x --x Increase/decrease the value of a Demo in the next slide.
-- x++ x-- variable (prefix/postfix operators)

Basic Computations 42
Expressions: Arith. Operators…

Explain yourself the output

Basic Computations 43
Expressions: Arith. Operators…

?
Explain yourself the output

Statistic:
•Multiply > Division
•Integral operations > floating-point ones.

Basic Computations 44
Expressions: Relational Operators
• For comparisional operators.
• < <= == >= > !=
• Return 1: true/ 0: false

Basic Computations 45
Expressions: Logical Operators

• Operator for association of conditions


• && (and), || (or) , ! (not)
• Return 1: true, 0: false

Basic Computations 46
Expressions: Bitwise Operators
• & (and), | (or) , ^ (xor): Will act on a pair of bits at the same
position in 2 operands.
• << Left shift bits of the operand (operands unchanged)
• >> Right shift bits of the operand (operands unchanged, the sign
is preserved.)
n&m
• ~ : Inverse bits of the operand. 0000 0000 0000 1100
0000 0000 0000 1000
n=12: 0000 0000 0000
0000 0000 0000 1000  8
1100
m= 8: 0000 0000 0000 n|m
1000 0000 0000 0000 1100
0000 0000 0000 1000
0000 0000 0000 1100  12
n^m
0000 0000 0000 1100
0000 0000 0000 1000
0000 0000 0000 0100  4

Basic Computations 47
Expressions: Bitwise Operators…

n=12: 0000 0000 0000 1100


n<<1:
0000 0000 0000 1100
0000 0000 0001 100  0
0000 0000 0001 1000 (2410)
Left shift 1 bit: multiply by 2

k=-1:
n=12: 0000 0000 0000 1100 1: 0000 0000 0000 0001
n>>1: -1: 1111 1111 1111 1111 (2-complement)
Sign: 0 Sign: 1
0000 0000 0000 1100
0000 0000 0000 110 1111 1111 1111 1111
Add the sign to the left” 111 1111 1 111 1111
0000 0000 0000 110  6 Add the sign to the left:
1111 1111 1111 1111  (-110)

Basic Computations 48
Expressions: Assignments
Operators
• Variable = expression
• Shorthand assignments:

Basic Computations 49
Expressions: Mixing Data Types

• Although the ALU does not perform operations on


operands of differing data type directly, C compilers can
interpret expressions that contain operands of differing
data type.
• If a binary expression contains operands of differing type,
a C compiler changes the data type of one of the
operands to match the other.
• Data type hierarchy: double, float, long, int , char .

Basic Computations 50
Expressions: Mixing Data Types
• Casting Data Type: x = y; 00000000 00000000
00000000 00000000
00000000 00000000
00011001 00011001

00000000
00000000
00000000
01000001 01000001

00000000
00000000
00000000
01000001 00011001

Direction for copying: From the lowest byte to higher bytes

Basic Computations 51
Expressions: Mixing Data Types
• Implicit Casting for the
assignment
• If the data type of the variable on
the left side of an assignment
0000 0000
operator differs from the data type 0000 0000
of the right side operand, the 0000 0000
compiler 0100 0001 0100 0001
– promotes the right operand to the
data type of the left operand if the left 0000 0000
operand is of a higher data type than 0000 0000
the right operand, 0000 0000
– truncates the right operand to the data 0001 1001 0001 1001
type of the left operand if the left
operand is of a lower data type than
the right operand.
Basic Computations 52
Expressions: Mixing Data Types
• Implicit Casting for arithmetic and relational expressions
If the operands in an arithmetic or relational expression differ in
data type, the compiler promotes the value of lower data type to
a value of higher data type before implementing the operation.

int n=3; long t=123; double x=5.3;


3*n + 620*t – 3*x
(int*int) + (int*long) - (int*double)
int + long - double
long - double
double
Basic Computations 53
Expressions: Mixing Data Types
• Explicit
Casting
We may
temporarily
change the
data type of 00000000
any operand in 00000000
00000001
any expression
00000000 00000000
to obtain a c n
result of a
certain data
type.
Basic Computations 54
Expressions: Operator Precedence
• In a expression containing some more than one operator.
Which operator will perform first?  Pre-defined Precedence.
• We can use ( ) to instruct the compiler to evaluate the
expression within the parentheses first

int m=3, k=2, n=4;


What is the results?
m<n
k<m<n
k>m>n
m<n>k
m&& k<n

Basic Computations 55
Summary
• Variable is ……
• Basic memory operations are…..
• Expression is ……….
• Which of the following operators will change value of a
variable? + - * / % ++
• Which of the following operators can accept only one
operand? + - * / % --
• 13 & 7 = ?
• 62 | 53 = ?
• 17 ^ 21 = ?
• 12 >> 2 = ?
• 65 << 3 = ?

Basic Computations 56
Summary
• Expressions
– Arithmetic operators
– Relational operators
– Logical operators
– Bit operators
– Shorthand Assignment Operators
– Casting
– Precedence

Basic Computations 57
Extra Exercise

Basic Computations 58
Thank You

Basic Computations 59
Bài tập

• Viết chương trình yêu cầu người dùng nhập vào năm sinh
và kiểm tra người dùng đang ở độ tuổi nào: trẻ em (0-6
tuổi), thiếu niên (7-17 tuổi ), thanh niên (18-35 tuổi), trung
niên (36-50 tuổi), người cao tuổi (>50 tuổi).
• In ra số năm còn lại để trở thành người cao tuổi

Basic Computations 60
•https://t.me/BGuruDev

Basic Computations 61

You might also like