In: Computer Science
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++
#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;
}