0% found this document useful (0 votes)
3 views9 pages

unit-1 Array , one dimensional array

One dimensional array from unit 1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

unit-1 Array , one dimensional array

One dimensional array from unit 1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 9
s ERETARRay | An array is a type of linear data structure that is defin same or different data types. They exist in both single These data structures come into picture when there is a of similar nature together at one place. ed as a collection of elements with dimension and multiple dimensions. necessity to store multiple elements Memory Address | INTRODUCTION TO DATA STRUCTURES Array Values Array Index _ Following are the important terms to understand the concept of Array. © Element : Each item stored in an array is called an element. © Index : Each location of an element in an array has a numerical index, which is used to identify the element. 1.4.1 : Types of Arrays There are two types of arrays: © — One-Dimensional Arrays e@ = Multi-Dimensional Arrays One -Dimensional Arrays A one-dimensional array is a kind of linear array. It involves single sub-scripting. The [] (brackets) is used for the subscript of the array and to declare and access the elements from the array. Syntax: DataType ArrayName [size]; For example: int a[10]; Multi-Dimensional Arrays In multi-dimensional arrays, we have two categories: Two-Dimensional Arrays © Three-Dimensional Arrays 1. Two-Dimensional Arrays ‘An array involving two subscripts (][] is known as a two-dimensional array. They are also known as the array of the array. Two-dimensional arrays are divided into rows and columns and are able to handle the data of the table. Syntax: DataType ArrayName[row_size]{column_size]; For Example: int arr[5][5}; 2. Three-Dimensional Arrays When we require to create two or more tables of the elements to declare the array elements, then in such a situation we use three-dimensional arrays. Syntax: DataType ArrayName[sizel ][size2][size3); For Example: int a(5][5][5]; Sunn. 1.4.2: Memory representation of an array As stated above, all the data elements of an array are main memory. The name of the array represents the base a element in the main memory. Each element of the array Is rep) We can define the indexing of an array in the below ways : 1. 0 (zero-based indexing): The first element of the array will be arr[0]. 2. 1 (one-based indexing): The first element of the array will be arr[1]- a i 3. (= based indexing): The first element of the array can reside at any random index number stored at contiguous locations in th. ddress or the address of the fj resented by proper indexi Base Address 100 104 108 |_| eo int arr[5] In the above image, we have shown the memory allocation of an array arr of size 5. The array follows a 0-based indexing approach. The base address of the array is 100 bytes. It is the address of arr{0]. Here, the size of the data type used is 4 bytes; therefore, each element will take 4 bytes in the memory. 1.4.3 : Basic Operations in Array The basic operations in the Arrays are insertion, deletion, searching, and update. display, traverse, Following are the basic operations supported by an array. ‘Traverse — print all the array elements one by one. Insertion — Adds an element at the given index. Deletion — Deletes an element at the given index. Search — Searches an element using the given index or by the value. Update — Updates an element at the given index. © Display — Displays the contents of the array. Insertion Operation In the insertion operation, we are adding one or more element; requirement, a new element can be added at the beginning, end, or Algorithm Following is an algorithm to insert elements into a Linear Arra’ the array “ eeeee s to the array. Based on the any given index of array. 'y until we reach the end of Start 2. Create an Array of a desired datatype and size. INTRODUCTION TO DATA STRUCTURES. 3. Initialize a variable ‘i’ as 0. 4. Enter the element at ith index of the array. 5. Increment i by 1. 6. Repeat Steps 4 & 5 until the end of the array. 7. Stop Example #include int main(){ int LAB] = (), i: printf(“Array Before Insertion:\n”); for(i = 0; i <3; i++) printf(“LA[%d] = %d \n”, i, LATi)); printf(“‘Inserting Elements. \n”); printf(“The array elements after insertion :\n”); // prints array values for(i = 0; i <3; i++) { LA[i] =i + 2; printf(“LA[%d] = %d \n”, i, LA[i]); } return 0; } Output Array Before Insertion: LA[0] = 0 LA[I] =0 ii LA[2]=0 Inserting elements.. ‘Array After Insertion: * Lato] =2 LAI] =3 LA[2] = LAB] =5 LA[4] = 6 Deletion Operation : : : In this array operation, we delete an element from the particular index of an array. mas deletion operation takes place as we assign the value in the consequent index to the current index. MS Algorithm 4K isa positive integer such that x Consider LA is nar array with N elements 2 a tion of LA. Following is the algorithm to delete an element available 1. Start 2. SetI=K 3. Repeat steps 4 and 5 while J void main(){ int LA(] = {1,3,5}; intn =3; int i; printf(“The original array elements are :\n"); for(i = 0; i void main(){ int LA(] = {1,3,5,7,8}; int item = 5,n = 5; inti=0,j=0; printf{“The original array elements are :\n"); for(i = 0; i int mainO{ int LA[] = {1,3,5,7,8}; int item = 10,k=3,n=5; inti=0,j=n; printf(“The original array elements are :\n"); for(i = 0; i #include // Function to merge two arrays and sort the result void mergeArrays(int* arrl, int nl, int* arr2, int n2, int* arr3) { int i= 0,j=0,k=0; // Traverse arr] and insert its elements into arr3 while (i

You might also like