C++ with DSA
C++ with DSA
Advanced DSA
Heaps (Priority Queue)
Hashmaps
Tries
Graph
Greedy Algorithms
Dynamic Programming
Segment Trees
Flowcharts:
Flowcharts are written with program flow from the top of a page to the bottom. Each
command is placed in a box of the appropriate shape, and arrows are used to direct program
flow. The following shapes are often used in flowcharts:
Pseudocode:
Pseudocode is a method of describing computer algorithms using a combination of natural
language and programming language. It is essentially an intermittent step towards the
development of the actual code. It allows the programmer to formulate their thoughts on the
organization and sequence of a computer algorithm without the need for actually following
the exact coding syntax.
Examples –
Ques1. Write a flowchart and pseudocode for finding the sum of 2 numbers.
Pseudocode:
1. Start
2. Input 2 numbers ʹ number1 and number2
3. Add number1 and number2 to find sum
4. Print sum
5. End
Variables:
c. no comma/blank space
Data Types:
Data Type Meaning Size (in Bytes)
int Integer 4
float Floating-point 4
char Character 1
bool Boolean 1
void Empty 0
Compilation:
A computer program that translates C code into machine code
Comments:
Output:
cout<<"hello world";
1. Comments
The two slash(//) signs are used to add comments in a program. It does
not have any effect on the behaviour or outcome of the program. It is
used to give description of the program you’re writing.
2. #include<iostream>
#include is the pre-processor directive that is used to include files in our
program. Here we are including the iostream standard file which is
necessary for the declarations of basic standard input/output library in
C++.
4. int main()
The execution of any C++ program starts with the main function, hence it
is necessary to have a main function in your program. ‘int’ is the return
value of this function. (We will be studying about functions in more detail
later).
5. {}
The curly brackets are used to indicate the starting and ending point of
any function. Every opening bracket should have a corresponding closing
bracket.
6. cout<<”Hello World!\n”;
This is a C++ statement. cout represents the standard output stream in
C++. It is declared in the iostream standard file within the std namespace.
The text between quotations will be printed on the screen.
\n will not be printed, it is used to add line break. Each
statement in C++ ends with a semicolon (;)
7. return 0;
return signifies the end of a function. Here the function is main, so when
we hit return 0, it exits the program. We are returning 0 because we
mentioned the return type of main function as integer (int main). A zero
indicates that everything went fine and a one indicates that something has
gone wrong.
The header file iostream must be included to make use of the input/output
(cin/cout) operators.
#include<iostream>
using namespace std;
int main()
{
cout << "Hello World!";
return 0;
}
To print the content of a variable the double quotes are not used.
#include<iostream>
int main()
{
char Yes = 'y';
cout << Yes;
return 0;
}
The << operator can be used multiple times in a single statement. Take a look
at an example:
#include<iostream>
using namespace std;
int main()
{
cout << "Hello, " << "this is a test " << "string."; return 0;
}
#include<iostream>
using namespace std;
int main()
{
char Yes = 'y';
cout << "Print the character " << Yes;
return 0;
}
The cout operator does not put a line break at the end of the output. So if you
want to print two sentences you will have to use the new-line character ( \n ).
int main(){
}
2.
#include <iostream>
using namespace std;
int main() {
cout << " *" << endl;
cout << " ***" << endl;
cout << " *****" << endl;
cout << " *******" << endl;
cout << "*********" << endl;
return 0;
}
Type conversion:
Implicit: small to big data type
#include <iostream>
using namespace std;
int main() {
char grade = 'A'; //65 this the ASCII value of the
uppercase letter 'A' is 65
int value = grade;
cout << value ;
return 0;
}
Type casting:
Explicit : big to small data type
#include <iostream>
using namespace std;
int main() {
double price = 75.3;
int newPrice = (int)price;
cout << newPrice << endl;
return 0;
}
Operators in C++
Operators are nothing but symbols that tell the compiler to perform some
specific operations. Operators are of the following types -
1. Arithmetic Operators
Arithmetic operators perform some arithmetic operation on one or two
operands. Operators that operate on one operand are called unary
arithmetic operators and operators that operate on two operands are
called binary arithmetic operators.
#include <iostream>
using namespace std;
int main() {
int a = 20, b = 10;
cout << "sum = " << (a + b) << "\n";
cout << "difference = " << (a - b) << "\n";
cout << "product = " << (a * b) << "\n";
cout << "division = " << (a / b) << "\n"; // 2
cout << "modulo = " << (a % b) << "\n"; // 0
return 0;
}
Int/Int = Int
#include <iostream>
using namespace std;
int main() {
int a = 12, b = 5;
cout << (5/2) << endl;
return 0;
}
Int/double = double
#include <iostream>
using namespace std;
int main() {
int a = 5;
double b = 2;
cout << (a / b) << endl;
return 0;
}
Alternate method
#include <iostream>
using namespace std;
int main() {
> Gives true if left operand is more than right A>B is not
operand true
< Gives true if left operand is less than right A<B is true
operand
>= Gives true if left operand is more than right A>=B is not
operand or equal to it true
<= Gives true if left operand is more than right A<=B is true
operand or equal to it
#include <iostream>
using namespace std;
int main() {
#include <iostream>
using namespace std;
int main() {
#include <iostream>
using namespace std;
int main() {
int a,b;
cout << "Enter a number "<< "a =" << "\n";
cin >> a;
cin >> b;
Conditions:
if/else:
The if block is used to specify the code to be executed if the condition specified
in if is true, the else block is executed otherwise.
#include <iostream>
using namespace std;
int main() {
int a= 10;
if(a>=0){
cout<< "positive\n";
}else{
cout<< "negative\n";
}
return 0;
}
#include <iostream>
using namespace std ;
int main () {
int age ;
cout<<"Enter your age\n";
cin>>age;
if ( age >= 18 ) {
cout << "You can vote." ;
}
else {
cout << "Not eligible for voting." ;
}
return 0 ;
}
#include <iostream>
using namespace std ;
int main () {
int n;
cout<<"Enter your number";
cin>>n;
if(n%2==0){
cout<<"Even"<<endl;
}else
{
cout<<"Odd"<<endl;
}
return 0 ;
}
else if:
To specify multiple if conditions, we first use if and then the consecutive statements use
else if.
#include <iostream>
using namespace std ;
int main () {
int marks;
cout<<"Enter your marks";
cin>>marks;
if (marks>= 90){
cout<<"Grade A \n";
}
else if (marks>=80)
{
cout<<"Grade B \n";
}
else{
cout<<"Grade C \n";
}
return 0 ;
}
Loops in C++:
A loop is used for executing a block of statements repeatedly until a particular condition is
satisfied. A loop consists of an initialization statement, a test condition and an increment
statement.
While loop:
initialization;
while (condition) {
// Code to execute
update;
}
#include<iostream>
int main(){
int count = 1;
while (count<=10){
cout<<count<< " " ;
count++;
}
} output: 1 2 3 4 5 6 7 8 9 10
Alternate method:
#include<iostream>
int main(){
int n = 10;
int count = 1;
while (count<=n){
cout<<count<< " " ;
count++;
}
} output: 1 2 3 4 5 6 7 8 9 10
#include<iostream>
using namespace std;
int main(){
int n = 10;
int count = 1;
while (count<=n){
cout<<count<< " " ; 1111111……nth
;
}
}
for loop:
NOTE: - All the tasks that can be done using a while loop can also be done using a for
loop.
#include <iostream>
using namespace std;
int main () {
int n = 10;
}
QS. Print odd number from 1 to 10.
#include <iostream>
using namespace std;
int main () {
int n = 10;
} Output: 1 3 5 7 9
QS. Sum of 5 numbers from 0 .
#include<iostream>
using namespace std ;
int main(){
int n = 5;
int sum = 0;
for (int i=1;i<=n;i++){
sum= sum+i; //sum+=1
}
cout<<"sum = "<<sum <<"\n";
}
Output:15
QS. Using Conditional statements and break statements in loop.
#include<iostream>
using namespace std ;
int main(){
int n = 5;
int sum = 0;
for (int i=1;i<=n;i++){
sum= sum+i;
if(i==3){
break;
}
}
cout<<"sum = "<<sum <<"\n";
}
Output: 1+2+3= 6
Do while loop:
The syntax for while loop is:
do {
// body of loop;
}
while (condition);
From Do While loop
#include <iostream>
using namespace std ;
int main () {
do{
cout<<"hello world!\n";
}while (3>5);
return 0 ;
}Output: hello world!
return 0 ;
}
Output : Empty
Nested Loop:
A nested loop is a loop inside another loop. The inner loop runs completely for each
iteration of the outer loop.
#include <iostream>
using namespace std;
int main() {
int n= 3;
int m= 3;
for (int i = 1; i <= n; i++) { //
Outer loop (runs 2 times) it is making
rows
for (int j = 1; j <= m ; j++)//
Inner loop (runs 3 times)it is for
printing the star vertically
cout << "* "; // Print star
#include <iostream>
using namespace std;
int main() {
Output: *
* *
* * *
Qs2. Print Numbers in a Triangle
Question: Print numbers in a triangular pattern of 3 rows.
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 3; i++)
{ // Rows
for (int j = 1; j <= i;
j++) { // Numbers
cout << j << " ";
}
cout << endl;
}
return 0;
}
Output: 1
1 2
1 2 3
Qs3. prints a square number pattern, where numbers are printed sequentially in
a 3x3 grid.
#include <iostream>
using namespace std;
int main() {
int n = 3;
int num = 1;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
cout << num << "";
num++;
}
cout << endl;
}
return 0;
}
Output: 1 2 3
4 5 6
7 8 9
Print a Reverse Triangle
Question: Print a reverse triangle of *.
#include <iostream>
using namespace std;
int main() {
for (int i = 3; i >= 1; i--) { //
Rows
for (int j = 1; j <= i; j++)
{ // Columns
cout << "* ";
}
cout << endl;
}
return 0;
}
Output: ***
**
*
Print a Reverse Number Triangle
Question: Print numbers in reverse order in a triangle.
#include <iostream>
using namespace std;
int main() {
#include <iostream>
using namespace std;
int main() {
int n = 4;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i;
j++)
cout << " ";
for (int k = 1; k <= i; k++)
cout << "* ";
cout << endl;
}
return 0;
}
Output:
*
* *
* * *
* * * *
Print an Inverted Pyramid
Question: Print an inverted pyramid of *.
#include <iostream>
using namespace std;
int main() {
int n = 4;
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= n - i;
j++)
cout << " ";
for (int k = 1; k <= i; k++)
cout << "* ";
cout << endl;
}
return 0;
}
Output:
* * * *
* * *
* *
*