0% found this document useful (0 votes)
11 views

Session 1

Uploaded by

Adel Ebrahim
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)
11 views

Session 1

Uploaded by

Adel Ebrahim
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/ 7

1 Session 1 – Intro & Basics

1.1 Intro
 Who are we?
 What is problem solving?
 Why should we learn problem solving?
 Problem solving competitions.
 Why C++?
 Compilers & IDEs.
 How to use code-blocks?
 Run Hello World program and Explain the
structure of the program.

1.2 Datatypes
Type Name Size
int 4 bytes = 231 = 2*109
Integers
long long 8 bytes = 263 = 9*1018
float 4 bytes
Floating Numbers
double1 8 bytes
Boolean bool 1 byte (True or False)
String string (Between “ “) --
Character char (Betwee ‘ ‘) --

1
Always use double
1|Session 1 – Intro & Basics
1.3 Identifiers
 Sequence of one or more letters, digits, or underscore characters (_).
 Spaces, punctuation marks, and symbols cannot be part of an identifier.
 Always begin with a letter or underscore characters (_).
 Cannot use reserved words.
 C++ is case sensitive.

1.4 Declaring Variables


A variable must be defined before you can use it in a program. When you
define a variable the type is specified and an appropriate amount of memory
reserved. This memory space is addressed by reference to the name of the variable.
A simple definition has the following syntax:

int a, b, c; int a;
int b;
int c;

1.5 Initializing Variables


A variable can be initialized, i.e. a value can be assigned to the variable, during
its definition. Initialization is achieved by placing the following immediately after the
name of the variable:

string s = "Hello World!"; string s;


char c = 'a'; s = "Hello World!";
int y = 0;

1.6 Operations
If a program is to be able to process the data input it receives, you must define
the operations to be performed for that data. The operations being executed will
depend on the type of data — you could add, multiply, or compare numbers, for
example.

2|Session 1 – Intro & Basics


1.6.1 Assignment Operator (=)
An assignment uses the assignment operator = to assign the value of a
variable to an expression.

x = 5;
y = 2 + x;
x = y = z = 5;

1.6.2 Arithmetic Operators ( + , - , * , / , % )


Arithmetic operators are used to perform calculations.

int x = 11 / 3 = 3; //Division(/)1
11 % 3 = 2; // Module(%)2

1.6.3 Compound Assignment

y+=x; y=y+x;
x-=5; x=x-5;
x/=y; x=x/y;
price *=units+1; price=price * ( units+1);

1.6.4 Increment and decrement ( ++ , --)

x=0;
++x;
x+=1;
x=x+1;
x = 3; x = 3;
y = ++x; y = x++;
// x contains 4, y contains 4 // x contains 4, y contains 3

1
Divisions performed with integral operands will produce integral results; for example, 7/2 computes to 3 . If
at least one of the operands is a floating-point number the result will also be a floating-point number; e.g., the
division 7.0/2 produces an exact result of 3.5.
2
Remainder division is only applicable to integral operands and returns the remainder of an integral division.
For example, 7%2 computes to 1.
3|Session 1 – Intro & Basics
1.6.5 Relational and comparison operators ( == , != , > , < , >= ,
<= )
( Equal to ,Not equal to ,Less than , Greater than , Less than or equal to , Greater
than or equal to )
(7 == 5) // evaluates to false
(5 > 4) // evaluates to true
(3 != 2) // evaluates to true
(6 >= 6) // evaluates to true
(5 < 5) // evaluates to false

(a == 5) // evaluates to false, since a is not equal to 5


(a*b >= c) // evaluates to true, since (2*3 >= 6) is true
(b+4 > a*c) // evaluates to false, since (3+4 > 2*6) is false
((b=2) == a) // evaluates to true

1.6.6 Logical operators ( ! , && , || )


The logical operators comprise the Boolean operators && (AND), || (OR), and
! (NOT).
They can be used to create compound conditions and perform conditional
execution of a program depending on multiple conditions. A logical expression
results in a value false or true, depending on whether the logical expression is correct
or incorrect.
Operator Means Explanation
AND True if both statements
&&
are true.
OR True if either statement is
||
true.
NOT The negative of the
!
statement.

!(5 == 5) // evaluates to false because the expression at its right (5 == 5) is true


!(6 <= 4) // evaluates to true because (6 <= 4) would be false
!true // evaluates to false
!false // evaluates to true

( (5 == 5) && (3 > 6) ) // evaluates to false ( true && false )


( (5 == 5) || (3 > 6) ) // evaluates to true ( true || false )

4|Session 1 – Intro & Basics


1.7 Input & Output
1.7.1 cin / cout
cin >> (variable name) ;
cout<< (variable name or value );

1.7.2 endl

cout << 3 << endl << 5;


// cout << 3 << "\n" << 5;
Output:
3
5

1.7.3 set precision


(dealing with doubles): determine the number of digits after the decimal point

double x=3.3465473423;
cout << fixed << setprecision(3) << x;
// Output
3.346

1.8 Comments
// for one line
/* For
More
Than
One
Line
*/

5|Session 1 – Intro & Basics


1.9 Example1
// i/o example

#include <bits/stdc++.h>
using namespace std;

int main ()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";
return 0;
}

1.10 Online Judge


 What is an online judge and how to deal with it?
 Time limit and memory limit
 Different results for your code
o Accepted
o Presentation error
o Wrong answer
o Time Limit Exceed
o Memory Limit Exceed
o Run time Error
 Pay attention to the output format
 Don’t forget to use endl if it is needed

1
// curly Braces {} Enclose Functions, if statement code and loops
// don’t forget semicolon ;
#include <bits/stdc++.h> includes all libraries so you don’t have to waste your time including every library and
it’s important for the next sessions so use it in every program.
6|Session 1 – Intro & Basics
1.11 Problems
Area of a Circle < 1002 >
Difference < 1007 >
Time conversion < 1019 >

1.12 Problem Sheet


Age in Days < 1020 >
Fuel spent < 1017 >
Distance < 1016 >
Consumption < 1014 >
Area <1012 >
Salary with Bonus < 1009 >
Average 2 < 1006 >

7|Session 1 – Intro & Basics

You might also like