In: Computer Science
Given a queue of integers, write an algorithm in Pseudocode that, using only the queue ADT, calculates and prints the sum and the average of the integers in the queue without changing the contents of the queue.
1.find out the size of the queue
2.take the value from the queue mean pop the value take it in variable then make it sum and put there a count in a loop of size length of the queue.
3.after popping again append in queue until the loop close.
and if you are doing in c then swap the front and rear after finishing the loop.
4.then divide the sum by the count.
python code:
queue = [10,12,13,15]
y=(len(queue))
sum=0
count=0
print("Initial queue")
print(queue)
for i in range(y):
print("\nElements dequeued from queue",x)
x=queue.pop(0)
sum=sum+x
count=count+1
queue.append(x)
print("Total sum=",sum)
print("The avg of the que=",(sum/count))
print("\nQueue after performing the operation")
print(queue)
output:
Initial queue [10, 12, 13, 15] Elements dequeued from queue 15 Elements dequeued from queue 10 Elements dequeued from queue 12 Elements dequeued from queue 13 Total sum= 50 The avg of the que= 12.5 Queue after performing the operation [10, 12, 13, 15]