FPL Lab -5 Bhargavi Dalal
Aim - Write a C Program to perform various operations on Array
• Arrays in C are one of the most versatile and powerful data structures in C. • What are Arrays in C Programming? • An Array in C programming language is a powerful data structure that allows users to store and manipulate a collection of elements, all of the same data type in a single variable. • Simply, it is a collection of elements of the same data type. • Arrays are the derived data type in C that can store values of both - fundamental data types like int, and char; and derived data types like pointers, and structure. The values get stored at contagious memory locations that can be accessed with their index number.
Operations on Arrays in Data Structures • Here are some common operations on arrays in data structures: • Traversal: Array traversal refers to the process of accessing each element of an array in a sequential order from the beginning to the end or from the end to the beginning. • Insertion: Insertion is the process of adding a new element to an array. Depending on the application, the new element can be inserted at the beginning, end, or any other location within the array. When inserting an element, the other elements in the array may need to be shifted to accommodate the new element.
• Deletion: Deletion is the process of removing an existing element from an array. Depending on the application, the element to be deleted can be at any location within the array. When deleting an element, the other elements in the array may need to be shifted to fill the gap left by the deleted element. • Search: Search is the process of finding a specific element within an array. This is done by comparing the target element with each element in the array until a match is found. There are various search algorithms that can be used, such as linear search and binary search. • Sorting: Sorting is the process of arranging the elements in an array in a specific order. The most common sorting algorithms are bubble sort, insertion sort, selection sort, merge sort, and quicksort. Sorting an array can help with searching and other operations, as it allows for efficient data processing. • Access: Access is the process of retrieving the value of an element from an array. This is done by specifying the index of the desired element within the array. Array access is fast and efficient, as it does not require traversal of the entire array.
Algorithm • Write an algorithm to perform various operations on Array • 1. Start • 2. Global declaration • 3. Initialize a variable ‘n’,'i' and key. • 4. Ask user for the number of elements in the array and store in variable ‘n’ • 5. Repeat Steps 6 and 7 until the end of the array • 6. Enter the number of elements in array • 7. Display the number of elements in array • 8. Write the function to find the largest element in the array • 9. Write the function to find the smallest element in the array
Continue… • 10. Print the largest and smallest element of an array. • 11. Write the function to calculate the sum of array element in the array • 12. Write the function to calculate the average of array element in the array • 13. Print the sum and average of a number using array • 14. Write the function to reverse the array • 15. Print the reverse of all array element. • 16. Write the function to search an element in the array • 17. Print the search element found or not found. • 18. End
Flowchart
• inputArray • displayArray • findLargest • findSmallest • sumArray • averageArray • reverseArray • searchElement
• #include <stdio.h> • // Function prototypes • void inputArray(int arr[], int n); • void displayArray(int arr[], int n); • int findLargest(int arr[], int n); • int findSmallest(int arr[], int n); • int sumArray(int arr[], int n); • float averageArray(int arr[], int n); • void reverseArray(int arr[], int n); • int searchElement(int arr[], int n, int key); • int main() • { • int n, i, key; // Ask user for the number of elements in the array • printf("Enter the number of elements: "); • scanf("%d", &n); • int arr[n]; // Input the array elements • inputArray(arr, n); // Display the array • printf("nArray elements: "); • displayArray(arr, n);
• // Find largest and smallest elements • int largest = findLargest(arr, n); • int smallest = findSmallest(arr, n); • printf("nLargest element: %d", largest); • printf("nSmallest element: %d", smallest); • // Calculate sum and average • int sum = sumArray(arr, n); • float avg = averageArray(arr, n); • printf("nSum of elements: %d", sum); • printf("nAverage of elements: %.2f", avg); • // Reverse the array • reverseArray(arr, n); • printf("nArray after reversing: "); • displayArray(arr, n); • // Search for an element • printf("nEnter the element to search: "); • scanf("%d", &key); • int index = searchElement(arr, n, key); • if (index != -1) • printf("Element %d found at index %dn", key, index); • else • printf("Element %d not found in the arrayn", key); • return 0; • }
inputArray • // Function to input elements into the array • void inputArray(int arr[], int n) • { • for (int i = 0; i < n; i++) • { • printf("Enter element %d: ", i + 1); • scanf("%d", &arr[i]); • } • }
displayArray • // Function to display the array elements • void displayArray(int arr[], int n) • { • for (int i = 0; i < n; i++) • { • printf("%d ", arr[i]); • } • printf("n");}
findLargest • // Function to find the largest element in the array • Int findLargest(int arr[], int n) • { • int largest = arr[0]; • for (int i = 1; i < n; i++) • { • if (arr[i] > largest) • { • largest = arr[i]; • } • } • return largest; • } findSmallest • // Function to find the smallest element in the array • int findSmallest(int arr[], int n) • { • int smallest = arr[0]; • for (int i = 1; i < n; i++) • { • if (arr[i] < smallest) • { • smallest = arr[i]; • } • } • return smallest; • }
sumArray • // Function to calculate the sum of array elements • int sumArray(int arr[], int n) • { • int sum = 0; • for (int i = 0; i < n; i++) • { • sum += arr[i]; • } • return sum; • }
averageArray • // Function to calculate the average of array elements • float averageArray(int arr[], int n) • { • int sum = sumArray(arr, n); • return (float)sum / n; • }
reverseArray • // Function to reverse the array • void reverseArray(int arr[], int n) • { • int start = 0, end = n - 1; • while (start < end) • { • int temp = arr[start]; • arr[start] = arr[end]; • arr[end] = temp; • start++; • end--; • } • }
searchElement • // Function to search an element in the array • int searchElement(int arr[], int n, int key) • { • for (int i = 0; i < n; i++) • { • if (arr[i] == key) • { • return i; // Element found, return index • } • } • return -1; // Element not found • }
Conclusion

Program Practical to operations on Array

  • 1.
  • 2.
    Aim - Writea C Program to perform various operations on Array
  • 3.
    • Arrays inC are one of the most versatile and powerful data structures in C. • What are Arrays in C Programming? • An Array in C programming language is a powerful data structure that allows users to store and manipulate a collection of elements, all of the same data type in a single variable. • Simply, it is a collection of elements of the same data type. • Arrays are the derived data type in C that can store values of both - fundamental data types like int, and char; and derived data types like pointers, and structure. The values get stored at contagious memory locations that can be accessed with their index number.
  • 4.
    Operations on Arraysin Data Structures • Here are some common operations on arrays in data structures: • Traversal: Array traversal refers to the process of accessing each element of an array in a sequential order from the beginning to the end or from the end to the beginning. • Insertion: Insertion is the process of adding a new element to an array. Depending on the application, the new element can be inserted at the beginning, end, or any other location within the array. When inserting an element, the other elements in the array may need to be shifted to accommodate the new element.
  • 5.
    • Deletion: Deletionis the process of removing an existing element from an array. Depending on the application, the element to be deleted can be at any location within the array. When deleting an element, the other elements in the array may need to be shifted to fill the gap left by the deleted element. • Search: Search is the process of finding a specific element within an array. This is done by comparing the target element with each element in the array until a match is found. There are various search algorithms that can be used, such as linear search and binary search. • Sorting: Sorting is the process of arranging the elements in an array in a specific order. The most common sorting algorithms are bubble sort, insertion sort, selection sort, merge sort, and quicksort. Sorting an array can help with searching and other operations, as it allows for efficient data processing. • Access: Access is the process of retrieving the value of an element from an array. This is done by specifying the index of the desired element within the array. Array access is fast and efficient, as it does not require traversal of the entire array.
  • 6.
    Algorithm • Write analgorithm to perform various operations on Array • 1. Start • 2. Global declaration • 3. Initialize a variable ‘n’,'i' and key. • 4. Ask user for the number of elements in the array and store in variable ‘n’ • 5. Repeat Steps 6 and 7 until the end of the array • 6. Enter the number of elements in array • 7. Display the number of elements in array • 8. Write the function to find the largest element in the array • 9. Write the function to find the smallest element in the array
  • 7.
    Continue… • 10. Printthe largest and smallest element of an array. • 11. Write the function to calculate the sum of array element in the array • 12. Write the function to calculate the average of array element in the array • 13. Print the sum and average of a number using array • 14. Write the function to reverse the array • 15. Print the reverse of all array element. • 16. Write the function to search an element in the array • 17. Print the search element found or not found. • 18. End
  • 8.
  • 9.
    • inputArray • displayArray •findLargest • findSmallest • sumArray • averageArray • reverseArray • searchElement
  • 10.
    • #include <stdio.h> •// Function prototypes • void inputArray(int arr[], int n); • void displayArray(int arr[], int n); • int findLargest(int arr[], int n); • int findSmallest(int arr[], int n); • int sumArray(int arr[], int n); • float averageArray(int arr[], int n); • void reverseArray(int arr[], int n); • int searchElement(int arr[], int n, int key); • int main() • { • int n, i, key; // Ask user for the number of elements in the array • printf("Enter the number of elements: "); • scanf("%d", &n); • int arr[n]; // Input the array elements • inputArray(arr, n); // Display the array • printf("nArray elements: "); • displayArray(arr, n);
  • 11.
    • // Findlargest and smallest elements • int largest = findLargest(arr, n); • int smallest = findSmallest(arr, n); • printf("nLargest element: %d", largest); • printf("nSmallest element: %d", smallest); • // Calculate sum and average • int sum = sumArray(arr, n); • float avg = averageArray(arr, n); • printf("nSum of elements: %d", sum); • printf("nAverage of elements: %.2f", avg); • // Reverse the array • reverseArray(arr, n); • printf("nArray after reversing: "); • displayArray(arr, n); • // Search for an element • printf("nEnter the element to search: "); • scanf("%d", &key); • int index = searchElement(arr, n, key); • if (index != -1) • printf("Element %d found at index %dn", key, index); • else • printf("Element %d not found in the arrayn", key); • return 0; • }
  • 12.
    inputArray • // Functionto input elements into the array • void inputArray(int arr[], int n) • { • for (int i = 0; i < n; i++) • { • printf("Enter element %d: ", i + 1); • scanf("%d", &arr[i]); • } • }
  • 13.
    displayArray • // Functionto display the array elements • void displayArray(int arr[], int n) • { • for (int i = 0; i < n; i++) • { • printf("%d ", arr[i]); • } • printf("n");}
  • 14.
    findLargest • // Functionto find the largest element in the array • Int findLargest(int arr[], int n) • { • int largest = arr[0]; • for (int i = 1; i < n; i++) • { • if (arr[i] > largest) • { • largest = arr[i]; • } • } • return largest; • } findSmallest • // Function to find the smallest element in the array • int findSmallest(int arr[], int n) • { • int smallest = arr[0]; • for (int i = 1; i < n; i++) • { • if (arr[i] < smallest) • { • smallest = arr[i]; • } • } • return smallest; • }
  • 15.
    sumArray • // Functionto calculate the sum of array elements • int sumArray(int arr[], int n) • { • int sum = 0; • for (int i = 0; i < n; i++) • { • sum += arr[i]; • } • return sum; • }
  • 16.
    averageArray • // Functionto calculate the average of array elements • float averageArray(int arr[], int n) • { • int sum = sumArray(arr, n); • return (float)sum / n; • }
  • 17.
    reverseArray • // Functionto reverse the array • void reverseArray(int arr[], int n) • { • int start = 0, end = n - 1; • while (start < end) • { • int temp = arr[start]; • arr[start] = arr[end]; • arr[end] = temp; • start++; • end--; • } • }
  • 18.
    searchElement • // Functionto search an element in the array • int searchElement(int arr[], int n, int key) • { • for (int i = 0; i < n; i++) • { • if (arr[i] == key) • { • return i; // Element found, return index • } • } • return -1; // Element not found • }
  • 19.