In: Computer Science
HI, I hope you are doing well. I really don't understand this question and don't know how to solve it at all because I am completely new to this c++ programming. can you please explain each line of code with long and clear comments? please think of me as someone who doesn't know to code at all. and I want this code to be written in c++
thank you very much and I will make sure to leave thumbs up.
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.
C++ Code
#include <iostream>
#include<queue>
using namespace std;
void show_queue(queue <int> q)
{
while(!q.empty())
{
cout<<q.front()<<" ";
q.pop();
}
cout<<"\n";
}
int digit_sum(int n)
{
int sum = 0;
while(n>0)
{
sum += (n%10);
n/=10;
}
return sum;
}
int main()
{
int n , arr[10000],i;
queue <int> q1,q2;
cout<<"Enter the value of n : ";
cin>>n;
cout<<"Random generated numbers are: ";
for(i=0;i<n;i++)
{
arr[i] = (rand()%(100-10+1))+10;
cout<<arr[i]<<" ";
q1.push(arr[i]);
}
for(i=0;i<n;i++)
{
q1.pop();
if(digit_sum(arr[i])%2!=0)
{
q2.push(arr[i]);
}
else
{
q1.push(arr[i]);
}
}
cout<<"\nqueue 1 elements : ";
show_queue(q1);
cout<<"\nqueue 2 elements : ";
show_queue(q2);
return 0;
}
ask if u have any query