Session 1
Session 1
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.
int a, b, c; int a;
int b;
int c;
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.
x = 5;
y = 2 + x;
x = y = z = 5;
int x = 11 / 3 = 3; //Division(/)1
11 % 3 = 2; // Module(%)2
y+=x; y=y+x;
x-=5; x=x-5;
x/=y; x=x/y;
price *=units+1; price=price * ( units+1);
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
1.7.2 endl
double x=3.3465473423;
cout << fixed << setprecision(3) << x;
// Output
3.346
1.8 Comments
// for one line
/* For
More
Than
One
Line
*/
#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
// 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 >