lester

- 20th February 2023, 13:39

Prompt:

Using PICBASIC PRO, for no specific Device. Create a routine to take 16 random numbers, sort them in sequential order of value, discard the top and bottom four values, sum and average the middle eight. Comment the code in plain english

Response:

'This code will take 16 random numbers, sort them in sequential order of value, discard the top and bottom four values, sum and average the middle eight.

Dim I, J, X, Y, Sum, Average

Dim Array(16) As Word

'Use a loop to generate the 16 random numbers and store them in the Array

For I = 0 To 15

Array(I) = Rnd(1)

Next

'Use a loop to sort the numbers in sequential order of value

For I = 0 To 14

For J = I + 1 To 15

If Array(I) > Array(J) Then

X = Array(I)

Y = Array(J)

Array(I) = Y

Array(J) = X

End If

Next

Next

'Initialize the Sum variable

Sum = 0

'Use a loop to discard the top and bottom four values

For I = 4 To 11

Sum = Sum + Array(I)

Next

'Calculate the average of the middle eight values

Average = Sum / 8

End

Using PICBASIC PRO, for no specific Device. Create a routine to take 16 random numbers, sort them in sequential order of value, discard the top and bottom four values, sum and average the middle eight. Comment the code in plain english

Response:

'This code will take 16 random numbers, sort them in sequential order of value, discard the top and bottom four values, sum and average the middle eight.

Dim I, J, X, Y, Sum, Average

Dim Array(16) As Word

'Use a loop to generate the 16 random numbers and store them in the Array

For I = 0 To 15

Array(I) = Rnd(1)

Next

'Use a loop to sort the numbers in sequential order of value

For I = 0 To 14

For J = I + 1 To 15

If Array(I) > Array(J) Then

X = Array(I)

Y = Array(J)

Array(I) = Y

Array(J) = X

End If

Next

Next

'Initialize the Sum variable

Sum = 0

'Use a loop to discard the top and bottom four values

For I = 4 To 11

Sum = Sum + Array(I)

Next

'Calculate the average of the middle eight values

Average = Sum / 8

End