How to save an element and then delete?

Mar 14, 2010 at 2:24pm
How to save an element and then delete?
Last edited on Mar 14, 2010 at 2:27pm
Mar 14, 2010 at 2:26pm
Your question is a bit confusing to me. What do you mean by 'element'? Do you want to save something from user input? We can't help you unless you are more specific.
Mar 14, 2010 at 2:29pm
yeah, i want to find min and secondarymin element in an array, for this should first find min element and save it and then delete. in step 2 find secondaryMin element
Mar 14, 2010 at 2:35pm
Sorry...I'm still confused. You have an array of values (ints/doubles?) and you want to find the two smallest values in the array? What's the point of saving the smallest element (min) and then deleting it? Please elaborate...
Mar 14, 2010 at 4:04pm
|8|2|4|9|12|3|5|

at first find ( a[1]=2 )
next find a[5]=3 that is Secondarymin element in this array. if you dont delete a[1]=2, in this step the compiler refind a[1]=2 as minimum element.
Mar 14, 2010 at 4:14pm
Let's write what you want to do in words. I'm assuming that the array isn't sorted. We'll use a basic sequential search.

1. Set 'min' to be equal to the first (i.e. index zero) element. Have another variable called 'secondMin' handy.
2. Loop through the remaining elements in the array. If an element is less than min, set secondMin = min, and min = newElement.

At the end of the loop, secondMin will be your second smallest element, and min will be your smallest.

Try and translate the above into code. Post your attempt if you get stuck.
Topic archived. No new replies allowed.