DSA Chapter 1
DSA Chapter 1
DSA Chapter 1
CHAPTER ONE
REVIEW OF C++
CONCEPTS
3/18/2024 DSA- CH-1: Review of C++ Concepts
CH-1 Contents
2
1. Array
2. Structures
3. Functions
4. Pointers
3/18/2024
CH-1 Contents
3
1. Array
2. Structures
3. Functions
4. Pointers
3/18/2024
Array in C++
4
3/18/2024
Declaring and Referencing Array
5
3/18/2024
7
int n[ ] = { 2,3,15,8,48,13 };
0,1,2,3,4 and 5 are the indices. It is like these are the
identities of 6 different elements of the array. Index
starts from 0. So, the first element has index 0.
We access the elements of an array by
writing array_name[index].
Here,
n[0] is 2
n[1] is 3
n[4] is 48
3/18/2024
Properties of Array
8
3/18/2024
Multidimensional Array
10
It is arrays of arrays.
A bi-dimensional array can be imagined as a bi-
dimensional table of a uniform concrete data type
3/18/2024
11
3/18/2024
Example-1
12
3/18/2024
Output-1
13
1. Score
2. Double
3. 5 (five)
4. 0 to 4
5. Any of score[0], score[1], score[2], score[3],
score[4].
3/18/2024
Example 2 : What is the output?
14
3/18/2024
Output - 2
15
3/18/2024
Exercise 1: What is the output?
16
3/18/2024
CH-1 Contents
17
1. Array
2. Structures
3. Functions
4. Pointers
3/18/2024
Structures in C++
18
3/18/2024
20
3/18/2024
22
struct student
{
int roll_no;
std::string name;
int phone_number;
};
int main()
{
struct student p1, p2, p3;
return 0;
}
3/18/2024
23
3/18/2024
24
3/18/2024
Example 3:
25
3/18/2024
Array of Structures
26
3/18/2024
27
struct student
{
int roll_no;
std::string name;
int phone_number;
};
int main()
{
struct student stud[100];
…
return 0;
}
3/18/2024
Exercise 2: What is the output?
28
3/18/2024
CH-1 Contents
29
1. Array
2. Structures
3. Functions
4. Pointers
3/18/2024
Functions in C++
30
3/18/2024
Why Functions?
31
3/18/2024
How to Declare a Function?
32
3/18/2024
34
3/18/2024
Calling Function
35
3/18/2024
Example 3:
36
3/18/2024
C++ functions generally adhere to
37
the following rules.
Every function must have a name.
They can contain up to 32 characters, they must begin
with a letter, and they can consist of letters, numbers,
and the underscore (_) character.
All function names have one set of parenthesis
immediately following them.
The body of each function, starting immediately
after parenthesis of the function name, must be
enclosed by braces.
3/18/2024
38
3/18/2024
CH-1 Contents
39
1. Array
2. Structures
3. Functions
4. Pointers
3/18/2024
Pointers in C++
40
int a= 44;
As we know, the variable 'a' will take some space and will
store 44 in it.
As we all know that when we declare 'a', it is given a
memory location and the value of 'a' is stored in that
memory location.
In the world of programming, 'a' will also have an
address.
So, this address is the address of that memory location in
which the value of 'a' is stored.
Address of 'a' is something like 0xffff377c. It will vary for
every computer as per memory given to 'a' at that time.
3/18/2024
41
3/18/2024
How to Use Pointers?
42
int a = 44;
int *b; /* declaration of pointer b */
b = &a;
int *b - This statement should mean that '*b' is an
integer but then what is the significance of '*' before
'b'?
◼ Itmeans that b points to some integer ('b' is a pointer to
some integer).
◼ Or we can say that 'b' will store the address of some
integer.
3/18/2024
43
3/18/2024
44
So, in short,
int a; - 'a' is an integer.
int *b; - 'b' is a pointer to an integer.
3/18/2024
Pointers in Array
45
3/18/2024
46
3/18/2024
Pointers in Structure
47
3/18/2024
Function parameters and arguments
48
3/18/2024
Calling by value
49
#.....
void Foo(int num)
{ num = 0;
cout<< “num = ” << num << “
\n”;
} int main()
{ int x = 10;
Foo(x);
cout<< “x = ”<<x<< “\n”;
return 0;
}
3/18/2024
Calling by reference
50
#.....
void Foo(int & num)
{
num = 0;
cout<< “num = ” << num << “ \n”;
} int main()
{
int x = 10;
Foo(x);
cout<< “x = ”<<x<< “\n”;
getch();
return 0;
3/18/2024
}
Example-4
51
#.......
void order(int &, int &);
int main()
{
3/18/2024
53
Questions?
3/18/2024
54
Thank You
3/18/2024