Question

Generate N>10 random values from 1 to 100 using a uniform probability distribution. N is entered by the user. The first 5 valCan anyone help me write a simple code for this problem in C++?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code is given below. The explanation has been comment out through out the code. If still you have any doubts please let me know.

Code:

#include<iostream>

#include<vector>

#include<queue>

#include<random>

using namespace std;

//a class that will store the key and a list of its childs

class myNode{

public:

int key;

vector<int> values;

myNode(){

}

myNode(int k){

key = k;

}

//add value to vector

void pushValue(int val){

values.push_back(val);

}

};

//comparator

class compar {

public:

bool operator() (myNode* const& lhs, myNode* const& rhs) {

return lhs->key > rhs->key;

}

};

int main(){

//create a priority queue that store class myNode

priority_queue<myNode *, vector<myNode*>, compar> PQueue;

//get n

int n;

cout<<"Please enter the value of n(Greater than 10): ";

cin>>n;

//check if n is greate than 10 . if it is not then exit

if(n <= 10){

cout<<"Entered values is not greater than 10\n";

return 0;

}

//create randomised numbers;

int arr[n];

//seed random value generator

srand(time(0));

for(int i = 0; i < n; i++){

arr[i] = 1 + rand()%100; //number from 1 to 100

}

//insert first five elements in PQ

//create arr of five nodes

myNode mn[5];

for(int i = 0; i < 5; i++){

mn[i] = myNode(arr[i]);

PQueue.push(&mn[i]);

}

//create a second priority quese to store the nodes

priority_queue<myNode* , vector<myNode*>, compar> PQueue2;

//insert remainig items

for(int i = 5; i < n; i++){

//find node

while(!PQueue.empty() && PQueue.top()->key < arr[i]){

//insert in second priority queue

PQueue2.push(PQueue.top());

PQueue.pop();

}

//if PQueue is not empty and the key to the top node

if(!PQueue.empty()){

PQueue.top()->pushValue(arr[i]);

}

//Add values back to PQueue

while(!PQueue2.empty()){

//insert in second priority queue

PQueue.push(PQueue2.top());

PQueue2.pop();

}

}

//Display all the nodes;

for(int i = 0; i < 5; i++){

//get minimum node

myNode t = *PQueue.top();

//print the key

cout<<"Node "<<t.key<<" has ";

//if empty vector then print NO values

if(t.values.size() == 0){

cout<<"NO values\n";

}

else{

//print all values.

cout<<"values ";

for(auto k : t.values){

cout<<k<<",";

}

cout<<endl;

}

//remove this element

PQueue.pop();

}

}

Code Screenshot:

Output:

Add a comment
Know the answer?
Add Answer to:
Can anyone help me write a simple code for this problem in C++? Generate N>10 random...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Code in C++. Can someone make it so that the code below can be compiled? ▪...

    Code in C++. Can someone make it so that the code below can be compiled? ▪ Creating an empty queue ▪ Inserting a value ▪ Removing a value ▪ Finding the size of the queue ▪ Printing the contents of the queue ▪ Adding the contents of one queue to the end of another ▪ Merging the contents of two queues into a third, new, queue Class Attributes Your class should be implemented using a linked list and should have...

  • C++. Please note the BOLDED ITEMS. You will create a simple linked structure. You will use...

    C++. Please note the BOLDED ITEMS. You will create a simple linked structure. You will use a simple node that has a pointer to a Node. The data for the Node will be a single data member of type char. You will build a structure where the last node you add will point to the first node created, i.e. it will be a circular linked structure. You will create a program that stores characters provided by the user, stored in...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • CÖ) Could you please help me to solve this problem?(ONLY USING C++ PROGRAMMING LANGUAGE PLEASE) Write...

    CÖ) Could you please help me to solve this problem?(ONLY USING C++ PROGRAMMING LANGUAGE PLEASE) Write a program for the patient information system in a policlinic. The patients are examined according to the triage char assigned to them. Triage codes are as follows: - ‘h’ : high priority - ‘m’ : medium priority - ‘l’ : low priority The patients having ‘h’ triage char are examined first then the patients having ‘m’ comes and finally patients with ‘l’ triage code...

  • C++ Vectors and Binary Search Trees • Write a program that takes from the user n...

    C++ Vectors and Binary Search Trees • Write a program that takes from the user n integers and stores them a vector of int. Then, create a function insert After that takes first Value and second Value. This function searches for each occurrence of first Value in the vector and insert the second Value after it in the same vector. The first and second values are taken from the user. • Create another function that creates a Binary Search Tree...

  • The ExceptionLab class provided: – Creates an array of 100 elements and fills it with random...

    The ExceptionLab class provided: – Creates an array of 100 elements and fills it with random numbers from 1 to 100. – It asks the user for an index value between 0 and 99. – Prints the element at that position. – If a number > 99 is entered by the user, the class will abort with an ArrayIndexOutOfBoundsException • Modify the ExceptionLab: – Add a try-catch clause which intercepts the ArrayIndexOutOfBounds and prints the message: Index value cannot be...

  • Can you please help with the below? 1)   Which of the following is true about using...

    Can you please help with the below? 1)   Which of the following is true about using a 2-3-4 tree? a.   It is designed to minimize node visits while keeping to an O(log n) search performance b.   It is designed to self-balance as new values are inserted into the tree c.   As soon as a node becomes full, it performs the split routine d.   None of the above 2)   Which of the following is true about a binary search tree? a.  ...

  • Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue....

    Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue. You will need to think about the differences in the Stack and Queue. Think about how they operate and how the nodes may differ.   Modify the code as necessary. You will need to push to the Stack, pop from the Stack, peek at the Stack. You will need a member functions like in the example code. Your program will need to show that everything...

  • Can anyone help me with my C hw? Exercise 3 You will write a new program...

    Can anyone help me with my C hw? Exercise 3 You will write a new program that combines dynamically allocating an array and saving that array to a file. These are the tasks your program must perform Open an output file named "data.txt" and prepare it for writing in text mode o If the file handle is NULL, quit the program o By default, it is created and stored in the same directory as your source code file Prompt the...

  • For this assignment, write a program that will generate random numbers in the range of 50-100...

    For this assignment, write a program that will generate random numbers in the range of 50-100 and count how many fall into particular ranges. Processing: The program should first seed the random number generator. This is done one time in the program. Use srand(0). Next, generate and display a series of 10 random numbers between 50 and 100. There are several ways to ensure that a random number falls between 50 and 100. One possibility is to use the following...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT