Queries to add, remove and return the difference of maximum and minimum

Introduction

Efficient data manipulation is essential in the field of programming. Managing a collection of elements is one common task, and we frequently need to carry out operations like adding or removing elements and figuring out the difference between the maximum and minimum values.

Adding Elements to a Collection

An array is commonly used in C to represent a group of elements. An array's current size must be taken into account when adding elements, and memory must be dynamically allocated to make room for the new elements. Let's look at a brief example of C code that shows how to add elements to an array:

Code

Output:

Queries to add, remove and return the difference of maximum and minimum

Code Explanation

Dynamic Memory Allocation

  • The {addElement` function demonstrates C's dynamic memory allocation. To accommodate the new element, it uses the `realloc` function to increase the array's size by 1. This guarantees effective memory management and permits the array's dynamic expansion when required.

Appending Elements

  • The function adds a new entry to the array's end. The new element is added by accessing the last position in the resized array ({arr[size - 1]}). This method makes sure that the new data is seamlessly integrated while maintaining the integrity of the existing elements.

Returning the Updated Array

  • After the new element is added, the function returns the updated array. As a result, the modified array can be retained by the main program, allowing for additional operations or display. Proper management of the array pointer guarantees that the changes remain valid outside of the function's scope.

Memory Deallocation

  • It is essential to use the free function in the main function to release the memory that has been allotted after using the updated array. This process guarantees effective memory utilization and stops memory leaks. Freeing dynamically allocated memory when it is no longer needed is a good practice to prevent possible problems in programs that run for a long time.

Removing Elements from a Collection

In a similar vein, resizing and deallocating memory are required when deleting elements from an array. This is a sample of code that shows how to take an element out of the array:

Code

Output:

Queries to add, remove and return the difference of maximum and minimum

Code Explanation

Memory Management and Element Removal

  • The purpose of the `removeElement` function is to eliminate an element from an array at a given index. In order to fill the space left by the removed element, it moves the elements that come after it. It also modifies the array's size and uses `realloc` to reallocate memory to reflect the smaller size.

Initialization and Display of a Dynamic Array

  • Using `malloc`, an array with space for five integers is dynamically initialized in the `main` function. Next, values are added to the array, setting each entry to a multiple of 10. Before any removal operations, the original array is shown to give insight into the data structure's initial state.

Index-Based Element Removal

  • After the removal, the final array is shown to demonstrate how successful the removal procedure was. It highlights the value of dynamic memory reallocation when modifying the array's size and the necessity of index validation to stop accessing elements beyond the array boundaries.

Calculating the Difference between Maximum and Minimum

We can iterate through an array, recording the maximum and minimum values encountered, to determine the difference between the array's maximum and minimum values. This is a sample of code that shows how this works:

Code

Output:

Queries to add, remove and return the difference of maximum and minimum

Code Explanation

Calculation of Difference

  • The difference between an integer array's maximum and minimum values is computed using the `findDifference` function. It starts with the first element and iteratively searches the array for the maximum and minimum values. {max - min} is the final difference that is calculated.

Function Call and Array Initialization

  • An integer array is initialized with the values {4, 9, 2, 7, 5, 1, 8} in the `main` function. Following that, the array and its size (calculated using `sizeof`) are passed to the `findDifference` function.

Handling Empty Arrays

  • Before calculating the difference, the code first checks to see if the array is empty. The function prints a message and returns 0 if the array is empty.