Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

Week 2

This document provides an agenda and overview for a programming principles course. It discusses installing Visual Studio, writing comments, variables and data types, arithmetic operations, input/output, and mathematical tasks to solve using programming. Students are assigned homework to write programs calculating geometric progression sums and converting between temperature scales.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Week 2

This document provides an agenda and overview for a programming principles course. It discusses installing Visual Studio, writing comments, variables and data types, arithmetic operations, input/output, and mathematical tasks to solve using programming. Students are assigned homework to write programs calculating geometric progression sums and converting between temperature scales.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

PRINCIPLES OF PROGRAMMING

International Black Sea University


Faculty of Business and Technology

Lecturer: Nika Narushvili


1ST WEEK
AGENDA

• 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:

Also choose Windows 10 SDK on the right side


in “Desktop development with C++” -> “Optional”
COMMENTS

• It is possible to write comments in the program


• Comments are not compiled and executed, they are there
just for other programmers to understand your code

• Can be single line (started with //), or multi-line


(surrounded by /* … */)

• Helpful when you want to explain some complex piece


of code or are writing documentation

• Avoid excessive usage of comments, your code should


be self-explanatory and comments should only be added
when they are really needed
VARIABLES AND CONSTANTS

• Variables are used to hold information in the application


• Variables have a name and a specific data type assigned to them
• Variables should have a clear name that tells us about its purpose
• Constant is like a variable but with the only difference – its value is declared
once and can not be changed
BASIC DATA TYPES

• 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

Value of the variable


VARIABLE DECLARATION RULES

• 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

• Incrementing and decrementing causes the


number to either increase or decrease by value of
1
• Can be done in two ways, by placing ++ in front
or at the end of the variable name
• If placed in front, the operation happens before
the current line has started execution
• If placed at the end, the line is executed first, and
then the operation is performed after
MODULUS OPERATOR

• 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

ceil – smallest integer that is >= than


target
floor – largest integer that is <= than
target
round – rounds the target to nearest
integer
abs – absolute value of a number,
removes the minus sign
INPUT IN C++

• 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

• Area of the rectangle can be calculated by just multiplying its sides


• Receive as input the values of sides of a rectangle
• Output the area of the rectangle
MATHEMATICAL TASK 2: PYTHAGOREAN
THEOREM
• In the Pythagorean theorem, the square of the hypotenuse is equal to the sum
of the squares of the catheters
• Receive as input two numbers – catheter lengths of a right triangle
• Use mathematic functions such as sqrt, pow, and arithmetic operations to
calculate a hypotenuse
MATHEMATICAL TASK 3: AREA AND
CIRCUMFERENCE OF A CIRCLE

• Receive the diameter of a circle as input


• Calculate and output both area and circumference of a circle
• Reminder: area = Pi * r2 , Circumference = 2 * Pi * r
MATHEMATICAL TASK 4: ARITHMETIC
PROGRESSION SUM

• Arithmetic progression sum is defined in the formula S = (n/2) * [2a + (n-


1)d]
• Receive as inputs: first term (a), common difference (d) and number of terms
• Calculate arithmetic progression sum and output it
HOMEWORK: GEOMETRIC PROGRESSION
SUM

• Receive as inputs: first term, common ratio and number of terms


• Calculate sum of geometric progression and output it
• Use meaningful variable names, write clean output, ask the user for input,
e.g. write “Please enter the first term: “ before receiving the input
HOMEWORK: TEMPERATURE CONVERSION

• Convert from Fahrenheit to Celsius and vice versa


• Receive input as Fahrenheit, calculate the Celsius value and output it
• After outputting the Celsius value above, receive a new input, this time, a
Celsius, and output its value in Fahrenheit
• Use meaningful variable names, write clean output, ask the user for input,
e.g. write “Please enter the value in Fahrenheit: “ before receiving the input

You might also like