adviksinghania / c-programs

A repository of various C programs
2 stars 8 forks source link

C program to take an Integer array from user and Perform Operations on it #15

Closed sohampathak1234 closed 2 years ago

sohampathak1234 commented 2 years ago

C program to take a integer array from user and Perform Operations like

  1. Search Element.
  2. Update Array.
  3. Delete Element from Array.
  4. Sum of Elements.
  5. Count Number of Duplicate Elements.

Hey @adviksinghania !! Please assign me this issue under Hacktoberfest22.

adviksinghania commented 2 years ago

Nice idea Soham, can you please elaborate on how will you update/delete an element from an array?

sohampathak1234 commented 2 years ago

I will take position from the user and then update that element or delete that element.

Please assign me

adviksinghania commented 2 years ago

I don't think deletion of an element in an array is possible in C. You'll have to make a new array and copy all other elements, right?

anjisnu07 commented 2 years ago

Hey, Assign me for this Issue

sohampathak1234 commented 2 years ago

I don't think deletion of an element in an array is possible in C. You'll have to make a new array and copy all other elements, right?

Yes it is possible. An array is the collection of the same data type elements or items stored in a contiguous memory block.

To delete a specific element from an array, a user must define the position from which the array's element should be removed. The deletion of the element does not affect the size of an array.

To Remove an element from an array in C :

For example, suppose an array contains seven elements, arr[] = {10, 25, 14, 8, 12, 15, 5); and the user want to delete element 8. So, first, the user must define the position of the 8th element, which is the 4th, and then check whether the deletion is possible or not. The position of the particular element should not be more than the total elements of an array. Here, we have 7 elements in an array, and the user wants to delete the 8th position element, which is impossible.

Step 1: Input the size of the array arr[] using num, and then declare the pos variable to define the position, and i represent the counter value.

Step 2: Use a loop to insert the elements in an array until (i < num) is satisfied.

Step 3: Now, input the position of the particular element that the user or programmer wants to delete from an array.

Step 4: Compare the position of an element (pos) from the total no. of elements (num+1). If the pos is greater than the num+1, the deletion of the element is not possible and jump to step 7.

Step 5: Else removes the particular element and shift the rest elements' position to the left side in an array.

Step 6: Display the resultant array after deletion or removal of the element from an array.

Step 7: Terminate or exit from the program.

adviksinghania commented 2 years ago

Okay, so you're overwriting the elements adjacent to each other after the specified deletion index. But the last index of the array remains unused and repeating this process n times will make the last n indices of the array unusable. Don't you think this is a wastage of allocated memory? A better approach will be to make a new array during runtime using malloc/calloc, copying the remaining elements, freeing up the memory of the previous array and making the new array as our original array. This will work for both the insertion and deletion but it is heavy computation task and not always guaranteed to work; as you said, an array needs a contiguous memory block.