Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*USE \"ReDim\" *THIS IS VBA CODE IN EXCEL Write a byref sub for swap, that accep

ID: 3738296 • Letter: #

Question

*USE "ReDim"

*THIS IS VBA CODE IN EXCEL

Write a byref sub for swap, that accepts the array, array size and first index to swap as input Write a main sub that Reads a list of numbers from column A to sort . We will use randbetween to create a list of numbers - so your code needs to be able to read a list of numbers of any length Sorts them in ascending order using Bubble Sort Counts the number of swaps writes the sorted array in column C Writes the number of swaps in cell D1 Programming Logic and Design, Eighth Edition

Explanation / Answer

'Open the excel an fill A1 to A50 by random numbers of your choice using rand between function.

'Click ALT+F11 to open the VB editor. Create a new module by right-clicking your sheet name in the left pane 'and select insert module option. Insert the below subroutines to complete the task. Press Alt+F8 from the excel 'sheet and select main routine and click run. This will fill the cells C1 - C50 with sorted numbers and D1 with 'number of swaps performed

Sub main()
Dim inputSize As Long
'Size of the array assumed to be 50
inputSize = 50
Dim inputList(50) As Long
Dim cell As Range
Dim i As Long, j As Long, swapCount As Long
i = 1
'reading the value of each input from the sheet and store in the array
For Each cell In Range("A1:A50")
inputList(i) = cell.Value
i = i + 1
Next
'Initialise the swap count as 0
swapCount = 0
'Implement bubble sort algorithm for ascending sorting in which the adjacent elements are compared
'If we need descending order just change the ">" sign in the comparison to "<"
For i = 1 To inputSize - 1
For j = 1 To inputSize - i
If inputList(j) > inputList(j + 1) Then
'Call the swap method in case of wrong order and increment the swap count
Call swap(inputList, inputSize, j)
swapCount = swapCount + 1
End If
Next j
Next i
i = 1
'Write the sorted array to cells C1 to C50
For Each cell In Range("C1:C50")
cell.Value = inputList(i)
i = i + 1
Next
'Write the swap count on to D1. Just reused the cell
For Each cell In Range("D1:D1")
cell.Value = swapCount
Next
End Sub

Private Sub swap(ByRef inputList() As Long, ByVal size As Long, ByVal firstIndex As Long)
'Condition for swapping
If (firtIndex < size - 1) Then
'swap the elements at first index and first index + 1
Dim temp As Long
temp = inputList(firstIndex)
inputList(firstIndex) = inputList(firstIndex + 1)
inputList(firstIndex + 1) = temp
End If
End Sub

A sample out with values of A, B, C and D columns. In B column I give the first last value for randBetween function

===========

23732245 1 1051029 580 17003301 99999999 1065823 54805382 5019516 3429540 6467705 56370558 7903454 49353726 10767947 36740678 11510264 33316452 13075915 29005638 13442068 23186762 18002221 30124571 19325727 90463528 23098608 26802516 27496936 51416734 29923608 78427060 29964070 94045272 31171560 28830621 32823029 55455369 33335475 22529112 34765655 54453215 35741387 47380561 38422367 20218392 38577869 57369610 41020668 8852705 43146405 79660257 48770916 68734279 52013597 91828081 57891435 5847355 60924249 33502491 64776151 45667741 65291399 66972794 65573359 77732511 66497835 50098441 69832992 76961339 70168881 41811464 71253320 81864884 73471016 21683811 74355039 80625880 77530819 5897792 79596912 24201780 81219093 64393232 82528050 28859893 85165103 55495351 86430912 82138786 91698047 718219 91787216 7528122 91974629 76023649 94249117 37566950 94851561 5335908 95691918 46932798 98201577