Question

Write a c++ code into the given code  to find composite numbers from the given random number...

Write a c++ code into the given code  to find composite numbers from the given random number list. The composite numbers is only counted once if there is a repeated number. I need to use this code and add on a code to find if the numbers generated is a composite function. Please help

#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>

using namespace std;

int main() {

   srand(time(NULL));
int size_of_list = 0; // the number of random numbers to be generated
   int count = 0; // counter to count the number of composite numbers

while (size_of_list <= 0) {
       cout << "How many randome numbers are to be generated?" << endl;
       cin >> size_of_list;
   }

   vector <long> list(size_of_list); // the list of random numbers
   vector <bool> counted(size_of_list, false);

   cout << endl << "The random numbers are" << endl << endl;
   for(int i=0; i<size_of_list; ++i) {
       list.at(i) = rand() % 100 + 1;
       cout << list.at(i) << " ";
   }


}

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

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You.

main.cpp

#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>

using namespace std;


bool isCompositeNumber(long num){
for(int i=2;i<num;i++){
if(num%i==0){
return true;
}
}
return false;
}
int main() {

srand(time(NULL));
int size_of_list = 0; // the number of random numbers to be generated
int count = 0; // counter to count the number of composite numbers

while (size_of_list <= 0) {
cout << "How many randome numbers are to be generated?" << endl;
cin >> size_of_list;
}

vector <long> list(size_of_list); // the list of random numbers
vector <long> compositeList; //list of composite numbers

cout << endl << "The random numbers are" << endl << endl;
for(int i=0; i<size_of_list; ++i) {
list.at(i) = rand() % 100 + 1;
cout << list.at(i) << " ";
}

cout << endl << "Calculating composite numbers..." << endl << endl;
int num;
bool found= false;
for(int i=0; i<size_of_list; ++i) {
num = list.at(i);
if(isCompositeNumber(num)){

found= false;
//now check if number already added or not
for(unsigned int i=0; i<compositeList.size(); ++i) {
if(num==compositeList[i]){
found = true;
break;
}
}

//if not found add number of vector
if(!found)
compositeList.push_back(num);
}
}
cout << endl << "The composite numbers are" << endl << endl;
for(unsigned int i=0; i<compositeList.size(); ++i) {
cout << compositeList.at(i) << " ";
}


}

E.txt Console x <terminated> cppProject Debug (C/C++ Application] C:\Users\Mohammad Shahrukh\Desktop\ How many randome number

Add a comment
Know the answer?
Add Answer to:
Write a c++ code into the given code  to find composite numbers from the given random number...
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
  • Hello, I am working on a C++ pick 5 lottery game that gives you the option...

    Hello, I am working on a C++ pick 5 lottery game that gives you the option to play over and over. I have everything working right except that every time the game runs it generates the same winning numbers. I know this is an srand or rand problem, (at least I think it is), but I can't figure out what my mistake is. I've tried moving srand out of the loop, but I'm still getting the same random numbers every...

  • Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write...

    Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....

  • I am trying to write this code which asks "Write a program that ask the user,...

    I am trying to write this code which asks "Write a program that ask the user, the question: What is a^b? //The program generates two signed integers and gives the user four attempts to get the correct answer //a=- , b = + //a= + , b = - " So far this what I wrote. I am not sure how to do this correctly. #include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main() {    srand(time(0));    int guess,a,ans,b, k;...

  • How can I write this code (posted below) using vectors instead of arrays? This is the...

    How can I write this code (posted below) using vectors instead of arrays? This is the task I have and the code below is for Task 1.3: Generate twenty random permutations of the number 0, 1, 2, ..., 9 using of the algorithms you designed for Task 1.3. Store these permutations into a vector and print them to the screen with the additional information of unchanged positions and number of calls torand(). Calculate the total numbers of unchanged positions. Compare...

  • Find and fix errors #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int MIN...

    Find and fix errors #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int MIN = 1; const int MAX = 10; int getRandom(int low, int high); int main() {    int random_num = 0; int player_num; int tries; int seed = static_cast<int>(time(0)); bool guessed = false;    srand(seed); // call the getRandom function below       tries = 4; while ( tries > 0 && !guessed ) { cout << "Enter a number within the range 1 to...

  • How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0,...

    How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return n; } return fibonacciRecursive(n-1) + fibonacciRecursive(n-2); } int main() { int n;...

  • Help with C++ reverse program with leading zeros. I need to put the line from the...

    Help with C++ reverse program with leading zeros. I need to put the line from the function that display the zeros in main not in the function. So how can I move the display with leading zeros in main. Thanks. Here is my code. #include <iostream> #include <cstdlib> #include <iostream> using std::cout; using std::cin; using std::endl; //function templates int reverseNum(int); int main() { //variables char buf[100]; int num; while (true) { //prompt user for input cout << "Enter the number...

  • #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code...

    #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code here } void fill(int arr[], int count){ for(int i = 0; i < count; i++){ cout << "Enter number: "; cin >> arr[i]; } } void display(int arr[], int count){ for(int i = 0; i < count; i++){ cout << arr[i] << endl; } } int main() { cout << "How many items: "; int count; cin >> count; int * arr = new...

  • What are the errors in the following code? My professor gave us this prewritten code and...

    What are the errors in the following code? My professor gave us this prewritten code and asked us to find the errors in it so that it can run properly #include <cstdlib> #include <ctime> #include <iostream> using namespace std; // input: integer // output: none // adds five to the given parameter void addFive( int x ) { x += 5; } // input: none // output: a random number int generateRandomNumber() { srand( time(0) ); return rand() % 100;...

  • You are given a sample code that when edited/ improved/completed -will work as a producer consumer...

    You are given a sample code that when edited/ improved/completed -will work as a producer consumer synchronized solution. In addition to the counter, you will implement a circular linked list as a buffer of size 200 that stores data items 0 or 1. The producer and consumer take *random* turns to produce and consume- *random* numbers of elements ( turning 1 to a 0 and visa-versa-each time, as discussed in class). After each turn of the producer and/or consumer, your...

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