MODUL new hlgjg thaybkhvnghgpv7E_02.pptx

lomic31750 1 views 35 slides May 16, 2025
Slide 1
Slide 1 of 35
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35

About This Presentation

Education


Slide Content

ARRAYS AND STRINGS MODULE: 02

WHAT IS ARRAY ? An array in C is a fixed-size collection of similar data items stored in contiguous memory locations. It can be used to store the collection of primitive data types such as int , char, float, etc., and also derived and user-defined data types such as pointers, structures, etc.

ARRAY DECLARATION we have to declare the array like any other variable before using it. We can declare an array by specifying its name, the type of its elements, and the size of its dimensions. When we declare an array in C, the compiler allocates the memory block of the specified size to the array name.

ARRAY DECLARATION

ARRAY INITIALIZATION Initialization in C is the process to assign some initial value to the variable. When the array is declared or allocated memory , the elements of the array contain some garbage value . So, we need to initialize the array to some meaningful value.

ARRAY INITIALIZATION 1. Array Initialization with Declaration we initialize the array along with its declaration. We use an initializer list to initialize multiple elements of the array. An initializer list is the list of values enclosed within braces  { }  separated b a comma .

Array Initialization

ARRAY INITIALIZATION 2. Array Initialization with Declaration without Size If we initialize an array using an initializer list, we can skip declaring the size of the array as the compiler can automatically deduce the size of the array in these cases. The size of the array in these cases is equal to the number of elements present in the initializer list as the compiler can automatically deduce the size of the array.

ARRAY INITIALIZATION 3.Array Initialization after Declaration (Using Loops) We initialize the array after the declaration by assigning the initial value to each element individually . We can use for loop, while loop, or do-while loop to assign the value to each element of the array.

ACCESS ARRAY ELEMENTS We can access any element of an array in C using the array subscript operator  [ ]    and the index value   i   of the element. One thing to note is that the indexing in the array always starts with 0 , i.e., the  first element  is at index   and the  last element  is at  N – 1   where  N  is the number of elements in the array

ACCESS ARRAY ELEMENTS

One-Dimensional Arrays A one-dimensional array can be viewed as a linear sequence of elements. We can only increase or decrease its size in a single direction.

One-Dimensional Arrays 1D Array Declaration Syntax 1D Array Initialization Syntax

One-Dimensional Arrays

One-Dimensional Arrays 1D Array Element Accessing/Updating Syntax After the declaration, we can use the index of the element along with the array name to access it Then, we can also assign the new value to the element using assignment operator.

Array of Characters (Strings) There is one popular use of array that is the 1D array of characters that is used to represent the textual data. It is more commonly known as strings. The strings are essentially an array of character that is terminated by a NULL character ('\0').

Two-Dimensional (2D) Arrays A two-dimensional array in C programming is essentially a collection of data elements organized in rows and columns, forming a matrix-like structure. It is a grid or table of elements, where each element can be accessed using two indices: one for the row and one for the column.

Two-Dimensional (2D) Arrays Key Features of Two-Dimensional Arrays: Rows and Columns : A two-dimensional array has rows and columns to store data in a tabular form. Homogeneous Elements : All elements in a two-dimensional array must be of the same data type. Fixed Size : The number of rows and columns is fixed at the time of array declaration.

Two-Dimensional (2D) Arrays

Two-Dimensional (2D) Arrays Declaring a Two-Dimensional Array Syntax of a Two-Dimensional Array The general syntax for declaring a two-dimensional array is: data_type array_name [ row_size ][ column_size ]; Declaring and Initializing a Two-Dimensional Array data_type array_name [ row_size ][ column_size ]={ {element1,element2,…..}, {element1,element2,…..}, {element1,element2,…..}, }

Two-Dimensional (2D) Arrays Declaration of 2D Array A 2D array with  m  rows and  n  columns can be created as: where, type :  Type of data to be stored in each element. arr_name :  Name assigned to the array. m :  Number of rows. n :  Number of columns. For example, we can declare a two-dimensional integer array with name  ‘ arr ’  with 10 rows and 20 columns as:

Two-Dimensional (2D) Arrays Accessing Elements in a Two-Dimensional Array You can access elements in a two-dimensional array using the syntax: array_name [ row_index ][ column_index ] ; row_index starts from 0 for the first row. column_index starts from 0 for the first column.

STRINGS A String in C programming is a sequence of characters terminated with a null character ‘\0’. The C String is stored as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character ‘\0’.

STRINGS String Declaration Syntax: Declaring a string in C is as simple as declaring a one-dimensional array. Below is the basic syntax for declaring a string.

STRINGS String Initialization: We can initialize a C string in 4 different ways which are as follows: 1. Assigning a String Literal without Size String literals can be assigned without size. Here, the name of the string str acts as a pointer because it is an array.

STRINGS String Initialization: 2. Assigning a String Literal with a Predefined Size String literals can be assigned with a predefined size. But we should always account for one extra space which will be assigned to the null character. If we want to store a string of size n then we should always declare a string with a size equal to or greater than n+1.

STRINGS String Initialization: 3. Assigning Character by Character with Size We can also assign a string character by character. But we should remember to set the end character as ‘\0’ which is a null character.

STRINGS String Initialization: 4. Assigning Character by Character without Size We can assign character by character without size with the NULL character at the end. The size of the string is determined by the compiler automatically.

STRINGS strlen () function: The  strlen ()  function in C calculates the length of a given string. The  strlen ()  function is defined in  string.h  header file. It doesn’t count the null character ‘\0’. Syntax of C strlen () The syntax of strlen () function in C is as follows:

STRINGS Compare( strcmp () ): strcmp ()  is a built-in library function used to compare two strings . It takes two strings (array of characters) as arguments, compares these two strings, and then returns 0 or 1 as the result. Syntax of strcmp () The strcmp () function is defined inside the  < string.h >  header file as:

STRINGS Concatinate ( strcat () ): strcat () function  appends the string pointed to by src to the end of the string pointed to by dest . It will append a copy of the source string in the destination string. plus a terminating Null character. The initial character of the string( src ) overwrites the Null-character present at the end of the string( dest ). Syntax: Strcat (s1,s2)

STRINGS Copy ( strcpy () ): strcpy ()  is a built-in function used to copy one string into another. It is a part of the C standard strings library, which provides various functions to manipulate strings efficiently. Syntax of strcpy () Parameters: dest : Pointer to the destination character array where the content is to be copied. src :  Pointer to the source character array which is to be copied. Return Value: This function returns a  pointer to dest   string.

Selection Sort Selection Sort  is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the  smallest (or largest)  element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.

Linear Search Algorithm In Linear Search, we iterate over all the elements of the array and check if it the current element is equal to the target element. If we find any element to be equal to the target element, then return the index of the current element. Otherwise, if no element is equal to the target element, then return -1 as the element is not found. Linear search is also known as  sequential search . For example:  Consider the array  arr [] = {10, 50, 30, 70, 80, 20, 90, 40}  and  key  = 30

Binary Search Algorithm Binary Search is an interval searching algorithm that searches for an item in the sorted list. It works by repeatedly dividing the list into two equal parts and then searching for the item in the part where it can possibly exist. Consider an array, arr [] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}  and the  key = 23
Tags