Question

In: Computer Science

Given a queue of integers, write an algorithm in Pseudocode that, using only the queue ADT,...

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.

Solutions

Expert Solution

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]

Related Solutions

Given a queue of integers, write an algorithm and the program in c++ that, using only...
Given a queue of integers, write an algorithm and the program in c++ 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.
Given two unsorted arrays of integers. a) Write a pseudocode algorithm which will output only the...
Given two unsorted arrays of integers. a) Write a pseudocode algorithm which will output only the integers not common to both arrays. When writing pseudocode, consider that no implementation for data structures or algorithms exist. b) Implement your algorithm in Modern C++
1. Write pseudocode for the algorithm using iteration (looping). Envision an algorithm that when given any...
1. Write pseudocode for the algorithm using iteration (looping). Envision an algorithm that when given any positive integer n, it will print out the sum of the squares from 1 to n. For example given 3 the algorithm will print 14 (because 1 + 4 + 9 =14) You can use multiplication denoted as * in your solution and you do not have to define it (For example. 3*3=9) For example if n is 6 it will print: The sum...
Write C++ programs to implement Queue ADT data structure using Linked List.
Write C++ programs to implement Queue ADT data structure using Linked List.
Write pseudocode for quick find algorithm anf quick union algorithm Write pseudocode for quick find algorithm...
Write pseudocode for quick find algorithm anf quick union algorithm Write pseudocode for quick find algorithm and quick union algorithm
Using the Queue ADT: Create a program that uses a Queue. Your program should ask the...
Using the Queue ADT: Create a program that uses a Queue. Your program should ask the user to input a few lines of text and then outputs strings in same order of entry. (use of the library ArrayDeque) In Java please.
JAVA: Implement a Queue ADT using a circular array with 5 string elements. Create a Queue...
JAVA: Implement a Queue ADT using a circular array with 5 string elements. Create a Queue object and try various operations below. Queue myQueue = new Queue(); myQueue.enqueue(“CPS 123”); myQueue.enqueue(“CPS 223”); myQueue.enqueue(“CPS 323”); myQueue.dequeue(); myQueue.enqueue(“CPS 113”); myQueue.enqueue(“CPS 153”); string course = myQueue.front(); // course should be CPS 223 size = myQueue.size(); // size should be 4 // output course and size
Using the linked list abstract data type “Queue ADT”, write a menu dirven user interfece to...
Using the linked list abstract data type “Queue ADT”, write a menu dirven user interfece to teach each of the operations in the ADT. Any errors discovered during the processing should be printed as a part of the test result. Please Use C++ language.
Give an algorithm for reversing a queue Q. Only following standard operations are allowed on queue....
Give an algorithm for reversing a queue Q. Only following standard operations are allowed on queue. 1. enqueue(x) : Add an item x to rear of queue. 2. dequeue() : Remove an item from front of queue. 3. empty() : Checks if a queue is empty or not.. (Hint: you can use LinkedList, Stacks, and Queues from the java data collection) OUTPUT Examples: Input : Q = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] Output :Q =...
//Source: Gilberg et al., Data Structures: A Pseudocode Approach with C //Queue ADT Type Defintions typedef...
//Source: Gilberg et al., Data Structures: A Pseudocode Approach with C //Queue ADT Type Defintions typedef struct node { void* dataPtr; struct node* next; } QUEUE_NODE; typedef struct { QUEUE_NODE* front; QUEUE_NODE* rear; int count; } QUEUE; //Prototype Declarations QUEUE* createQueue (void); QUEUE* destroyQueue (QUEUE* queue); bool dequeue (QUEUE* queue, void** itemPtr); bool enqueue (QUEUE* queue, void* itemPtr); bool queueFront (QUEUE* queue, void** itemPtr); bool queueRear (QUEUE* queue, void** itemPtr); int queueCount (QUEUE* queue); bool emptyQueue (QUEUE* queue); bool fullQueue...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT