1). Describe an algorithm that takes a list of n integers a1,a2,...,an and find the average of the largest and smallest integers in the list.
// Algorithm to find the largest and the smallest among the list of n integers a1....an and find the average of both
Procedure LargeSmallAvg(a1,...,an:Integers)
largest:=a1 // assume a1 is largest
smallest:=a1 // assume a1 is smallest
for i:= 2 to n // loop through remaining elements
if(ai > largest) // if ai is bigger than largest store ai in largest
largest:=ai
else if (ai < smallest) // if ai is smaller than smallest
// store ai in smallest
smallest:=ai
endif
endif
endfor
Avg:=(largest+smallest)/2 // take the average of smallest and //largest elements in the list
Display Avg // display the average
Get Answers For Free
Most questions answered within 1 hours.