DS----Data Structure unit 1.3
Data Structure - Single Dimensional Array
1.Insertion: We can also insert the new element in a data structure.
#include
int main() { int arr[10]; // declare an array of size 10 int n = 5; // number of elements to insert printf("Enter %d elements into the array:\n", n); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); // read elements from user input } printf("Array elements are:\n"); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); // print the elements in the array } printf("\n"); return 0; }
output:-

2.Searching: We can search for any element in a data structure.
#include
int search(int arr[], int size, int key) { int i; for (i = 0; i < size; i++) { if (arr[i] == key) { return i; } } return -1; } int main() { int arr[5] = {1, 2, 3, 4, 5}; int key ; int position; printf("Enter the Key value Key="); scanf("%d",&key); position = search(arr, 5, key); if (position == -1) { printf("%d not found in the array.\n", key); } else { printf("%d found at position %d in the array.\n", key, position); } return 0; }
output:-

3.Updation: We can also update the element, i.e., we can replace the element with another element.
#include
int main() { int arr[5] = {11, 22, 33, 44, 55}; // initialize an array of five elements int index, value; printf("Array elements before updation:\n"); for (int i = 0; i < 5; i++) { printf("%d ", arr[i]); // print the elements in the array } printf("\n"); printf("Enter the index and value to update:\n"); scanf("%d %d", &index, &value); // read the index and value to update if (index < 0 || index >= 5) { printf("Invalid index\n"); // check if the index is valid } else { arr[index] = value; // update the element at the given index printf("Array elements after updation:\n"); for (int i = 0; i < 5; i++) { printf("%d ", arr[i]); // print the updated elements in the array } printf("\n"); } return 0; }
output:-

4.Deletion: We can also perform the delete operation to remove the element from the data structure.
#include
int main() { int arr[10] = {11, 22, 33, 44, 55,}; int n = 5; int pos, i; // Display the original array printf("Original Array: "); for (i = 0; i < n; i++) { printf("%d ", arr[i]); } // Get the position of the element to be deleted printf("\nEnter the position of the element to be deleted: "); scanf("%d", &pos); // Delete the element at the specified position if (pos < 1 || pos > n) { printf("Invalid position!"); } else { for (i = pos - 1; i < n - 1; i++) { arr[i] = arr[i + 1]; } n--; // Display the updated array printf("Updated Array: "); for (i = 0; i < n; i++) { printf("%d ", arr[i]); } } return 0; }
output:-

5.Sorting: We can sort the elements of a data structure either in an ascending or descending order.
#include
int main() { int arr[5] = {44, 11, 33, 55, 22}; int n = 5; int i, j, temp; // Display the original array printf("Original Array: "); for (i = 0; i < n; i++) { printf("%d ", arr[i]); } // Sort the array in ascending order using bubble sort for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } // Display the sorted array printf("\nSorted Array: "); for (i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
output:-

Data Structure - multi Dimensional Array
A multi-dimensional array is an array that has more than one index or dimension. In C programming language, a multi-dimensional array is simply an array of arrays. For example, a 2D array is an array of arrays, where each sub-array has the same number of elements.
A multi-dimensional array can be thought of as a table or grid of values, where each element is identified by a unique set of indices. The first index identifies the row and the second index identifies the column. For example, in a 2D array arr, arr[i][j] represents the element in row i and column j.
In C, you can create a multi-dimensional array by specifying the number of dimensions and the size of each dimension in the array declaration. Here's an example of a 2D array declaration with 3 rows and 4 columns:
int arr[3][4];
This creates an array with 3 rows and 4 columns, and each element is initialized to 0 by default. You can also initialize the array with values at the time of declaration, like this:
int arr[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
This creates an array with the same dimensions as before, but with initial values specified for each element.
To access the elements of a multi-dimensional array, you use the array indices in the same order as they were specified in the array declaration. For example, to access the element in row 1 and column 2 of arr, you would use the expression arr[1][2].
Multi-dimensional arrays can be useful for representing and processing data that has a natural tabular or grid-like structure, such as images, matrices, or spreadsheet data. They can also be used to implement more complex data structures, such as trees or graphs.
#include
int main() { // Declare a 2D array with 3 rows and 4 columns int arr[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; // Print out the array elements for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { printf("%d ", arr[i][j]); } printf("\n"); } return 0; }
output:-

Comments
Post a Comment