Week 2
Week 2
• Course introduction
• Problem Solving & Algorithms
• Computer Systems and Architectures
• From Machine code to Programming Languages
• Introduction to programming languages
• Writing your first program
VISUAL STUDIO 2022 INSTALLATION
https://visualstudio.microsoft.com/vs/features/cplusplus/
Choose Community 2022
Mark these 2 options:
• Data types are used to tell the computer what kind of information a variable
can hold
• Common data types are: int, long, float, double, char, bool
• Each data type has its own purpose and application
BASIC NUMERICAL DATA TYPES
Type Size (in bytes) Minimum value Maximum value
short int 2 -32,768 32,767
int 4 -2,147,483,648 2,147,483,647
long At least 4 Same as int mostly Same as int mostly
long long 8 - 9,223,372,036,854,77
9,223,372,036,854,77 5,807
5,808
The range (min-max values) is result of the size of the data type, e.g. the short int uses 2 bytes = 16 bits.
Computer is a binary system where there are two possible states of transistors – on and off (0 and 1),
therefore we can calculate the min max values by simply using the formula 2x where x = number of bytes.
In case of short int, it gives us 65536. The min value is –(65536/2), while max being (65536/2) – 1
You will learn about how to represent numbers in a binary format in some other lecture
VARIABLE DECLARATION EXAMPLE
Data type
Variable name
Assign operator
• Variable declaration should start with the data type (int, float and others)
• Data type should be followed by the variable name
• Variable name can contain letters, digits and underscores
• Variable names must begin with a letter or an underscore
• Variable names are case-sensitive (numberOne and numberone are different variables)
• Variable names cannot contain whitespaces or special characters like !, #, %, etc.
• After the variable name declaration, you may either end the statement with a semicolon (;) or
assign a value to the variable directly (more about this later)
BASIC NUMERICAL DATA TYPES EXAMPLE
FLOATING POINT NUMBERS
• Data types: float, double, long double are considered floating point numbers
• Are used to represent numbers with decimal spaces, such as: 9.5
• Can be precise for up to 7-20 digits after the dot, depends on the data type
• Not very good at storing precision-sensitive values, as keeping floating point
numbers precisely in a computer is not possible due to the way the computer is
storing data (in binary format)
• Use for simple numerical cases, do not use for things like currency rate and money
FLOATING POINT NUMBERS EXAMPLE
WRITING FULL VERSION OF FLOATING POINT NUMBERS
IN COUT
You may notice that in previous example, the latitude and longitude numbers were not displayed fully, you may
use setprecision function in the cout to let it display floating point numbers as they are without cutting them off
Note: you need to include <iomanip> at the top of the file
BOOLEAN DATA TYPE
• Boolean is a data type that can only take two values – true or false (1 or 0)
• Putting the Boolean value in the cout function outputs its numerical value (1 or 0)
STATIC AND DYNAMIC VARIABLE/CONSTANT
INITIALIZATION
ARITHMETIC OPERATIONS
INCREMENTING AND DECREMENTING
• Is used to obtain a
remainder from division
of two numbers
• Can be used just like any
other arithmetic operator,
using the % symbol
DIVIDING WHOLE NUMBERS
• Dividing whole numbers and saving
the result in a whole number loses
the precision if there is any
• If there is a remainder in the
division between two numbers, then
the result should be saved in a
floating point number
• Saving the division result with
precision requires you to do type-
casting, where the result is
converted into a given data type
USEFUL
MATHEMATIC
FUNCTIONS
• We can use the cin function to take in input from the user
• We can use dynamic variable initialization to assign variable to an input
MATHEMATICAL TASK 1: AREA OF A
RECTANGLE