Question

In: Computer Science

**I need to make this program to make sure that the numbers are unique. In another...

**I need to make this program to make sure that the numbers are unique. In another word, if the number has been generated and appear early, it should continue to find the next number until the number is unique.

Hint: use the array to check whether the number has been generated.

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main() {

srand(time(NULL));

int n, low, high;

cout << "Enter how many random numbers you would like to see: ";

cin >> n;

cout << "Enter the lower limit now: ";

cin >> low;

cout << "And now enter the higher limit: ";

cin >> high;

cout << n << " random numbers:";

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

cout << " " << low + (rand() % (high-low+1)) << endl;

}

cout << endl;

return 0;

}

Please show me how it can be done and explain code needed if possible. Thanks!

Solutions

Expert Solution

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
srand(time(NULL));
int n, low, high;
cout << "Enter how many random numbers you would like to see: ";
cin >> n;

int *arr = new int[n];

cout << "Enter the lower limit now: ";
cin >> low;

cout << "And now enter the higher limit: ";
cin >> high;

int r, k = 0;
bool found;
for(int i = 0;k<n;i++){
   found = false;
   r = low + (rand() % (high-low+1));
   
   // Checking whether new random value r already exists in the array arr
   for(int j = 0;j<k;j++){
      if(arr[j]==r){
         found=true;
         break;
      }
   }
   
   // If not found then adding r to arr
   if(!found){
      arr[k++] = r;
   }
}

// Printing the random array
cout << n << " random numbers:";
for (int i = 0; i < n; ++i) {
   cout << " " << arr[i] << endl;

}
cout << endl;
return 0;
}


Related Solutions

*****NOTE: I DO NOT NEED A DRIVER FILE. I NEED mixed.h AND mixed.cpp********* *****PLEASE MAKE SURE...
*****NOTE: I DO NOT NEED A DRIVER FILE. I NEED mixed.h AND mixed.cpp********* *****PLEASE MAKE SURE THAT mixed.h AND mixed.cpp WORK WITH THE DRIVER FILE PROVIDED******************* Objective: Upon completion of this program, you should gain experience with overloading basic operators for use with a C++ class. The code for this assignment should be portable -- make sure you test with g++ on linprog.cs.fsu.edu before you submit. Task Create a class called Mixed. Objects of type Mixed will store and manage...
I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
Make a java program of Mickey I have the starter program but I need to add...
Make a java program of Mickey I have the starter program but I need to add eyes and a smile to it. import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import javax.swing.JFrame; public class Mickey extends Canvas { public static void main(String[] args) { JFrame frame = new JFrame("Mickey Mouse"); Canvas canvas = new Mickey(); canvas.setSize(400, 400); canvas.setBackground(Color.white); frame.add(canvas); frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { Rectangle bb = new Rectangle(100, 100, 200, 200); mickey(g, bb); } public void...
I just need to understand Question 1 and 2. I just wanted to make sure I...
I just need to understand Question 1 and 2. I just wanted to make sure I did the revenue management right. Freedom Airlines recently started operations in the Southwest. The airline owns two airplanes, one based in Phoenix and the other in Denver. Each airplane has a coach section with 140 seats available. Each afternoon, the Phoenix based airplane flies to San Francisco with stopovers in Las Vegas and in San Diego. The Denver-based airplane also flies to San Francisco...
I need a NPV calculation. I just want to make sure I got it correct. Initial...
I need a NPV calculation. I just want to make sure I got it correct. Initial Cost at t=0 is $150 rate = 16% growth rate in perpetuity after year 5 at 1.5% Year 1 2 3 4 5 Cash Flow 5 11.4 14 21 28
I just need to understand Question 1 and 2. I just wanted to make sure I...
I just need to understand Question 1 and 2. I just wanted to make sure I did the revenue management right. Freedom Airlines recently started operations in the Southwest. The airline owns two airplanes, one based in Phoenix and the other in Denver. Each airplane has a coach section with 140 seats available. Each afternoon, the Phoenix based airplane flies to San Francisco with stopovers in Las Vegas and in San Diego. The Denver-based airplane also flies to San Francisco...
I have placed my answers in bold I just need to make sure these are right...
I have placed my answers in bold I just need to make sure these are right I have some with two answers because I'm just not sure which is correct. I just really want to do well on this last assignment any help is apperciated. Thank you! Abby is a 20-year-old female college student. For at least the last 3 months, Abby has experienced ongoing anxiety and worry without a specific cause for these feelings. She has been restless and...
Using java, I need to make a program that reverses a file. The program will read...
Using java, I need to make a program that reverses a file. The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order) EX: Hello World                       eyB       Bye            becomes    dlroW olleH I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and...
I need to make sure that my answers are correct please review. The Case as following:...
I need to make sure that my answers are correct please review. The Case as following: Focus Drilling Supplies has been growing steadily over the last 20 years. With increased exploration in the mining sector, the company has decided to expand their facilities for supplies and custom drill bit production to meet the increased demand. The expansion will occur over 4 years and is expected to require $2.8 million. Management has developed a payment plan for carrying out this expansion....
I need to make JAVA program that calculates the area of a brick of a specific...
I need to make JAVA program that calculates the area of a brick of a specific color. I have class Brick with the following UML: String color int number int length int width I made methods: getColor(), getNumber(), getLength(), getWidth() in the class Brick So it starts like this: ##### public Brick(String color, int number, int length, int width) { this.color = color; this.number = number; this.length = length; this.width = width;    }   public String toString() {   return number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT