DSA Chapter 1
DSA Chapter 1
CHAPTER ONE
REVIEW OF C++
CONCEPTS
04/29/2024 DSA- CH-1: Review of C++ Concepts
CH-1 Contents
2
1. Array
2. Structures
3. Functions
4. Pointers
04/29/2024
CH-1 Contents
3
1. Array
2. Structures
3. Functions
4. Pointers
04/29/2024
Array in C++
4
04/29/2024
Declaring and Referencing Array
5
04/29/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
It is arrays of arrays.
A bi-dimensional array can be imagined as a bi-
dimensional table of a uniform concrete data type
04/29/2024
11
04/29/2024
Example-1
12
04/29/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].
04/29/2024
Example 2 : What is the output?
14
04/29/2024
Output - 2
15
04/29/2024
Exercise 1: What is the output?
16
04/29/2024
CH-1 Contents
17
1. Array
2. Structures
3. Functions
4. Pointers
04/29/2024
Structures in C++
18
04/29/2024
20
04/29/2024
22
struct student
{
int roll_no;
std::string name;
int phone_number;
};
int main()
{
struct student p1, p2, p3;
return 0;
}
04/29/2024
23
04/29/2024
24
04/29/2024
Example 3:
25
04/29/2024
Array of Structures
26
04/29/2024
27
struct student
{
int roll_no;
std::string name;
int phone_number;
};
int main()
{
struct student stud[100];
…
return 0;
}
04/29/2024
Exercise 2: What is the output?
28
04/29/2024
CH-1 Contents
29
1. Array
2. Structures
3. Functions
4. Pointers
04/29/2024
Functions in C++
30
04/29/2024
Why Functions?
31
04/29/2024
How to Declare a Function?
32
04/29/2024
C++ functions generally adhere to the following
rules.
37
04/29/2024
38
04/29/2024
CH-1 Contents
39
1. Array
2. Structures
3. Functions
4. Pointers
04/29/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.
04/29/2024
41
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'?
It means 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.
04/29/2024
43
So, in short,
int a; - 'a' is an integer.
int *b; - 'b' is a pointer to an integer.
b = &a; - 'b' is now pointing to 'a'(value of 'b' is the
address of 'a').
'*b' will now represent a (value of '*b' is the value of
'a').
04/29/2024
Pointers in Array
45
04/29/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;
} 04/29/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;
04/29/2024
}
Example-4
51
#.......
void order(int &, int &);
int main()
{
04/29/2024
53
Questions?
04/29/2024
54
Thank You
04/29/2024