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
Write a program to simulate a calculator. Generate two random numbers (between 1-15) and an ask...
Write a program to simulate a calculator. Generate two random numbers (between 1-15) and an ask the user for an arithmetic operator. Using a switch statement, your program has to perform the arithmetic operation on those two operands depending on what operator the user entered. Ask the user if he/she wants to repeat the calculations and if the answer is yes or YES, keep repeating. If the answer is something else, then stop after the first calculation. c++
Write a C++ program to generate two random numbers (Rnd1 and Rnd2). These two numbers should...
Write a C++ program to generate two random numbers (Rnd1 and Rnd2). These two numbers should be within a range [100, 500]. If Rnd1 greater than or equals to Rnd2, print out the result of (Rnd1-Rnd2). Otherwise, print out the result of (Rnd2-Rnd1). In all cases, print Rnd1, Rnd2, and the results of subtractions in suitable messages.
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...
Write a simple Python program that will generate two random between 1 and 6 ( 1...
Write a simple Python program that will generate two random between 1 and 6 ( 1 and 6 are included). If the sum of the number is grader or equal to 10 programs will display “ YOU WON!!!”. if the sum is less than 10, it will display “YOU LOST”. After this massage program will ask users a question: “if you want to play again enter Y. if you want to exit enter ‘N’) If the user enters Y they...
Write a simple Python program that will generate two random between 1 and 6 ( 1...
Write a simple Python program that will generate two random between 1 and 6 ( 1 and 6 are included). If the sum of the number is grader or equal to 10 programs will display “ YOU WON!!!”. if the sum is less than 10, it will display “YOU LOST”. After this massage program will ask users a question: “if you want to play again enter Y. if you want to exit enter ‘N’) If the user enters Y they...
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)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT