0% found this document useful (0 votes)
4 views

Lesson-18, 19, 20

Uploaded by

creativeboy8110
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lesson-18, 19, 20

Uploaded by

creativeboy8110
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Arrays

Dept. of Computer Science & Engineering

1
Lesson – 18

Topic Lesson Learning Outcome Teaching Assessment


(at the end of the lesson students will be Learning Method
able to…) Methodology
1-D Array • Explain the concept of array Multimedia Class Test,
(B1: Ch-7, • Declare and initialize one dimensional Presentation , Exam,
B2: Ch-5) array Question and etc.
• Differentiate between runtime and Answer
compile time initialization of arrays

2 Department of CSE, CUET


Introduction

⮚ An array is a fixed-size sequenced collection of related


data items that share a common name.
⮚ For example: salaries of a group of employees in an
organization.
⮚ int salary[10]; //declaration of salary of 10 employee
⮚ salary[0] //represents the salary of 1st employee
⮚ Complete set of values- array
⮚ Individual values- elements

3 Department of CSE, CUET


Types of Array

⮚ One dimensional arrays


⮚ A list of items can be given one variable name using only one
subscript and such a variable is called a one dimensional array.

⮚ Two dimensional arrays


⮚ Situations where a table of values will have to be stored.

⮚ Multidimensional arrays
⮚ Situations where more than one table is required.

4 Department of CSE, CUET


One-Dimensional Arrays

⮚ The elements, the values of the items, of a one-


dimensional array are conceptually arranged one
after another in a single row.
⮚ int a[n];

a[0] a[1] a[n-1]

⮚ Array elements are always numbered starting from


0, so the elements of an array of length n are indexed
from 0 to n-1

5 Department of CSE, CUET


Declaration of Arrays

type variable-name [size];


⮚ type specifies the type of element that will be
contained in the array such as int, float, char
⮚Variable name must follow the rules of declaring
variable that we have learned before
⮚Size indicates the maximum number of elements
that can be stored in the array.
float height [50];
int group [10];
char name [20];

6 Department of CSE, CUET


Initialization of Arrays

⮚ After declaration the elements of an array must be


initialized.

⮚ If an array is not properly initialized then it will


contain garbage values.

⮚ An array can be initialized at,


⮚ Compile time
⮚ Run time

7 Department of CSE, CUET


Compile Time Initialization

type array-name [size] ={list of values}

int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int a[10] = {1 , 2, 3, 4, 5, 6};


/* {1, 2, 3, 4, 5, 0, 0, 0, 0, 0} */

int a[10] = {0};


/* {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} */

int a[ ] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

8 Department of CSE, CUET


Run Time Initialization

Arrays can also be explicitly initialized at runtime.

9 Department of CSE, CUET


Indexing

⮚ C doesn’t require that subscript bounds be checked. If


a subscript goes out of range, the program’s behavior
is undefined.

int a[10]={0};
print(“%d”, a[10])

⮚ Array subscript may be any integer expression,

a[i+j*10];
a[i] = b[i++];

10 Department of CSE, CUET


Lesson – 19

Topic Lesson Learning Outcome Teaching Assessment


(at the end of the lesson students will be Learning Method
able to…) Methodology
Searching • Apply searching and sorting techniques in Multimedia Class Test,
and sorting array Presentation , Exam,
of array • Know about the complexity of searching Question and etc.
(B1: Ch-7, and sorting Answer
B2: Ch-5)

11 Department of CSE, CUET


Searching

⮚ Searching is the process of finding the location of the


specific element in an array.

⮚ The specified element is called the search key

⮚ Commonly used search techniques,

⮚ Sequential/linear search
⮚ Binary search

12 Department of CSE, CUET


Linear Search
Write a program to search an element in array,

13 Department of CSE, CUET


Complexity

⮚ Best case complexity: O (1)


⮚ searched element is the first element of the array

⮚ Average case complexity: O (n)

⮚ Worst case complexity: O (n)


⮚ searched element is the last element of the array

14 Department of CSE, CUET


Related problems

⮚ Write a C program to find maximum and minimum


element in an array.

⮚ Write a C program to find second largest element in


an array.

⮚ Write a C program to count total number of even and


odd elements in an array.

⮚ Write a C program to count total number of negative


elements in an array.

15 Department of CSE, CUET


Sorting

⮚ Sorting is the process of arranging elements in the


array according to their values.

⮚ An array can be sorted either in ascending or


descending order.

⮚ Commonly used sorting techniques,


⮚ Bubble sort
⮚ Selection sort
⮚ Insertion sort

16 Department of CSE, CUET


Bubble Sort
Bubble sort can be implemented by following steps,
1.Start at index zero,
a. compare the element with the next one (a[0] & a[1]), and
swap if a[0] > a[1].
b. Now compare a[1] & a[2] and swap if a[1] > a[2].
c. Repeat this process until the end of the array. After doing
this, the largest element is present at the end.
d. This whole thing is known as a pass. In the first pass, we
process array elements from [0,n-1].
2.Repeat step one but process array elements [0, n-2] because the
last one, i.e., a[n-1], is present at its correct position. After this
step, the largest two elements are present at the end.
3.Repeat this process n-1 times.

17 Department of CSE, CUET


Bubble Sort

18 Department of CSE, CUET


Bubble Sort

19 Department of CSE, CUET


Lesson – 20

Topic Lesson Learning Outcome Teaching Assessment


(at the end of the lesson students will be Learning Method
able to…) Methodology

Multidimensi • Declare and initialize 2-D arrays Multimedia Class Test,


onal array • Perform matrix multiplication Presentation , Exam,
(B1: Ch-7, • Solve problems using multidimensional Question and etc.
B2: Ch-5) arrays Answer

20 Department of CSE, CUET


Two-dimensional Arrays

⮚ A two-dimensional array consists of both rows and


columns of elements. It is essentially a matrix.

⮚ To declare a two-dimensional array, we merely use


two sets of square brackets. The first contains the
number of rows. The second contains the number of
columns.

//Creates a 2D array with 3 rows and 4 columns


int vals[3][4];

21 Department of CSE, CUET


Two-dimensional Arrays

⮚ Tables with rows and columns (m by n array)


⮚ Like matrices: specify row, then column

Col 0 Col 1 Col 2 Col 3

Row 0 ara[0][0] ara[0][1] ara[0][2] ara[0][3]

Row 1 ara[1][0] ara[1][1] ara[1][2] ara[0][3]


ara[2][0] ara[2][1] ara[2][2] ara[0][3]
Row 2

22 Department of CSE, CUET


Initializing 2D arrays

⮚ You can use additional braces to indicate when rows start and
end, but you don’t have to do that.

⮚ int val[3][4] = { {8,16,9,52}, 8 16 9 52

{3,15,27,6}, 3 15 27 6

{14,25,2,10} 14 25 2 10

};

⮚ int val[3][4] = {8,16,9,52,3,15,27,6,14,25,2,10};

23 Department of CSE, CUET


Initializing 2D arrays

⮚ Initialization

• int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; 1 2

• Initializers grouped by row in braces 3 4

• If not enough, unspecified elements set to zero 1 0


• int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; 3 4

⮚ Referencing elements
• Specify row, then column
• printf( "%d", b[ 0 ][ 1 ] );

24 Department of CSE, CUET


Using 2D arrays

⮚ Just like 1D arrays, once you have specified the index,


you are just working with a single variable of the given
data type.

⮚ 2D arrays work well with (for) loops like 1D arrays.


However, to access all elements, typically you will
need nested loops for 2D arrays.

⮚ Can you see why?

25 Department of CSE, CUET


Example (1)
...
int main()
{
int NUM_ROW=3;
int NUM_COL=4;
int row,col;
int vals[NUM_ROW][NUM_COL] = { {11, 12, 13, 14},
{21, 22, 23, 24},
{31, 32, 33, 34} };
// output the transpose of the array
for (row = 0; row < NUM_ROW; row++)
{
for (col = 0; col < NUM_COL; col++)
{
printf(“%d”,vals[row][col]);
}
printf(“\n”);
}
...

26 Department of CSE, CUET


Example (2)
...
int main()
{
int NUM_ROW=3;
int NUM_COL=4;
int row,col;
int vals[NUM_ROW][NUM_COL] = { {11, 12, 13, 14},
{21, 22, 23, 24},
{31, 32, 33, 34} };
// output the transpose of the array
for (col = 0; col < NUM_COL; col++)
{
for (row = 0; row < NUM_ROW; row++)
{
printf(“%d”,vals[row][col]);
}
printf(“\n”);
}
...

27 Department of CSE, CUET


Matrix Addition Algorithm

⮚ Add two n x m matrices A, B;

▪ for each row i of A do


▪ for each column j of A do
▪ C[i][j] = A[i][j] + B[i][j];

⮚ Output matrices A, B and C.

28 Department of CSE, CUET


matrixAdd.cpp
...
int main()
{
int NUM_ROWS=3; // number of matrix rows
int NUM_COLS=2; // number of matrix columns
int i,j;

// Note: A, B and C have the same dimensions


int A[NUM_ROWS][NUM_COLS] = { {3, 3}, {1, 1}, {2, 0} };
int B[NUM_ROWS][NUM_COLS] = { {3, 0}, {3, 0}, {0, 1} };
int C[NUM_ROWS][NUM_COLS];

// C = A + B
for (i=0; i<NUM_ROWS; i++)
{
for (j=0; j<NUM_COLS; j++)
{
C[i][j] = A[i][j] + B[i][j];
}
}
...

29 Department of CSE, CUET


matrixAdd.cpp (2)

...
// display C
for (i=0; i<NUM_ROWS; i++)
{
for (j=0; j<NUM_COLS; j++)
{
printf(“%d”,C[i][j]);
}
printf(“\n”);
}
printf(“\n”);
...

30 Department of CSE, CUET


Dynamic Arrays

⮚ The process of allocating memory at compile time is


known as Static Memory Allocation and the arrays that
receive static memory allocation are called Static
Arrays.

⮚ In C it is possible to allocate memory to arrays at run


time. This feature is known as Dynamic Memory
Allocation and the arrays created at run time are called
Dynamic Arrays.

31 Department of CSE, CUET


Common Programming Errors

⮚ Addressing indices that are out of bounds of the array


range. This will run-time crash or at the very least a
logical error. Be careful especially when using
expressions to compute the indices.
▪ Remember, indexing starts with 0, not 1!!!

⮚ Forgetting to declare the array (either altogether or


forgetting the [])

⮚ Assuming the array is initialized (to zero.)

32 Department of CSE, CUET


33 Department of CSE, CUET

You might also like