CP101 Lecture1 2022
CP101 Lecture1 2022
● You are given a set of problems, you need to write code to solve
these problems.
} Further Reading:
http://www.cplusplus.com/doc/tutorial/
program_structure/
Code Template
int main(): The main function which
gets executed by default when you run
#include<bits/stdc++.h>
the program.
} http://www.cplusplus.com/doc/tutorial/
program_structure/
Primitive Data Types
● Further Reading :
https://www.geeksforgeeks.org/c-d
ata-types/
Operators
Try these:
1. (100-84)%6
2. (84-100)%6 *
3. (76*91)%15
Operators
Logical Operators (&&, ||, !) also return boolean values and are
extremely useful
Eg: AND (&&)
int myAge = 19;
bool eligible = (myAge >= 18);
bool hasCard = true;
bool canVote = (eligible && hasCard);
Operators
Eg: OR(||)
bool goUp = true;
bool goDown = false;
bool isActive = (goUp || goDown);
Operators
Eg: NOT
int marks = 55;
bool passed = !(marks<33);
equivalent to (marks>=33);
Operators
Eg:
bool hasLicense = true;
bool isDrunk = false;
bool canDrive = true;
Operators
Eg:
bool hasLicense = true;
bool isDrunk = false;
bool canDrive = (hasLicense && !isDrunk);
Input and Output
Standard streams are used for reading input and writing output
In C++, the standard streams are cin for input and cout for
output.
The input for the program usually consists of numbers and strings
that are separated with spaces and newlines.
int a, b;
string x;
cin >> a >> b >> x;
int a, b;
string x;
cin >> a >> b >> x;
string s = "CSEA";
cout << a << " " << endl << b << " " << s;
Often cin and cout are slow and less efficient than C
counterpart scanf and printf
Hence to make code efficient, we add the following lines in
the beginning which makes the program execution much faster.
ios::sync_with_stdio(0);
cin.tie(0);
cin doesn’t ignore white spaces so in case when want to read
a string with whitespaces then we have to use the getline
function
string a; string a;
getline(cin,a); cin>>a;
cout<<a; cout<<a;
Further Reading :
http://www.cplusplus.com/doc/tutorial/basic_io/
Conditional statements
if(condition){ if(condition1){
// do thing 1
//Do something ...
} }
else if(condition2){
// do thing 2
...
}else if(condition3){
if(condition1){ // do thing 3
// do thing 1 ...
} }else{
else{ // optional
// optional ...
} }
switch statements
switch(var){
case n1 : // code if var == n1
break;
case n2 : // code if var == n2
break;
case n3 : // code if var == n3
break;
default : //~else
}
● CP Handbook (https://cses.fi/book/book.pdf)
● thenewboston (youtube channel) for basic c++
● cplusplus.com for advanced c++ , keep googling the things you
don’t know
● geeksforgeeks.com, DSA Articles and implementations, may or
may not be 100% correct so verify once
● cp-algorithms.com : Implementations and to the point theory
● CLRS : for deep study of DSA, part of syllabus in 3rd sem CSE.
Some problems
● Set up C++
○ Use Codeblocks IDE (from Bucky’s tutorial)
○ Use VSCode and GCC-compiler (https://youtu.be/jvg4VtYEhKU) or
(https://www.geeksforgeeks.org/how-to-setup-competitive-programming-i
n-visual-studio-code-for-c/ )
● Solve problems on Hackerrank Problem Solving section
(here) and C++(here)
● Ask doubts on the Teams chat if you face any.
● Create accounts on Hackerrank, Codechef, Atcoder and
Codeforces. Fill the form which will be floated.
That’s all
You can ask your doubts on the Teams chat
anytime.