Question

In: Computer Science

For this computer assignment, you are to write and implement an interactive C++ program to find...

For this computer assignment, you are to write and implement an interactive C++ program to find and print all prime numbers, which are less than or equal to a given value of n, using the algorithm known as the Sieve of Eratosthenes. A prime number p is an integer greater than 1 that is divisible only by 1 and p (itself). The algorithm begins by initializing a set container to contain all the integers in the range 2 to n. A loop makes multiple passes over the elements in the set, using successive integer key values 2, 3, 4, … Each pass “shakes free” nonprime numbers and lets them “filter through the sieve.” At the end, only the prime numbers remain. Begin with the integer m = 2, which is the smallest prime number. The pass scans the set and removes all multiples of 2, having the form 2 * k for k >= 2. The multiples cannot be prime numbers, because they are divisible by 2. At the end of the pass, we have removed all even numbers except 2. Next, look at the integer m = 3, which is a prime number. As with value 2, remove all multiples of 3, having the form 3 * k for k >= 3. The multiples 12, 18, and so forth, are even numbers, so they have already been removed from the set. The next key integer is m = 4, which is no longer in the set, because it was removed as a multiple of 2. The pass takes no action. The process continues until it removes all multiples of prime numbers. In other words, for integer m, remove all multiples of m, having the form m * k for k >= m.

The numbers that remain in the sieve are the prime numbers in the range 2 to n. The algorithm uses an optimization feature by looking at the key values for m <= sqrt ( n ). However, in your implementation, test all numbers m such that m * m <= n, rather than computing the square root of n.

Programming Notes:

1. Use a set container to store the prime numbers. In the STL, a set is implemented as an associative container, it uses the model of a mathematical set, it stores keys that are objects of a specified data type, where duplicate keys are not allowed. To use a set in your program, you need to insert the statement: #include in your header file.

2. In addition to the main ( ) routine, implement (at least) the following two subroutines in your program: • void sieve ( set < int >& s, int n ): This routine can be used to apply the Sieve of Eratosthenes algorithm to remove all nonprime numbers from the integer set s = { 2, 3, …, n }. • void print_primes ( const set < int >& s ): This routine can be used to print the elements in the integer set s (NO_ITEMS = 16 primes per line and ITEM_W = 4 spaces allocated for each prime). Sieve of Eratosthenes 2

3. The main ( ) routine creates an empty set of integers and prompts the user for an upper limit and calls the subroutine sieve ( ) to execute the Sieve of Eratosthenes. It calls the subroutine print_primes ( ) to print the prime numbers on stdout.

4. Include your header file prog3.h, by inserting the statement: #include “prog3.h” at the top of your source file prog3.cc.

ALSO PLEASE ATTACH AN HEADER FILE

Solutions

Expert Solution

Code:

untitled1.cpp

#include<iostream>
#include<set>
#include "untitled1.h"
using namespace std;
int main()
{
   int n;
   cout << "Enter n: " ;cin >> n;
   set < int > s;
   for(int i = 2;i < n;i++) s.insert(i);//inserting all numbers from 2 to n-1
   sieve(s, n);//calling sieve function
   print_primes(s);//
   return 0;
}
untitled1.h

#include<iostream>
#include<set>
void sieve( std::set < int >& s, int n)
{
   for(int i = 2; i * i <= n; i++)//iterate upto an element less than sqrt(n)
   {
       for(int j = 2; j * i < n;j++)//j*i stores all the multiples of i which are less than n
       {
           s.erase(j*i);//removing j*i from the set
       }
   }
}
void print_primes ( const std::set < int >& s )
{
   std::cout << "Prime numbers:" << std::endl;
   int cnt = 0;
   for( std::set < int > :: iterator it = s.begin(); it != s.end(); it++)
   {
       cnt++;
       std::cout << *it << " ";
       if(cnt == 16)
       {
           std::cout << std::endl;
           cnt = 0;
       }
   }
   std::cout << std::endl;
}

Output:


Related Solutions

For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
In this programming assignment, you will implement a SimpleWebGet program for non- interactive download of files...
In this programming assignment, you will implement a SimpleWebGet program for non- interactive download of files from the Internet. This program is very similar to wget utility in Unix/Linux environment.The synopsis of SimpleWebGet is: java SimpleWebGet URL. The URL could be either a valid link on the Internet, e.g., www.asu.edu/index.html, or gaia.cs.umass.edu/wireshark-labs/alice.txt or an invalid link, e.g., www.asu.edu/inde.html. ww.asu.edu/inde.html. The output of SimpleWebGet for valid links should be the same as wget utility in Linux, except the progress line highlighted...
Assignment: Write an interactive C++·program to determine a type of a triangle based on three sides....
Assignment: Write an interactive C++·program to determine a type of a triangle based on three sides. Have a user input the length of three sides of a triangle. Allow the user to enter the sides in any order. Determine if the entered sides form a valid triangle. The triangle may not have negative sides. The sum of any two sides must be greater than the third side to form a triangle. Determine if the entered sides are part of a...
Write and test a C program to implement Bubble Sort. . In your C program, you...
Write and test a C program to implement Bubble Sort. . In your C program, you should do: Implement the array use an integer pointer, get the size of the array from standard input and use the malloc function to allocate the required memory for it. Read the array elements from standard input. Print out the sorted array, and don’t forget to free the memory. Debug your program using Eclipse C/C++ CDT.
Write a C program with the following prompt *do not use gotostatement* "Write an interactive...
Write a C program with the following prompt *do not use goto statement* "Write an interactive program that implements a simple calculator. The program should allow the user to choose a binary arithmetic operation and enter two terms to which to apply the operation. The program should then compute the result and display it to the user. Your calculator will have two modes: double-precision mode and integer mode. Double-precision mode will do calculations and output using variables of type double....
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement:...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement: The program will read from standard input two things - a string str1 on the first line of stdin (this string may be an empty string) - a string str2 on the second line of stdin (this string may be an empty string) Note that stdin does not end with '\n'. The program will output a string that is the concatenation of string str1...
In C++ For this assignment, you will write a program to count the number of times...
In C++ For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array...
For this week’s lab assignment, you will write a program called lab9.c. You will write a...
For this week’s lab assignment, you will write a program called lab9.c. You will write a program so that it contains two functions, one for each conversion. The program will work the same way and will produce the same exact output. The two prototypes should be the following: int btod(int size, char inputBin[size]); int dtob(int inputDec); The algorithm for the main() function should be the following: 1. Declare needed variables 2. Prompt user to enter a binary number 3. Use...
Java/Python/C++ Assignment Write a program to implement one round of AES-128 Input is the key and...
Java/Python/C++ Assignment Write a program to implement one round of AES-128 Input is the key and plaintext a. Show the plaintext as a 4x4 matrix b. Show the result after adding RoundKey0 c. Show the result after SubBytes d. Show the result after ShiftRows e. Show the result after MixColumns f. Show the result after first round
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT