LectureSession - 1
LectureSession - 1
SESSION
Welcome to FCI Damanhour ☺
WHO AM I?
• Reem Ghareeb
• CS student - 3rd year.
• Instructor and problem Setter at ICPC Damanhour
Community.
• Having over 2 years experience in Competitive Programming.
• Backend Developer.
Introduction
CPU (Processor)
What do you
think the Memory (RAM / Hard Disk)
computer
Input Devices
consists of?
Output Devices
Your goals in training
•Programming Concept (Data Types and variables, Input and Output,
Conditions, Loops, Arrays 1D and 2D, Functions and strings, Basic
Recursion, Pointers).
• C++ Language
•How to search.
•Debug, test, and fast in coding.
•Strategy in a contest.
•Organize code and style.
•Learn how to learn
•Build a new network.
•Increase thinking skills.
What is programming?
Computer programming:
It is a way of giving computers instructions about what they should do.
Programming Languages:
It is a formal language, which comprises a set of instructions that produce
various kinds of output, Like C++ and python.
Complier:
It is a program that translates a high-level programming
language (called source code) into machine language (the
target language).
Machine language:
It is a sequence of 0’s and 1’s that the machine (computer)
understands and can interpret into instructions.
Your Data Computer Data
Videos Images
Audios
Text Numbers
Codes
Binary
Representation
ASCII Table
Topics of this week
• The basic structure of C++.
• Comments.
• Data types in C++.
• Declaration variables.
• Operators in C++.
• Input and Output.
• Errors.
Basic structure of C++
Basic structure of C++
Line 1:
Is a header file library that lets us work with input and output
objects, such as cout (used in line 5). Header files add
functionality to C++ programs.
Line 2:
This means that we can use names for objects and variables from
the standard library.
Don't worry if you don't understand how
#include <iostream> and using namespace std
works. Just think of it as something that (almost)
always appears in your program.
Basic structure of C++
Line 3:
A blank line. C++ ignores white space. But we use it to make the code
more readable.
Line 4:
Another thing that always appears in a C++ program, is int main().
This is called a function. Any code inside its curly brackets { } will be
executed.
Basic structure of C++
Line 5:
“cout”, is an object used together with the insertion operator (<<) to
output/print text. In our example, it will output "Welcome to ICPC
Damanhour".
Remember:
The compiler ignores white spaces. However, multiple lines make the
code more readable.
Basic structure of C++
Line 6:
return 0, ends the main function.
Line 7:
Do not forget to add the closing curly bracket “ } “ to actually
end the main function.
Comments:
Single-line Multi-line
Single-line comments start Multi-line comments start
with two forward slashes (//). with /* and ends with */.
Any text between // and the
Any text between /* and */ will
end of the line is ignored by
be ignored by the compiler.
the compiler (will not be
executed). Example:
Example: /* The code below will print
the words Hello World!
// This is a comment to the screen, and it is
cout << "Hello World!"; amazing */
cout << "Hello World!";
Variables
Variables are containers for storing data values.
stores single characters (symbols), such as 'a' or 'B'. Char values are
char 1 byte
surrounded by single quotes.
depends on the stores text, such as "Hello World". String values are surrounded by
string size of the string double quotes.
Note:
1 byte = 8 bits
Exercise
• 9
What is the type • “Hello”
of these values? • ‘A’
• true
• 9.99
Note:
The precision of a floating-point value indicates how many digits
the value can have after the decimal point. The precision of float is
only six or seven decimal digits, while double variables have a
precision of about 15 digits. Therefore, it is safer to use double for
most calculations.
Declaring (Creating)
Variables
Declaring Variables
To create a variable, specify the type and assign it a value:
Syntax: type variableName = value;
Where type is one of C++ types (such as int), and variableName is the name of the
variable (such as x or myName). The equal sign is used to assign values to the
variable.
Example:
Create a variable called myNum of type int and assign it the value 15:
int myNum = 15;
cout << myNum;
Exercise
• Create a variable named myNum
and assign the value 50 to it.
• Create a variable named myName
and assign the value “Salma” to it.
Declaring Variables
Declare Multiples Variables:
To declare more than one variable of the same type, use a comma-separated list:
Example:
int x = 5, y = 6, z = 50;
cout << x + y + z;
You should always declare the variable as constant when you have values
that are unlikely to change:
Example:
const int minutesPerHour = 60;
const float PI = 3.14;
Operators
1.Arithmetic Operators
Operator Name Example Result
+ Addition x=2+4
- Subtraction x=5-3
* Multiplication x=3*2
/ Division x = 10 / 2
% Modulus x=7%3
++ Increment x = 1, x++
-- Decrement x = 4, x--
• Num / 2 => fraction just with odd numbers
• Num % 2 => tell us if num is even or odd
• Num / 10 => remove last digit of num
• Num % 10 => give us last digit of num
• Casting variables (convert double to int)
Operators
Note -> Increment and decrement:
Prefix Postfix
x=3 x = 3;
y = ++x; y = x++;
// x: 4, y: 4 // x: 4, y: 3
Operators
Trace this
code, then tell
us what is the
output.
Operators
Trace this
code, then tell
us what is the
output.
Notes
• int / int = int
• int / float = float
• float / int = float
• float / float = float
• int * int = int
• long long * int = long long
• long long * double = double
• The % operator can only be used with integers.
Operators
2. Assignment Operators
Operator Expression Equivalent to
= y = 3; -----
+= x += 4; x = x + 4;
-= x -= 7; x = x – 7;
*= x *= y; x = x * y;
/= x /= 5; x = x / 5;
%= x %= 2; x = x % 2;
Operators
Trace this
code, then tell
us what is the
output.
Operators
• Problem 1: Write a program that initializes two variables named x
and y with values 3,5 print their sum and subtract and multiply.
(overflow error!)
Errors
10) Runtime Error (Floating point exception):