Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Swap min val with max in array
#1
Solved: 10 Years, 4 Months ago
Hi all,

I want to swap the min value with max value without changing the array element.

Array(10,5,20,35,15)

Thanks in advance
Reply
#2
Solved: 10 Years, 4 Months ago
Code:
sort = Array(10,5,20,35,15)

smallest = sort(0) 'Assuming
greatest = sort(0) 'Assuming

For i = 1 To UBound(sort)
    
    'Looking for smallest
    If sort(i) < smallest then
        smallest = sort(i)
        small_index = i
    End If
    'Looking for greatest
    If sort(i) > greatest then
        greatest = sort(i)    
        great_index = i
    End If
    
Next

'Swapping Values
sort(small_index) = greatest
sort(great_index) = smallest

'Just confirming
For y = o To 4
    msgbox sort(y)
Next
Reply
#3
Solved: 10 Years, 4 Months ago
Tahnks a lot Ankur.........

This question ask me a interviewer.

I have taken min and max value but don't know how to swap.

Thanks Again.... Doing grate job Dear
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to convert a single dimension array to two dimensional array venkatesh9032 3 5,541 02-10-2014, 03:07 PM
Last Post: pranikgarg
  Adding array to another Global array in VB Script test911 1 3,174 12-02-2012, 12:40 PM
Last Post: parminderdhiman84

Forum Jump:


Users browsing this thread: 2 Guest(s)