ARRAYS 1. An array is a group of similar data items the elements & an array are a the identical type and each element can be accessed using the same name, but with different index values. 2. The elements of the array are collected in consecutive memory places and are referenced by an index (also recognized as the subscript). 3. A subscript is an ordinal number which it used to recognize an element of the array SYNTAX Datatype arrayname [Size]; Eg . Char name[25] CLARA JINCY B 23CSU009 DATA STRUCTURE
REPRESENTATION OFAN ARRAY IN MEMORY: We can represent an array in various ways in different programming languages As an illustration lets see the declaration of array in language. As per the above Illustration there are some of the following Important points: * Index starts with 0. * the arrays length is 10, which means can store 10 elements * Each element in the array can be accessed via its index. DATA STRUCTURE CLARA JINCY B 23CSU009
ARRAY OPERATIONS The basic operations supported in the array: • Traversal : this operation is used to print the elements of the array. • Insertion : It is used to add an element at a particular index. • Deletion : It is used to delete an element from a Particular index. • Search : It used to search an element using the given index. • Update : It updates an element at a particular index. TRAVERSAL OPERATION The operation is performed to traverse through the array element one after another. #include <stdio.h> Void main( ) { int arr [5]={18,30,15,70,12}; int i; Printf(“Elements of the array are:”n); For(i=0;i<5;i++){ Printf(“Arr[%d]=%d”,i,arr[i]; } } Output Elements the array are Arr[0] = 18, Arr[] = 30, Arr[2] =15 Arr[3] = 70, Arr[4] =12 DATA STRUCTURE CLARA JINCY B 23CSU009
INSERTION OPERATION The operation is performed to front one or more elements the array, As per the requirements an element can be added at the beginning, and, or, at any index of the array. #include <iostream> using namespace std; void insertElement(int arr[], int& n, int element, int position) { if (position > n + 1 || position < 1) { cout << “Invalid position!” << endl; return; for (int I = n; I >= position; i--) { arr[i] = arr[I – 1]; arr[position – 1] = Element; n++; } void displayArray(int arr[], int n) { for (int I = 0; I < n; i++) { cout << arr[i] << “ “; } cout << endl; } DATA STRUCTURE CLARA JINCY B 23CSU009
Int main() { int arr[100] = {1, 2, 3, 4, 5}; // Initial array int n = 5; // Number of elements in the array int element = 10; int position = 3; cout << “Original array: “; displayArray(arr, n); insertElement(arr, n, element, position); cout << “Array after insertion: “; displayArray(arr, n); return 0; } Original array: 1 2 3 4 5 Array after insertion: 1 2 10 3 4 5 Output CLARA JINCY B 23CSU009 DATA STRUCTURE
DELETION OPERATION The operation removes an element from the away then recognize all of the array Elements. #include <iostream> using namespace std; void deleteElement(int arr[], int& n, int x) { int pos = -1; for (int I = 0; I < n; i++) { if (arr[i] == x) { pos = I; break; } } if (pos == -1) { cout << “Element not found.” << endl; return; } for (int I = pos; I < n – 1; i++) { arr[i] = arr[I + 1]; } n--; } CLARA JINCY B 23CSU009 DATA STRUCTURE
Int main() { int arr[] = {1, 2, 3, 4, 5}; int n = 5, x; cout << “Original array: “; for (int I = 0; I < n; i++) cout << arr[i] << “ “; cout << “nEnter element to delete: “; cin >> x; Delete element (arr, n, x); Cout<< “Array after deletion: “; for (int I = 0; I < n; i++) cout << arr[i] << “ “; cout << endl; return 0; } Original array: 1 2 3 4 5 Enter element to delete: 3 Array after deletion: 1 2 4 5 Output Original array: 1 2 3 4 5 Enter element to delete: 6 Element not found. Array after deletion: 1 2 3 4 5 CLARA JINCY B 23CSU009 DATS STRUCTURE
SEARCH OPERATION The operation to performed to search an element in the array based on the value or index. #include <iostream> int linearSearch(int arr[], int size, int target) { for (int I = 0; I < size; i++) { if (arr[i] == target) { return I; } } return -1; } int main() { int arr[] = {34, 78, 12, 56, 23, 89, 90}; int size = sizeof(arr) / sizeof(arr[0]); int target = 56; int result = linearSearch(arr, size, target); if (result != -1) { cout << “Element found at index: “ << result << endl; } else { cout << “Element not found” << endl; } return 0; } Output Element found at index: 3 CLARA JINCY B 23CSU009 DATA STRUCTURE
UPDATE OPERATION #include <stdio.h> void main () 2 int arr In] = = 2183,80,15,10,123; int item =50, 1, POS = 8; Printf(“ Given array olamenti are: n”); For(i=0;i<5;i++){ Printf (“arr [%d] = %d”, arr[i]; } Arr [pos-1]= item; printf (“nn Array elements after updation : n”); For(i=0;i<5;i++){ Printf (“arr[%d] = %d”, arr[i]); } } Output Given array elements are arr[0]=18 , arr[1]=30, arr[2] = 15, arr [3]=70 , arr[4]=12 Array elements after updation arr[0]=18 , arr[1]=30,arr[2]=50,arr[3]=70,arr[4]=12 CLARA JINCY B 23CSU009 DATA STRUCTURE
• Storing and accessing data: Arrays are used to store and retrieve data in a specific order. For example, an array can be used to store the scores of a group of students, or the temperatures recorded by a weather station. • Sorting:- Arrays can be used to sort data in ascending or descending order. Sorting algorithms such as bubble sort, merge sort, and quicksort rely heavily on arrays. • Searching: Arrays can be searched for specific elements using algorithms such as linear search and binary search. APPLICATIONS OFARRAY • Matrices: Arrays are used to represent matrices in mathematical computations such as matrix multiplication, linear algebra, and image processing • Stacks and queues: Arrays are used as the underlying data structure for implementing stacks and queues, which are commonly used in algorithms and data structures • Graphs: Arrays can be used to represent graphs in computer science. Each element in the array represents a node in the graph, and the relationships between the nodes are represented by the values stored in the array • Dynamic programming: Dynamic programming algorithms often use arrays to store intermediate results of sub problem in order to solve a larger problem. CLARA JINCY B 23CSU009 DATA STRUCTURE

Array (data structure using c++).PPT presentation

  • 1.
    ARRAYS 1. An arrayis a group of similar data items the elements & an array are a the identical type and each element can be accessed using the same name, but with different index values. 2. The elements of the array are collected in consecutive memory places and are referenced by an index (also recognized as the subscript). 3. A subscript is an ordinal number which it used to recognize an element of the array SYNTAX Datatype arrayname [Size]; Eg . Char name[25] CLARA JINCY B 23CSU009 DATA STRUCTURE
  • 2.
    REPRESENTATION OFAN ARRAYIN MEMORY: We can represent an array in various ways in different programming languages As an illustration lets see the declaration of array in language. As per the above Illustration there are some of the following Important points: * Index starts with 0. * the arrays length is 10, which means can store 10 elements * Each element in the array can be accessed via its index. DATA STRUCTURE CLARA JINCY B 23CSU009
  • 3.
    ARRAY OPERATIONS The basicoperations supported in the array: • Traversal : this operation is used to print the elements of the array. • Insertion : It is used to add an element at a particular index. • Deletion : It is used to delete an element from a Particular index. • Search : It used to search an element using the given index. • Update : It updates an element at a particular index. TRAVERSAL OPERATION The operation is performed to traverse through the array element one after another. #include <stdio.h> Void main( ) { int arr [5]={18,30,15,70,12}; int i; Printf(“Elements of the array are:”n); For(i=0;i<5;i++){ Printf(“Arr[%d]=%d”,i,arr[i]; } } Output Elements the array are Arr[0] = 18, Arr[] = 30, Arr[2] =15 Arr[3] = 70, Arr[4] =12 DATA STRUCTURE CLARA JINCY B 23CSU009
  • 4.
    INSERTION OPERATION The operationis performed to front one or more elements the array, As per the requirements an element can be added at the beginning, and, or, at any index of the array. #include <iostream> using namespace std; void insertElement(int arr[], int& n, int element, int position) { if (position > n + 1 || position < 1) { cout << “Invalid position!” << endl; return; for (int I = n; I >= position; i--) { arr[i] = arr[I – 1]; arr[position – 1] = Element; n++; } void displayArray(int arr[], int n) { for (int I = 0; I < n; i++) { cout << arr[i] << “ “; } cout << endl; } DATA STRUCTURE CLARA JINCY B 23CSU009
  • 5.
    Int main() { intarr[100] = {1, 2, 3, 4, 5}; // Initial array int n = 5; // Number of elements in the array int element = 10; int position = 3; cout << “Original array: “; displayArray(arr, n); insertElement(arr, n, element, position); cout << “Array after insertion: “; displayArray(arr, n); return 0; } Original array: 1 2 3 4 5 Array after insertion: 1 2 10 3 4 5 Output CLARA JINCY B 23CSU009 DATA STRUCTURE
  • 6.
    DELETION OPERATION The operationremoves an element from the away then recognize all of the array Elements. #include <iostream> using namespace std; void deleteElement(int arr[], int& n, int x) { int pos = -1; for (int I = 0; I < n; i++) { if (arr[i] == x) { pos = I; break; } } if (pos == -1) { cout << “Element not found.” << endl; return; } for (int I = pos; I < n – 1; i++) { arr[i] = arr[I + 1]; } n--; } CLARA JINCY B 23CSU009 DATA STRUCTURE
  • 7.
    Int main() { intarr[] = {1, 2, 3, 4, 5}; int n = 5, x; cout << “Original array: “; for (int I = 0; I < n; i++) cout << arr[i] << “ “; cout << “nEnter element to delete: “; cin >> x; Delete element (arr, n, x); Cout<< “Array after deletion: “; for (int I = 0; I < n; i++) cout << arr[i] << “ “; cout << endl; return 0; } Original array: 1 2 3 4 5 Enter element to delete: 3 Array after deletion: 1 2 4 5 Output Original array: 1 2 3 4 5 Enter element to delete: 6 Element not found. Array after deletion: 1 2 3 4 5 CLARA JINCY B 23CSU009 DATS STRUCTURE
  • 8.
    SEARCH OPERATION The operationto performed to search an element in the array based on the value or index. #include <iostream> int linearSearch(int arr[], int size, int target) { for (int I = 0; I < size; i++) { if (arr[i] == target) { return I; } } return -1; } int main() { int arr[] = {34, 78, 12, 56, 23, 89, 90}; int size = sizeof(arr) / sizeof(arr[0]); int target = 56; int result = linearSearch(arr, size, target); if (result != -1) { cout << “Element found at index: “ << result << endl; } else { cout << “Element not found” << endl; } return 0; } Output Element found at index: 3 CLARA JINCY B 23CSU009 DATA STRUCTURE
  • 9.
    UPDATE OPERATION #include <stdio.h> voidmain () 2 int arr In] = = 2183,80,15,10,123; int item =50, 1, POS = 8; Printf(“ Given array olamenti are: n”); For(i=0;i<5;i++){ Printf (“arr [%d] = %d”, arr[i]; } Arr [pos-1]= item; printf (“nn Array elements after updation : n”); For(i=0;i<5;i++){ Printf (“arr[%d] = %d”, arr[i]); } } Output Given array elements are arr[0]=18 , arr[1]=30, arr[2] = 15, arr [3]=70 , arr[4]=12 Array elements after updation arr[0]=18 , arr[1]=30,arr[2]=50,arr[3]=70,arr[4]=12 CLARA JINCY B 23CSU009 DATA STRUCTURE
  • 10.
    • Storing andaccessing data: Arrays are used to store and retrieve data in a specific order. For example, an array can be used to store the scores of a group of students, or the temperatures recorded by a weather station. • Sorting:- Arrays can be used to sort data in ascending or descending order. Sorting algorithms such as bubble sort, merge sort, and quicksort rely heavily on arrays. • Searching: Arrays can be searched for specific elements using algorithms such as linear search and binary search. APPLICATIONS OFARRAY • Matrices: Arrays are used to represent matrices in mathematical computations such as matrix multiplication, linear algebra, and image processing • Stacks and queues: Arrays are used as the underlying data structure for implementing stacks and queues, which are commonly used in algorithms and data structures • Graphs: Arrays can be used to represent graphs in computer science. Each element in the array represents a node in the graph, and the relationships between the nodes are represented by the values stored in the array • Dynamic programming: Dynamic programming algorithms often use arrays to store intermediate results of sub problem in order to solve a larger problem. CLARA JINCY B 23CSU009 DATA STRUCTURE