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

C++ with DSA

This document provides an overview of a C++ programming course covering topics such as flowcharts, pseudocode, data types, operators, control structures, and data structures. It includes examples of C++ program structure, input/output operations, and various algorithms. The course also delves into advanced data structures and algorithms, including heaps, hashmaps, and dynamic programming.

Uploaded by

Pranav
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

C++ with DSA

This document provides an overview of a C++ programming course covering topics such as flowcharts, pseudocode, data types, operators, control structures, and data structures. It includes examples of C++ program structure, input/output operations, and various algorithms. The course also delves into advanced data structures and algorithms, including heaps, hashmaps, and dynamic programming.

Uploaded by

Pranav
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/ 38

Over View of this course:

Programming with C++


Flowcharts & Pseudocode
Variables & Data Types
Operators
Conditional Statements
Loops (Flow Control)
Patterns Functions & Scope

Data Structures & Algorithms


Arrays & 2D Arrays
Sorting Algorithms
Strings
Pointers & Dynamic Allocation
Standard Template Library (STL)
Time & Space Complexity
Recursion & Backtracking
Divide & Conquer
Object Oriented Programming (OOPs)
Linked Lists
Stacks & Queues
Binary Trees
Binary Search Trees

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

Ques2. Write a flowchart and pseudocode to find the area of a square.


Pseudocode
1. Start
2. Input length of the square
3. Calculate area of square as length * length
4. Print area
5. End

Variables:

Variable is the name of a memory


location which stores some data.
Rules:
a. Variables are case sensitive

b. 1st character is alphabet or '_'

c. no comma/blank space

d. No other symbol other than '_'

Data Types:
Data Type Meaning Size (in Bytes)

int Integer 4

float Floating-point 4

double Double Floating-point 8

char Character 1

bool Boolean 1

void Empty 0

C++ Program Structure:


#include <iostream>
using namespace std;
int main(){
cout<<"hello world";
return 0;
}

Compilation:
A computer program that translates C code into machine code

How to check code successfully compiled?


g++ -o file name file name.cpp

Comments:

Lines that are not part of program


Single Line: //
Multiple Line: /*hello world*/
#include <iostream>
//this code displays how data types work in C+
/*
This is a multi-line comment
*/
using namespace std;
int main(){
cout<<"hello world";
return 0;
}

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

3. Using namespace std


All elements of the standard C++ library are declared within namespace.
Here we are using std namespace.

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.

Input and Output in C++

The header file iostream must be included to make use of the input/output
(cin/cout) operators.

Standard Output (cout)


By default, the standard output of a program points at the screen. So with the
cout operator and the “insertion” operator (<<) you can print a message onto
the screen.

#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>

using namespace std;

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;
}

It is possible to combine variables and text:

#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 ).

cout << "This is one sentence.\n";


cout << "This is another.\n";
It is possible to use the endl manipulator instead of the new-line character.

cout << "This is one sentence." << endl;


cout << "This is another." << endl;

Standard input (cin)


In most cases the standard input device is the keyboard. With the cin and >> operators it
is possible to read input from the keyboard.

Take a look at an example:


#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter the age" ;
cin >> age;
cout << "you age is :" << age << endl;
return 0;
}
Homework questions?
1.
#include<iostream>
using namespace std;

int main(){

cout<< " *\n";


cout<< " ***\n";
cout<< " *****\n";
cout<< " *******\n";
cout<< "*********\n";

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

+, -, *, /, % are binary operators.


++, -- are unary operators.

Suppose: A=5 and B=10

Operator Operation Example

+ Adds two operands A+B = 15

- Subtracts right operand from left operand B-A = 5

* Multiplies two operands A*B = 50

/ Divides left operand by right operand B/A = 2

% Finds the remainder after integer division B%A = 0


Understanding from the code:

#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;
}

Let’s see the case of division:

Depends upon data type -

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() {

cout << (5/ (double)2) << endl; here we using type


casting
return 0;
}
2. Relational Operators:
Relational operators define the relation between 2 entities. They give a Boolean value as
result i.e., true or false.

Operat Operation Example


or
== Gives true if two operands are equal A==B is not
true

!= Gives true if two operands are not equal A!=B is true

> 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() {

cout << (5<8)<<"\n"; //True = 1


cout << (5>8)<<"\n";//False = 0
cout << (5>=8)<<"\n";//False = 0
cout << (5<=8)<<"\n";//True = 1
cout << (5!=8)<<"\n";//True = 1
cout << (8==8)<<"\n";//True = 1
return 0;
}
3. Logical Operators:
Logical operators are used to connect multiple expressions or conditions together.
We have 3 basic logical operators.

Suppose: A=0 and B=1

Operat Operation Example


or
&& AND operator. Gives true if both operands (A && B) is
are non- zero false

|| OR operator. Gives true if atleast one of the (A || B) is


two operands are non-zero. true

! NOT operator. Reverse the logical state of !A is true


operand

#include <iostream>
using namespace std;
int main() {

cout << !(5<8) << "\n"; //True = 1 but here we us !


operator so Ans is reverse
cout << ((5<8) || (5>8)) << "\n"; //at least one
statement is true then the Ans is True = 1
cout << ((5<8) && (5>8)) << "\n";// one is wrong
cout << ((5<8) && (5<8)) << "\n"; // both are
correct
return 0;
}
4. Bitwise operator: Bitwise operators in C++ are used to perform operations directly on
binary numbers (bits). They work at the bit level of data.
Qs. Sum of two number by using cout & cin?

#include <iostream>
using namespace std;
int main() {
int a,b;
cout << "Enter a number "<< "a =" << "\n";
cin >> a;

cout << "Enter a number "<< "b =" << "\n";

cin >> b;

int sum = a+b;


cout << "sum = "<<sum<<"\n";
return 0;
}

5. Unary operators / Binary / Ternary


#include <iostream>
using namespace std;
int main() {
int a= 10;
int b=a++;// First: executing then updating it is
called post increment
cout<< "a = "<<a << "\n";
cout<< "b = "<<b << "\n";
return 0;
}
Output: a = 11
b = 10

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>

using namespace std;

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>

using namespace std;

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

Infinite Loop case:

#include<iostream>
using namespace std;

int main(){
int n = 10;
int count = 1;

while (count<=n){
cout<<count<< " " ; 1111111……nth
;
}
}
for loop:

The syntax of the for loop is:

for (initialization; condition; update) {


// body of-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;

for (int i=1;i<=n;i++){


cout <<i<<" ";
}

}
QS. Print odd number from 1 to 10.
#include <iostream>
using namespace std;

int main () {
int n = 10;

for (int i=1;i<=n;i= i+2){


cout <<i<<" ";
}

} 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!

From While loop


#include <iostream>
using namespace std ;
int main () {
while (3>5)
{
cout<<"hello world!\n";
}

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.

Square star pattern

#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

cout << endl; // Move to the next


line
}
return 0;
} Output: * * *
* * *
* * *
Pattern Questions:

Qs1. Print a Right-Angled Triangle


Question: Print a triangle of stars (*) with 3 rows.

#include <iostream>
using namespace std;

int main() {

for (int i = 1; i <= 3; i++) { //


Rows
for (int j = 1; j <= i; j++) { //
Columns
cout << "* ";
}
cout << endl;
}
return 0;
}

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.

✅ "Sequential Number Square Pattern"


✅ "Matrix-like Number Pattern"
✅ "Number Grid Pattern"

#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() {

for (int i = 4; i >= 1; i--) {


for (int j = 1; j <= i; j++) {
cout << j << " ";
}

cout << endl;


}
return 0;
}
Output:1 2 3 4
1 2 3
1 2
1
Print a Pyramid
Question: Print a pyramid of *.

#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:
* * * *
* * *
* *
*

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy