Question

In: Computer Science

For this problem, you will write a program using two queues. Generate n random numbers between...

For this problem, you will write a program using two queues.

Generate n random numbers between 10 and 100 (both inclusive), where n>9.

The value of n should be taken as input from the user and n should be >9.

The numbers could be duplicated.

Enqueue all these numbers to the first queue.

The objective is to find the numbers whose sum of digits is odd and enqueue them to the second queue.

The remaining numbers (whose sum of digits is even) should stay in the first queue.

You need to print the generated random numbers and content of both queues.

code in c++

Solutions

Expert Solution

#include<iostream>
#include<stdlib>

using namespace std;

int Q1[], Q2[], n;
int f1=-1,f2=-1,r1=-1,r2=-1;

void enqueue1(int a)//insertion in the first queue
{
   if ((r1+1)%n==f1)
       cout<<"Queue overflow";
   else if(f1==-1 && r1==-1)
   {   ++f1;
       ++r1;
       Q1[r1]=a;
   }
   else
   {   ++r1;
       Q1[r1]=a;
   }
}

void enqueue2(int a)//insertion in the 2nd queue
{
  
   if ((r2+1)%n==f2)
       cout<<"Queue overflow";
   else if(f2==-1 && r2==-1)//if only one element is left in the queue
   {   ++f2;
       ++r2;
       Q2[r2]=a;
   }
   else
   {   ++r2;
       Q2[r2]=a;
   }

}

void deqeue()//method to delete an element from the queue
{

   if(f1==r1)
       f1=r1=-1;
   else if(f1==-1)
       cout<<"Queue Underflow";
   else
       ++f1;
}

void display(int A[], int f, int r);
{
   for(int i=f; i<=r; i++)
   {
       cout<<A[i];
   }  
}

int sumofdigits(int a)//method to calculate the sum of a given number
{
   int sum=0;
   while(n!=0)
   {
       r=n%10;
       sum= sum*10+r;
       n=n/10;
   }
  
   return sum;
}

int main()
{

   cout<<"Enter the maximum size of the queues.";
   cin>>n;
   if(n>9){
       Q1= new int [n];
       Q2= new int [n];

       for(int i=f1; i<r1; i++)
       {
           int random = 1 + 10*(rand() % 11);
           //random number generator between 10 and 100 since the remainder of anything divided by 11 multiplied by 10 and added with 1 ranges from 10 to 100
           enqueue1(random);
       }
      
       for(int i=f1; i<r1; i++)
       {
          
           if(sumofdigits(Q1[i])%2==1)
           {   dequeue();
               enqueue2(random);
           }
       }
  
       display(Q1, f1, r1);
       display(Q2, f2, r2);
}
else
{cout<<"Invalid Entry of Queue size";
exit(0);}
return 0;

}


Related Solutions

Write a Java program to generate random numbers in the following range a. 1 <=n <=...
Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 JAVA PROGRAMMING
I am to write a code that uses two queues, and print the generated random numbers...
I am to write a code that uses two queues, and print the generated random numbers and content of both queues. I need to use the enqueue and dequeue functions in the code.  The instructions are below. Program should be in C Instructions: Generate n random numbers with values between 10 - 100. Note: n>9 if u write a function (for example, called generateRand) to do this - what is the data or input we have to give it? Create a...
For this assignment, write a program that will generate three randomly sized sets of random numbers...
For this assignment, write a program that will generate three randomly sized sets of random numbers using DEV C++ To use the random number generator, first add a #include statement for the cstdlib library to the top of the program: #include <cstdlib> Next, initialize the random number generator. This is done by calling the srand function and passing in an integer value (known as a seed value). This should only be done ONE time and it MUST be done before...
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100...
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100 and 300. Pick a number at random out of these 50 random variables. a. Determine the probability of the chosen number being greater than 200. This may be achieved by counting the numbers that are greater than 200 and dividing the count by 50. Make sure you, i.Formulate the appropriate if-conditions to check for a number being greater than 200 ii. Use a for-loop...
Write C++ program (submit the .cpp,.h, .sln and .vcxproj files) Problem 1. Generate 100 random numbers...
Write C++ program (submit the .cpp,.h, .sln and .vcxproj files) Problem 1. Generate 100 random numbers of the values 1-20 in an input.txt. Now create a binary search tree using the numbers of the sequence. The tree set should not have any nodes with same values and all repeated numbers of the random sequence must be stored in the node as a counter variable. For example, if there are five 20s’ in the random sequence then the tree node having...
Given n. Write a program in PYTHON to Generate all numbers with number of digits equal...
Given n. Write a program in PYTHON to Generate all numbers with number of digits equal to n, such that the digit to the right is greater than the left digit (ai+1 > ai). E.g. if n=3 (123,124,125,……129,234,…..789)
java please 1. Write a Java program to generate random numbers in the following range a....
java please 1. Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 2. Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 2 b) 1 ≤ n ≤ 100 c) 0 ≤ n ≤ 9 d) 1000 ≤...
Assuming you are write a program for the emergency room. You maintain two queues. A regular...
Assuming you are write a program for the emergency room. You maintain two queues. A regular FIFO queue for doctors. When a doctor walked in, he/she will join this queue if there is no patients waiting. Otherwise, remove the patient with the highest priority and let the doctor treat this patient. A priority queue for patients. When a patient walked, input patients name and priority. If doctor’s queue is not empty, remove the first doctor and let this doctor server...
Write a function that will generate an array of random numbers. It needs to:
DO IN C++Write a function that will generate an array of random numbers. It needs to:Take 3 integers as parameters-The first is the minimum value-the second is the maximum value-the third is the size of the new array-create a dynamically allocated array of the correct size-generate a random number (between min and max) for each element in the array-return a pointer to the arrayCreate a main() function that tests the above function and displays the values in the random array.
Part (a) Write a number guessing game using System.Collections.Generic.Dictionary. Generate 10 distinct random numbers in the...
Part (a) Write a number guessing game using System.Collections.Generic.Dictionary. Generate 10 distinct random numbers in the range of 1 to 20. Each random number is associated with a prize money (from 1 to 10000). Use a Dictionary to store the mapping between the random number and the prize money. Ask user for two distinct numbers, a and b, both from 1 to 20; if a and b are not distinct, or out of range, quit the program Lookup the prize...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT