Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
60 views77 pages

DS Week 1 C++ Lecture by DR Gaurav

Uploaded by

titirshaasingh
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)
60 views77 pages

DS Week 1 C++ Lecture by DR Gaurav

Uploaded by

titirshaasingh
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/ 77

Subject: Data Structure

Using C++ (CSET243)

Week 1 Lecture/Lab

Introduction to DS using C++


What, Why and How?

by

Dr Gaurav Kumar
Asst. Prof, Bennett University
Course Structure

3 Hours Lectures 1 Hour Tutorial

6 Hours Lab 7 Credits


Assessment Component

Certification Codeathon Mid Term


(5) (10) (15)

Lab End Term


(30) (40)
Note: Complete 2 Course Certifications, Each Course should be of minimum 18 hours.
DSA Syllabus
Module 1 (Contact hours: 12)
Why Data Structures, First C++ Program, Execution Cycle of C++ Program, OOPs Concepts, C++ Inheritance, Multiple
inheritance, Friend Function, Run-time Polymorphism, Time Complexity: Asymptotic Analysis, Big-Oh Notation, Handling
Arrays, Insertion, Deletion, Traversal, Linear Search, Recursion, Binary Search, Tower of Hanoi, Sorting, Bubble Sort, Selection
Sort, Insertion Sort, Merge Sort.

Module 2 (Contact hours: 10)


C++ Pointers, Structures and Unions, Linked List, Singly Linked List Implementation, Singly Linked List Traversal, Searching,
Insertion, Deletion, Polynomial Handling, Stacks, Infix to Postfix Conversion, Post-fix Expression Evaluation, Queues, Simple
Queue Insertion, Deletion, Traversal, Circular Queue Insertion, Deletion, Traversal.
DSA Syllabus
Module 3 (Contact hours: 12)
Tree Data Structures, Height, Complete, Binary Search Trees, Pre-Order, In-Order, Post- Order, BST Searching, BST Insertion,
BST Deletion, Heaps, Min-Max Heaps, Heapsort, Hashing, Hash Functions, Hash Tables, Hashing Collision Resolution
Strategies: Separate Chaining, Open Addressing, Double Hashing, Graphs, Different Types of Graphs, Graphs Representations,
Incidence Matrix, Adjacency Matrix, Graphs Traversals: BFS, DFS.

Module 4 (Contact hours: 8)


Height Balanced Trees: AVL Trees, Balanced Factor, Rotations, Insertion, B Trees, Insertion, Deletion, Disjoint Sets, Path
Compression, Union Finding Algorithm
Increase your Imagination and Creativity

Note: Reading Book/s and Making dedicated DSA Notes are mandatory for all students.
Increase your Imagination and Creativity

Data Structure- https://in.coursera.org/learn/data-structures

C++ Certifications
C++ Essentials – Part 1 (Basics) Channel: Edube Interactive Mode: Self-Study
Level: Beginner Cost: Free Study Time: 42 hours (Recommended: 7h/week)

C++ Essentials – Part 2 (Intermediate) Channel: Edube Interactive Mode: Self-Study


Level: Intermediate Cost: Free Study Time: 42 hours (Suggested: 7 hours/week)
https://cppinstitute.org/certification
Increase your Imagination and Creativity

Geekforgeeks for practising questions and assignments.

Class Lectures
First Week Topics

C++ Topics for the First Week (10 Hours)

1.Introduction to C++, OOPs Concept


2.Execution Cycle, Variables and Tokens
3.Operators, Conditional Control Statements
4.Functions, Call by Values & Reference, Pointers, Function Pointers
5.Array & Structures
6.Dynamic Memory Allocations
Welcome!

Meet Your
Subject Mentor
Dr Gaurav Kumar
Asst. Professor, SCSET
NSS Program Coordinator, Bennett University
About
Dr Gaurav
3.8 years of research and
Social Entrepreneurship
Experience,
(7.5+ Years of
Volunteering Experience)
About Dr Gaurav

Grew up in Completed Master and PhD in


Muzaffarpur City, Bihar Computer Sciences from JNU
GOLD MEDAL
Community Service (2017)
Achievements
BEST VOLUNTEER AWARD
Year 2017, MHRD, Govt of India

PUBLISHED 9 CONFERENCES
& JOURNALS

42 TRAINING SESSIONS
Computer & Cyber Security
CHAIR OF ACM & IEEE CHAPTER
Year 2013 and 2017
75 + WORKSHOPS AND SEMINARS
ORGANISED
UGC NET AND CSIR JRF
Year 2013
Fun Facts about Dr Gaurav
www.collcom.org

Food Startup Social Entreprenurship


Established Year 2018
Year 2019, 6 Months Ongoing
Dr Gaurav
Social Service

Community Service
means a lot
Cyber Sanskar
Talks in Schools
or Colleges
Social Engineering Research Activities

Online Cyber Training


Mega Campaign
Love to be Fit
and Healthy
RYM Fitness Digital
Detoxification Campaign
Hobbies

Community Service,
Cooking Badminton
Teaching & Training
Thank You!
MOBILE EMAIL ADDRESS
+91-8586968801 [email protected]

Office- M Cub 311 QUERIES & DISCUSSION


09 A.M. to 5:30 P.M. 10 A.M. to 8:30 P.M.

/gauravjnu /gauravsp1988 /gauravjnu


Data Structure Lab

Concept: Printing the string


1. Write a C++ program to print a string
Sample Test Case
Sample Output:
This is my first C++ Program
Data Structure Lab Review
// - Comment is used for reading the code to better understand the
// Printing the string functionality of the program. (Ignored by the C++ compiler)

#include <iostream> The #include is a preprocessor directive used to include files for “C out”

Short form of standard, the std namespace contains the built-in classes
using namespace std; and declared functions, bring scope of identifiers into current scope.

int main() Execution begins from main function

{
cout << "This is my first C++ Program";
return 0; It indicates the exit status of the program

}
Data Structure Lab

Concept: Printing the string


2. Write a C++ program to print a string without using ‘namespace std’
Sample Test Case
Sample Output:
This is my first C++ Program
Data Structure Lab Review

// Printing the string


#include <iostream>
int main()
{
std::cout << "This is my first C++ Program";
return 0;
}
Assessment Time
10
9
8
7
6
5
4
3
2
1
#include <iostream>
int main() {
cout << "This is my first C++ Program";
return 0; }

Which one is the a) Print the Output without any warning

correct output of b) Print the Output with some warning

c) Compilation Error
the program?
d) I am confused
Correct Answer is c
Assessment Time
10
9
8
7
6
5
4
3
2
1
#include <iostream>
int main() {
std::cout << "This is my first C++ Program";
return -11; }

Which one is the a) Print the Output without any warning


correct output of b) Print the Output with some warning
the program? c) Compilation Error

Correct Answer is a d) I am confused


Assessment Time
10
9
8
7
6
5
4
3
2
1
#include <iostream>
int main() {
printf ("This is my first C++ Program”);
return 0; }

Which one is the a) Print the Output without any warning


correct output of b) Print the Output with some warning
the program? c) Compilation Error

Correct Answer is a d) I am confused


Assessment Time
10
9
8
7
6
5
4
3
2
1
#include <iostream>
using namespace std;
int display() {
cout<< "This is my first C++ Program”;
return 0; }
Which one is the a) Print the Output

correct output of b) Will not Print any output

c) Compilation Error
the program?
d) I am confused

Correct Answer is c
Data Structure Lab

Concept: Variable Types


3. Write a C++ program to print the size of int, char, float and double type
variables
Sample Test Case
Sample Output: 4 1 4 8
Data Structure Lab

Concept: Control Statements


4. Write a C++ program to find the minimum price among three options for a product. Input
prices using user-defined input to discover the most affordable choice, helping you make the
best decision while shopping.
Sample Test Case
Sample Input:
Enter the price of Smartphone 1: $600
Enter the price of Smartphone 2: $550
Enter the price of Smartphone 3: $580
Sample Output: The best deal in Smartphone is $550.
Data Structure Lab
5. Write a C++ program where you take a sentence as an input from the user and
output each word of a sentence in a separate line
Sample Test Case
Sample Input: This is a program to get the idea of control statements
Sample Output:
This
is
a
program
to
get
the
idea
of
control
statements
Data Structure Lab

6. Write a C++ program to find a sum of first n natural numbers (where n is defined by user)
Sample Test Case
Sample Input: Enter a positive number:5
Sample Output: Sum = 15 Explanation Sum of first 5 natural numbers is 1+2+3+4+5

7. Understand how computers store and process data in binary format for efficient computation and
data storage. Write a C++ program that converts a decimal number to its binary representation
using loops.
Sample Test Case
Enter a decimal number: 13
Binary representation: 1101
Data Structure Lab
8. Implement a C++ program to calculate Investment Growth with Compound Interest: You want to invest a certain
amount ('a') in a long-term account with a fixed interest rate ('r'). Write a program to calculate the investment's value
over 'n' years and print the growth at each interval. This will help you plan your financial future wisely.

Sample Test Case


Enter the starting investment (a): 5000
Enter the common ratio (r): 1.1
Enter the number of years (n): 5

Investment Growth Over Time (GP with common ratio 1.1):


Year 1: $5000
Year 2: $5500
Year 3: $6050
Year 4: $6655
Year 5: $7320.5
Assignment Data Structure Lab

9. Implement a C++ program to find the greatest common divisor (GCD) of two given positive integers 'a' and 'b'
using the Euclidean algorithm with a loop.

Sample Test Case


Enter two positive integers 'a' and 'b': 24 18
GCD of 24 and 18 is: 6

10. Create a C++ program for a personal health tracker. Input weight and height to calculate your Body Mass Index
(BMI).

Sample Test Case


Enter your weight in kilograms: 70
Enter your height in meters: 1.645

You are overweight.


Kindly submit all programs on LMS latest by the end of this week.
DS using C++

Data Structure C++


Programming
Language
C++ and OOPs Concept

C++ Topics for the First Week (10 Hours)

• Introduction to C++, OOPs Concept Execution Cycle,

Variables and Tokens Operators,

• Conditional Control Statements Functions, Call by Values &

Reference,

• Pointers, Function Pointers Array & Structures

• Dynamic Memory Allocations


About C++

Founder of C++ : Bjarne Stroustrup


Professor of Computer Science in Columbia University in
New York City

Founded C++, Early 1980s at AT&T Bell Laboratories in


Murray Hill, New Jersey, USA

Birth : 30 December 1950 (From Denmark)


My First Program
// Printing the string // - Comment is used for reading the code to better understand the
functionality of the program. (Ignored by the C++ compiler)
#include <iostream> The #include is a preprocessor directive used to include files for “C out”

using namespace std; Short form of standard, the std namespace contains the built-in classes
and declared functions, bring scope of identifiers into current scope.
int main() Execution begins from main function
{
cout << "This is my first C++ Program";
return 0;
It indicates the exit status of the program
}
Brain Storming

Kaun Banega Choclatepati


KBC Assessment Time
10
9
8
7
6
5
4
3
2
1
#include <iostream>
int main() {
cout << "This is my first C++ Program";
return 0; }

Which one is the a) Print the Output without any warning

correct output of b) Print the Output with some warning

c) Compilation Error
the program?
d) I am confused
Correct Answer is c
KBC Assessment Time
10
9
8
7
6
5
4
3
2
1
#include <iostream>
int main() {
std::cout << "This is my first C++ Program";
return -11; }

Which one is the a) Print the Output without any warning


correct output of b) Print the Output with some warning
the program? c) Compilation Error

Correct Answer is a d) I am confused


Important Concept

Specified Working Directory Current Working Directory

#include <iostream> #include “iostream”

#include <Header File> #include “User Defined File”


KBC Assessment Time
10
9
8
7
6
5
4
3
2
1
#include “iostream”
int main() {
printf ("This is my first C++ Program”);
return 0; }

Which one is the a) Print the Output without any warning


correct output of b) Print the Output with some warning
the program? c) Compilation Error

Correct Answer is a d) I am confused


Execution Cycle of C++ Program

#include <iostream>
#define MAX 5 //macro
int main()
{
for (int i = 0; i < MAX; i++)
{
std::cout << i << "\n";
}
return 0;
}
Execution Cycle of C++ Program
KBC Assessment Time
10
9
8
7
6
5
4
3
2
1
#include <iostream>
using namespace std;
int display() {
cout<< "This is my first C++ Program”;
return 0; }
Which one is the a) Print the Output

correct output of b) Compile the Program Successfully

c) Execution Error
the program?
d) I am confused
Correct Answer is b, c
(Please understand the concept of Compilation and Run time Execution of Program.)
Why Object-Oriented Programing
Why Object-Oriented Programing

Self Study Topics


Procedural Oriented Programming,
Applications, Drawback, why there is a
need of OOPS and Its applications
Why Object-Oriented Programing

Spot the Differences

Pen Down the differences between them


Why Object-Oriented Programing
Spot the Differences

Color Size Age Breed

All dogs share these common characteristics


Why Object-Oriented Programing

Spot the Differences

Eat Sleep Sit Run

All dogs share these common actions (Methods)


Why Object-Oriented Programing

Spot the Differences


Why Object-Oriented Programing
Why Object-Oriented Programing
#include <iostream>
using namespace std; int main()
class Dog {
{ Dog dog1; // Declare an object of class Dog
public: // Access specifier
string breed; // Data Members // accessing data member
string size;
int age; dog1.breed = “Neopolitan_Mastiff";
string color; dog1.size=“Large”;
dog1.age=5;
void printname() // Member Functions() dog1.color=“black”
{ dog1.printname(); // accessing member function
cout << “Breed is: " << breed;
cout << “Size is: " << size;
return 0;
cout << “Age is: " << age; }
cout << “Color is: " << color;
}
};
O/p- Breed is: Neopolitan_Mastiff || Size is: Large || Age is: 5 || Color is: black
Understanding Class and Objects

A class is the blueprint of an object.

It is a user-defined data structure that has


data and functions as its members.

An object is an instance of a class.

When a class is created no memory is allocated but when an instance is


created by declaring an object, memory is then allocated to store data and
perform required operations on them.

Note: This statement is partly true, please research and understand the actual concept behind this.
Characteristics of OOPs
Characteristics of OOPs
Encapsulation

Wrapping data and methods in a single unit to prevents


un-authorized access from outside class.

Using access specifiers to stops data from being accessed from the outside world
• Public – Data members & member functions can be accessed from outside the class.
• Private – Data members & member functions can only be accessed from within the class.
• Protected – Data members & member functions can be accessed by the class and its derived classes.
Characteristics of OOPs
Abstraction

Hiding lower-level details and exposing only the essential and


relevant details to the users.

Abstraction using Classes - A Class can decide using available access specifiers which data member will be
visible to the outside world and which is not.

Abstraction in Header Files – Usage of pow() method present in cmath header file.
Characteristics of OOPs
Polymorphism

“ +” operator in C++ The same entity (function or object) behaves differently in different
scenarios.
Used in numbers to performs addition

Used in the string to performs concatenation.

A man can be a father to someone, a husband, a boss, an employee, a


son, a brother, or can have many other relationships with various people.

This man represents the object, and his relationships display the ability
of this object to be represented in many forms with totally different
characteristics.
Characteristics of OOPs
Inheritance (Reusability)
Inheritance is a process of obtaining the data members and methods
from one class to another class, plus can have its own is known as
inheritance.
Working of Inheritance
#include <iostream> Inheritance (Reusability)
using namespace std;
class Employee
{
public:
float salary = 60000;
};

class Programmer: public Employee


{
public:
float bonus = 5000;
};

int main(void) {
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}
#include <iostream> Inheritance (Reusability)
using namespace std;
class Employee
{
public:
float salary = 60000;
};

class Programmer: public Employee


{
public:
float bonus = 5000;
};

int main(void) {
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}
Structure

• Structure is another user defined data type that allows to


combine data items of different kinds.

• Structures are used to represent a record. Suppose you


want to keep track of your books in a library.
Title
Author
• You might want to track the following attributes about each
Subject
book
Book ID
Structure
Each variable in the structure is known as a member of the structure.

struct [structure tag] struct Books {


{ char title[50];
char author[50];
member definition;
char subject[100]; Title
member definition;
int book_id; Author
...
Subject
member definition; };
Book ID
} [one or more structure variables]; struct Books Book1;
struct Books Book2;
Accessing Structure Variables

struct Books { struct Books book1, book2; //another way


char title[50]; Book1.title= “Data Structure";
char author[50]; Book1.author=“Yashwant Kanetkar";
char subject[100];
Book1.subject= “DS Tutorial";
int book_id;
Book1.book_id = 6495407;
}book1, book2;

cout<< "Book 1 title : %s\n", Book1.title”;


cout<< “Book 1 author : %s\n", Book1.author”;
Structure as a Function Argument
struct Books { /* function declaration */
char title[50]; void printBook( struct Books book );
char author[50];
char subject[100];
int book_id; /* function definition */
}; void printBook( struct Books book)
{
cout<<"Book 1 title : %s\n", book.title;
cout<<"Book 1 author : %s\n", book.author;
}
Pointer
A pointer is a variable whose value is the address of another variable, i.e.,
direct address of the memory location.

int var1 = 100;


int *ptr ; //pointer to an integer
ptr= &var1;
Cout<<*ptr;

* is indirection operator or dereferencing operator


Extended Pointer (Double Pointer)
int a, *b, **c; a
a=10;
10 b
b=&a;
c=&b; 2000 2000
c
cout<<"The value of a is : “<< a; 4000
4000
cout<<"The value of b is : “<< *b ;
7000
cout<<"The value of c is : “<< **c;

Output: 10, 10, 10


Assessment Time
int a, *b, **c; a
a=10;
**c 20
10
b=&a; b
c=&b; 2000
**c=20; 2000
cout<<"The value of a is : “<< a;
c
4000
Output: 4000
(A) 10
(B) 20 7000
(C) 30
(D) Error

Correct Answer is B
Assessment Time
int a, *b, **c; a
a=10;
*b **c 30
10
b=&a; b
c=&b; 2000
**c=a + 2*(*b); 2000
cout<<"The value of a is : “<< a;
c
4000
Output: 4000
(A) 10
(B) 20 7000
(C) 30
(D) Error

Correct Answer is C
Returning a Pointer (Assessment Time)

int* fun() main() p


A
{ { Address
10 of A ??
int A = 10; int* p;
5000 2000
return (&A); p = fun();
} }
Address
of A
Output ??
Note: Scope/Life of a Variable is
limited to the Local Function only Segmentation Error
Returning a Pointer

int* fun() main()


{
{
int* p;
static int A = 10;
p = fun();
return (&A);
}
}
Pointer - Homework

Explore the different


operations on Pointer
Pointers to Structures
struct Books { struct Books *struct_pointer;
char title[50];
char author[50]; struct_pointer = &Book1;
char subject[100];
int book_id;
}book1, book2;

To access the members of a structure using a pointer to that structure, you must use
the → operator as follows

struct_pointer->title;
In Normal Structure
cout<< "Book title :“ << book->title; book.title
Dynamic Memory Allocation

Self Study

Note: More Details will be cover in the DSA Lab


Readings for the Next Week
• DS, Types, Algorithms and Program

• Time Complexity

• Asymptotic Analysis

• Arrays

Recursions

• Searching Strategy
Any Queries?
Office: MCub311
Email: [email protected]

You might also like