You are on page 1of 54

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

 Arrays are data structures consisting of related


data items of the same type. (An array is a
consecutive group of memory locations that all
have the same type.)
 Torefer to a particular location or element in the array,
we specify the name of the array and the position
number of the particular element in the array.

3/18/2024
Declaring and Referencing Array
5

 To declare an array in C++, specify the type of the


elements and the number of elements required by
an array as follows:
 DATATYPE ARRAY_NAME [ ARRAY_SIZE ];
◼ For example, take an integer array 'n'.
◼ int n[6];
◼ n[ ] is used to denote an array named 'n'.

 So,n[6] means that 'n' is an array of 6 integers.


Here, 6 is the size of the array i.e., there are 6
elements of array 'n'.
3/18/2024
6

 We can also declare an array by another method.


 int n[ ] = { 2,3,15,8,48,13 };
◼ Inthis case, we are declaring and assigning values to the
array at the same time. Hence, no need to specify the
array size because the compiler gets it from {
2,3,15,8,48,13 }.
 Following is the pictorial view of the array.

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

 Array names follow the same conventions as other


variable names, i.e., they must be identifiers.
 Just like a variable, an array can be of any other data type
also.
 Array can only hold values of one type
 A subscript must be an integer or integer expression
(using any integral type). If a program uses an
expression as a subscript, then the program evaluates
the expression to determine the subscript.
 The indexes of an array always start with 0 and end
with the integer that is one less than the size of the
array.
 It is illegal to refer to an element outside of the array
bounds. 3/18/2024
Initializing an array
9

 By writing int n[ ]={ 2,4,8 }; , we are initializing the


array.
 But when we declare an array like int n[3]; , we need to
assign the values to it separately. Because 'int n[3];' will
definitely allocate the space of 3 integers in the memory
but there are no integers in that.
 To assign values to the array, assign a value to each of
the element of the array.
 n[0] = 2;
 n[1] = 4;
 n[2] = 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

 DATATYPE MULTID_ARRAY_NAME[ ROW_SIZE ] [ COL_SIZE ];

3/18/2024
11

 Matrix represents a bi-dimensional array of 3 per


5 values of type int .
 The way to declare this array would be:
int matrix [3] [5] ;
 The way to reference the second element vertically and
fourth horizontally would be:
matrix[1][3];

3/18/2024
Example-1
12

 In the array declaration


 double score[5];
 State the following
1. The array name
2. The base type
3. The declared size of the array
4. The range of values that an index for this array can have
5. One of the indexed variables (or elements) of this array

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

 Enter marks of first student


 78

 Enter marks of second student


 92

 Enter marks of third student


 54

 Average marks : 74.6667

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

 Have you ever thought if there is any way to


store dissimilar data?
 The answer is yes. We use structures to store
different types of data.
 For example, you are a student. Your name is a string
and your phone number and roll no are integers.
 So, here name, roll no and phone number are those
different types of data.
 Here, structure comes in the picture.

 A structure is a collection of one or more variable


types grouped together.
3/18/2024
Defining a Structure
19

 The syntax for structure is:


 struct structure_name
{
data-type member-1;
data-type member-2;
data-type member-3;
data-type member-4;
};

3/18/2024
20

 In our case, let's name the structure as student.


 The members of the structure in our case are name,
roll_no and phone_number.
 So, our structure will look like:
 struct student
{
int roll_no;
std::string name;
int phone_number;
};
3/18/2024
Declaration of Structure Variable
21

 Just as we declare variables of type int, char etc,


we can declare variables of a structure as well.
 Suppose we want to store the roll no, name and
phone number of three students.
 For this, we will define a structure named student (as
declared above) and then declare three variables,
say p1, p2 and p3 (which will represent the three
students respectively) of type 'student'.
 This declaration will be done in the main function.

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

 We can also declare structure variables at the time


of defining the structure as follows.
 struct student
{
int roll_no;
std::string name;
int phone_number;
}p1, p2, p3;

3/18/2024
24

 Now, let's see how to enter the details of each


student i.e. roll_no, name and phone number.
 Suppose we want to assign a roll number to the first
student. For that, we need to access the roll number
of the first student. We do this by writing
 p1.roll_no = 1;
 This means that we use dot (.) to use variables in a
structure. p1.roll_no can be understood as roll_no of
p1.

3/18/2024
Example 3:
25

3/18/2024
Array of Structures
26

 In the first example in structures, we stored the data


of 3 students.
 Now suppose we need to store the data of 100
such children.
 Declaring 100 separate variables of the structure is
definitely not a good option.
 For that, we need to create an array of structures.

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

 A function is nothing but a group of codes written


together and given a name.
 And these can be called anytime in the main function
without typing the whole code ( as we know the main
function is executed first ).

3/18/2024
Why Functions?
31

 Functions make our code neat and readable and


prevent us from writing the same code again and
again.
 Types of Functions in C++, there are two types of
functions in C++.
◼ Library Functions
◼ Library functions are pre-defined functions in C++.
◼ User-defined Functions
◼ We can also define our own functions in C++.

3/18/2024
How to Declare a Function?
32

 We declare a function as follows


 return_type function_name ( parameters ) ;
◼ As an example, suppose we have to calculate the average of 2
numbers, num1 and num2.
◼ The numbers are of type integer and average is of type float.
Then we will pass the numbers to a function that will return the
average value of those numbers.
◼ We will declare that function as follows:
 float average( int num1, int num2 );
 Here, the function named 'average' is taking num1 and
num2 of integer type as input and then returning the
average value of type float after calculating it.
3/18/2024
Defining Function
33

 Syntax for defining a function is


 return_type function_name ( parameters )
{
//code
}
 Let's see the average function that we defined above.
 float average( int num1, int num2 )
{
float avg; /* declaring local variable */
avg = ( num1 + num2 )/2.0;
return avg; /* returning the average value */
}

3/18/2024
34

 As seen before, variables declared inside function


are called local variables.
A local variable can only be used in the function in
which it is declared. It has no use outside the function.
For example, in our case, avg is a local variable.
 While defining functions, it is necessary to specify
the parameter type along with the parameters.
 Therefore, we wrote 'int' along with num1 and num2.
 return avg; - This means that this function will give
us or return us avg which is of type float.

3/18/2024
Calling Function
35

 To use a function, we need to call it.


 Once we call a function, it performs its operations and
after that, the control again passes to the main
program.
 To call a function, we need to specify the function
name along with its parameters.
 function_name ( parameters ) ;
 So we will call our average function as shown below
 average( num1, num2 );

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

 We can also define a function at the time of


declaration
 If a function doesn't return anything, then its return
type is written as void.
 'void' means that function will not return anything.
 We can call a function inside another function.

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

 Now coming to the pointer, a pointer points to some


variable, that is, it stores the address of a variable.
 E.g.- if 'a' has an address 0xffff377c, then the pointer to 'a'
will store a value 0xffff377c in it.
 So, if 'b' is pointer to 'a' and the value of 'a' is 10 and
address is 0xffff377c, then 'b' will have a value 0xffff377c
and its address will be different.
 Address in C++ is represented as &a read as address
of a. Remember that all the time when we were taking
the value of 'a' using 'cin', we were taking an input from
the user and storing it at the address of 'a', i.e. in a.

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

 b = &a; - As said, 'b' will store the address of some integer


because it is a pointer to an integer. In this declaration, it is
storing the address of 'a'.
 Since 'b' is a pointer and '&a' represents address, so, by
declaring 'b = &a;' we are storing the address of 'a' in 'b'.
 So, *b is the value of the variable 'b' is pointing to. Here *b
is 44.
 As stated earlier, 'int *b;' means that '*b' is an integer, but '*'
before means that b is a pointer.
 So, '*b' will be the value of the variable to which 'b' is pointing.
Here, 'b' is pointing to 'a' therefore, 'b' will store the address of
'a' and '*b' will be the value of the integer to which 'b' is pointing
i.e. 'a'

3/18/2024
44

 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').

3/18/2024
Pointers in Array
45

 As we all know that pointer is a variable whose value is


the address of some other variable i.e., if a variable y
points to another variable x means that the value of the
variable 'y' is the address of 'x'.
 Similarly, if we say that a variable y points to an array
n, then it means that the value of 'y' is the address of the
first element of the array i.e., n[0]. So, y is the pointer to
the array n.
 Array name is a pointer to the first element of the
array.

3/18/2024
46

 If p is a pointer to the array age, then it means that p(or


age) points to age[0].
 int age[50];
int *p;
p = age;
 The above code assigns the address of the first element of age to
p.
 Now, since p points to the first element of the array age, *p is the
value of the first element of the array.
 So, *p is age[0], *(p+1) is age[1], *(p+2) is age[2].
 Similarly, *age is age[0] ( value at age ), *(age+1) is age[1] (
value at age+1 ), *(age+2) is age[2] ( value at age+2 ) and so
on.

3/18/2024
Pointers in Structure
47

 Like we have pointers to int, char and other data-types, we


also have pointers pointing to structures. These pointers are
called structure pointers.
 Now, how to define a pointer to a structure? The answer is
below:
◼ struct structure_name
{
data-type member-1;
data-type member-1;
data-type member-1;
data-type member-1;
};
int main()
{
struct structure_name *ptr;
}

3/18/2024
Function parameters and arguments
48

 The arguments of function calling:


 Call by value (passing by value) or
 Call by reference (passing by reference)

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

int n1 = 99, n2=11;


int n3 = 22, n4=88; void order(int & num1,int & num2)
order(n1,n2); {
order(n3,n4); if(num1 > num2)
{
cout<< “n1=”<<n1<<endl;
int temp=num1;
cout<< “n2=”<<n2<<endl; num1 = num2;
cout<< “n3=”<<n3<<endl; num2 = temp;
}
cout<< “n4=”<<n4<<endl;
}
return 0;
3/18/2024
}
Example 4:
52

3/18/2024
53

Questions?
3/18/2024
54

Thank You
3/18/2024

You might also like