Question

In: Computer Science

C++ , Write an iterative routine that will have 2 queues. Loop for 100 times and...

C++ , Write an iterative routine that will have 2 queues. Loop for 100 times and in the loop roll two die and place the first result into the queue1 and second into queue2. Then dequeue the rolls one at a time from each, printing each pair, and count how many times two rolls either add up to 7 or are doubles (i.e. same value). After the queues are empty, print out the number of 7s and doubles.

Assume srand has already been done.

Solutions

Expert Solution

we will use rand() function here to generate random numbers between range(1 to 6) which is dies range.

1+ rand()%6 (rand()%6 generates random number in range 0 to 5 we added 1 to make range 1 to 6)

first we iterate 100 times and inserted values in queue q1 and q2;

Then we popped out elements from queue and counted pair with sum 7 or pair that doubles(both value same).

following is the full code with comments.

#include<bits/stdc++.h>
using namespace std;
int main()
{
    queue<int> q1,q2;//declaring 2 queues
     srand(time(0));//in question given that it is done before
   for(int i=1;i<=100;i++) //iterating 100 times
   {
       //rand() generates raandom number we used %6 to generate random number between  0 to 5 and added one to get number in die's range(1 to 6)
       int value_die1 = 1+(rand()%6);
       int value_die2 = 1+(rand()%6);
       q1.push(value_die1);
       q2.push(value_die2);
   }
   int ans=0;//will tel number of apir which adds up to 7 or doubles
   while(!q1.empty())//as size of both q1 and q2 same  run  lopp til any of them is no empty; 
   {
        int value1 = q1.front();
        q1.pop();
        int value2 = q2.front();
        q2.pop();
        if(value1==value2)  //both number  at dies are same(doubles) 
        ans++;
        if((value1+value2)==7)
        ans++;
   } 
   cout<<ans;
}


Related Solutions

Write a RIMS-compatible C-language for-loop that counts the number of times a bit of A is...
Write a RIMS-compatible C-language for-loop that counts the number of times a bit of A is followed by a bit of the opposite parity (01 or 10) and writes the value to B. For example 00100110 has 4 cases: 00100110, 00100110, 00100110, 00100110.
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version)...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version) The recursive ternarySearch method returns true or false depending if the element was found or not. The ternarySearch method works in a similar manner to a binary search except it uses two mid values that “divide” the array into three portions. So, it needs to consider three recursive scenarios: See sample run: Accounts are: [0] 5658845 [1] 8080152 [2] 1005231 [3] 4520125 [4] 4562555...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version)...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version) The recursive ternarySearch method returns true or false depending if the element was found or not. The ternarySearch method works in a similar manner to a binary search except it uses two mid values that “divide” the array into three portions. So, it needs to consider three recursive scenarios: See sample run: Accounts are: [0] 5658845 [1] 8080152 [2] 1005231 [3] 4520125 [4] 4562555...
2.c++ if and loop statement Write a program that will count the number of even number...
2.c++ if and loop statement Write a program that will count the number of even number and odd numbers between two inputted numbers. Display the numbers and compute the sum and average of all the even numbers and the sum and average all the odd numbers. Sample outputs: Enter starting number:3 Enter starting number:4 Enter ending number:10 Enter ending number:10 odd numbers Even number 3 4 5 6 7 8 9 10 number of even numbers=4 number of even numbers=4...
write an operational semantic for a while loop in c++
write an operational semantic for a while loop in c++
C++ Progamming This lab gives you a little practice with stacks and queues ·In “stackfib.cpp”, write...
C++ Progamming This lab gives you a little practice with stacks and queues ·In “stackfib.cpp”, write a non-recursive function fib() which: ·Takes a single int parameter n ·Returns the nth Fibonacci number. We will start with fib(0) == fib(1) == 1, then fib(n) = fib(n-1) + fib(n-2) ·To compute this without using recursion, we use a stack to store the numbers we need to compute (1)   Initialize your result to be 0 (2)   Push the parameter n onto the stack (3)   While the...
4.3 Lab: Queues 1 Write the c++ implementation of the following four member functions of the...
4.3 Lab: Queues 1 Write the c++ implementation of the following four member functions of the Queue class: getLength(): returns the number of elements in the queue isEmpty(): returns true if the queue is empty, false otherwise peek(): returns the value at the front of the queue without removing it. Assumes queue is not empty (no need to check) peekRear(): returns the value at the end of the queue without removing it. Assumes queue is not empty (no need to...
C++, Write a routine that would receive a pointer to the top of the linked list...
C++, Write a routine that would receive a pointer to the top of the linked list that has an integer for each node. Count all positive even integers in the linked list but make negatives into positives. Return the count negatives that became positives. Hint, use Node<ItemType>* to define pointers and use pointers to go through the linked list.
C++ Write a routine that would receive a pointer to the top of the linked list...
C++ Write a routine that would receive a pointer to the top of the linked list that has a string for each node. Count all strings that start with a vowel (assume lowercase) in the linked list but tack on a “?” on all non-vowel strings. Return the count. Hint, use Node<ItemType>* to define pointers and use pointers to go through the linked list.
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT