0% found this document useful (0 votes)
9 views65 pages

LectureSession - 1

Uploaded by

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

LectureSession - 1

Uploaded by

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

Lecture

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".

Note: Every C++ statement ends with a semicolon “; “.

Note: The body of int main() could also be written as:

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.

In C++, there are different types of variables (defined


with different keywords), examples:
variables
Data Type Size Description
stores integers (whole numbers), without decimals, such as 123 or
int 4 byte
-123.
stores integers (whole numbers), without decimals, but big
long long 8 byte
integers.
stores floating point numbers, with decimals, such as 19.99 or
float 4 byte
-19.99.
stores floating point numbers, with decimals, such as 19.99 or -
double 8 byte
19.99 and integers also.

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.

bool 1 byte stores values with two states: true or false.


variables

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;

One Value to Multiple Variables:


You can also assign the same value to multiple variables in one line:
Example:
int x, y, z;
x = y = z = 50;
cout << x + y + z;
Declaring Variables
Declaration and initialize variables:
• In two lines:
int n; //declaration of a variable called n
n = 5; //initialize the variable n and assign a value to it
• In one line:
int x = 10; //declaration and initialize the variable called x
Identifiers
- All C++ variables must be identified with unique -
names.
- These unique names are called identifiers.
- Identifiers can be short names (like x and y) or more
descriptive names (age, sum, totalVolume).

Note: It is recommended to use descriptive names to


create understandable and maintainable code.
The general rules for naming variables are:
• Names can contain letters, digits, and underscores.
• Names must begin with a letter or an underscore (_)
• Names are case sensitive (myVar and myvar are different variables)
• Names cannot contain whitespaces or special characters like (!, #, %, etc.).
• Reserved words (like C++ keywords, such as int) cannot be used as names.
Reserved Keywords in C++
asm double new switch
auto else operator template
break enum private this
case extern protected throw
catch float public try
char for register typedef
class friend return union
const goto short unsigned
continue if signed virtual
default inline sizeof void
delete int static volatile
do long struct while
Constants
When you do not want others (or yourself) to change existing variable
values, use the const keyword (this will declare the variable as "constant",
which means unchangeable and read-only):
const int myNum = 15; // myNum will always be 15
myNum = 10; // error: assignment of read-only variable 'myNum’

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.

• Problem 2: Write a program that initializes a variable named x


with value 123 and print digit (3) digit (2) and digit (1).
(Hint: use modulo)
Operators
• Problem 3: Trace this code… what will the values of x, y, and z will
be?
Input and Output
Input and Output
How do we get something from the user?
cin is a predefined variable that reads data from the
keyboard with the extraction operator (>>).
Input and Output
In the following
example, the user
can input a number,
which is stored in
the variable x. Then
we print the value
of x:
cout is pronounced, ”see-out”. Used for
output, and uses the insertion operator (<<)
cin is pronounced, ”see-in”. Used for input,
and uses the extraction operator (>>)
Input and Output

Example (Creating a Simple Calculator):


the user must input two numbers. Then we
print the sum by calculating (adding) the two
numbers.
New Lines
• To insert a new line, you can use the \n character:
Example:
#include <iostream>
using namespace std;
int main() {
cout << ”Hello World! \n”;
cout << ”I am learning C++”;
return 0;
}
New Lines
• Another way to insert a new line is with the
‘endl’ manipulator.
Example:
#include <iostream>
using namespace std;
int main() {
cout << ”Hello World!” << endl;
cout << ”I am learning C++”;
return 0;
}
• Both \n and endl are used to break lines. However, \n is the
most used and faster than endl.

• But what is \n exactly?


-> The newline character (\n) is called an escape sequence, and
it forces the cursor to change its position to the beginning of
the next line on the screen. This results in a new line.
New Lines
Escape sequence Character represented
\b Backspace
\n New-line
Escape \r Carriage return
\t Horizontal tab
Sequences: \' Single quotation mark
\" Double quotation mark
\? Question mark
\\ Backslash
Errors
Errors
1) Compiler errors (Missing semicolon)
Errors
2) Compiler errors (Be careful with <<)
Errors
3) Compiler errors (Missing double quote char)
Errors
4) Compiler errors (Where is the “main” function?)
You must give the main function.
But we have Main?
No, C++ is character sensitive, it must be
main, not MAIN, or Main, it’s just “main”!
Errors
5) Compiler errors (Missing the braces {} )
Errors
6) Compiler errors (Missing #include <iostream)
Errors
7) Compiler errors (Never print outside this way!) )
Errors
8) Compiler Warning (where is the return?)

Nothing stops the compiler from building the exe,


however, it is better to solve the warnings.
Errors
9) Compiler Warning (Maximum for int is 2147483647)

(overflow error!)
Errors
10) Runtime Error (Floating point exception):

The compiler thinks it’s okay (No Errors, no Warnings)


But, when we divide by 0, the program crashes in the middle!)

You might also like