Sei sulla pagina 1di 11

Array

Introduction to Array (1-D Array)


2-D Array
Sorting

1 Bilal A. Khan-How to Program 12/07/21


Introduction to 1-D Array

2 Bilal A. Khan-How to Program 12/07/21


Introduction to Array (1-D Array)
• A kind of a variable
• More than values can be stored in one variable at different elements
• Three parts
– Datatype
– Variable name
– Number of elements given in square brackets

Defining 1-D Array


elements Five Elements
int number [ ]; int number [5];

variable name Position


datatype

int number [5]; 0 1 2 3 4

3 Bilal A. Khan-How to Program 12/07/21


Introduction to Array (1-D Array)

int number [5] = {34 , 42 , 101 , 4 , 13};

0 1 2 3 4

Position
34 42 101 4 13

Values at respective positions

4 Bilal A. Khan-How to Program 12/07/21


Introduction to Array (1-D Array)

int number [5] = {34 , 42 , 101 , 4 , 13};

number[0] number[1] number[2] number[3] number[4]


34 42 101 4 13

5 Bilal A. Khan-How to Program 12/07/21


2-D Array

6 Bilal A. Khan-How to Program 12/07/21


2-D Array
• Array has two sets of elements
• The first set forms the rows
• The second forms the columns

Defining 2-D Array


int number [ ] [ ]; int number [5] [5];

Rows Columns
Rows
int number [5] [5]; 0,0 0,1 0,2 0,3 0,4
1,0 1,1 1,2 1,3 1,4
2,0 2,1 2,2 2,3 2,4
3,0 3,1 3,2 3,3 3,4
7 4,0 Bilal A.4,1 4,2
Khan-How to Program 4,3 4,4 12/07/21
Sorting

8 Bilal A. Khan-How to Program 12/07/21


Sorting
• Rearranging the data stored in an array
• Different kinds of Sorting Techniques
Understanding Bubble Sort

int number [5] = {34 , 42 , 101 , 4 , 13};

Ascending Order 4 13 34 42 101

Descending Order 101 42 34 13 4

9 Bilal A. Khan-How to Program 12/07/21


Sorting
• Sorting needs two major operations
– Comparison
– Swapping of value
• Storing an old value in some other variable and new value in
current variable in order to avoid overwriting of values
Basic Operation

int number [5] = {34 , 42 , 101 , 4 , 13};

:Step 3
:Step 14
2 34
4 42
34
13
4 101
42
13
34
4 101
13
42
4 101
13

10 Bilal A. Khan-How to Program 12/07/21


Thank You

11 Bilal A. Khan-How to Program 12/07/21

Potrebbero piacerti anche