100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Array operation (deletion from array|explanation with code|data structure) $8.39   Add to cart

Class notes

Array operation (deletion from array|explanation with code|data structure)

 0 view  0 purchase
  • Course
  • Institution

Improve your knowledge in array

Preview 1 out of 2  pages

  • November 6, 2023
  • 2
  • 2019/2020
  • Class notes
  • Jenny
  • All classes
avatar-seller
1.3 Array Operations | Deletion from Array | Explanation with Code |
Data Structure
Jenny's Lectures CS IT
In this lesson, we will discuss the deletion operation in arrays.
We will use an example array of size 5 to explain the process of
deleting data from a specific position, beginning, and end of
the array. We will also write the code and analyze the time
complexity of the operation.

First, we initialize an array of size 50 but ask the user for the
number of elements they want to insert. We then populate the
array with the user's input and ask them which position they
want to delete data from. For example, if they choose position
2 (index 1), we cannot leave that space blank. Instead, we shift
the elements to fill the empty space and decrease the size of
the array by 1.

We start a loop to shift the elements to the left, starting from
the index before the position chosen by the user until the
second last index of the array. We shift the element at index
i+1 to index i and continue until the end of the loop,
overwriting the deleted element. If we want to print the
deleted data, we store it in a separate variable before shifting
the elements.

We provide the code for the deletion operation, where we ask
the user for the position to delete, check if it's a valid position,
and then start the loop to shift the elements.

int size = 5; int arr[size]; int pos, i; int
item; // ask user for position to delete
printf("Enter position to delete: "); scanf("%d",
&pos); // check if valid position if (pos <= 0
|| pos > size) { printf("Invalid position"); }
else if (pos == size) { printf("No data at
position %d", pos); } else { // start loop to
shift elements for (i = pos-1; i < size-1; i++) {
arr[i] = arr[i+1]; } size--; // decrease size
of array }

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller SARAVANAVEL. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $8.39. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

76710 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$8.39
  • (0)
  Add to cart