Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Task A: 1. New Project and give your project the name Lab4A. 2. Add a source file to your project, called FindPrimeNumbers.cpp. 3. C++ program

Task A: 1. New Project and give your project the name Lab4A. 2. Add a source file to your project, called FindPrimeNumbers.cpp. 3. C++ program that uses an array of n elements (where n is a user input) to determine and display the prime numbers between 2 and n, including n using the method described below. Ignore array elements 0 and 1. A prime number is any integer greater than one that is divisible only by itself and 1. A list of prime numbers can be found by the following method. a. primitive type bool array with all elements initialized to true. Array elements with prime indices will remain true. All other array elements will eventually be set to false. b. Starting with array index 2, determine whether a given element is true. If so, loop through the remaining of the array and set to false every element whose index is a multiple of the index for the element with value true. c. Then continue the process with the next element with value true. For array index 2, all elements beyond element 2 in the array that have indices which are multiples of 2 (indices 4, 6, 8, 10, etc.) will be set to false; for array index 3, all elements beyond element 3 in the array that have indices which are multiples of 3 (indices 6, 9, 12, 15, etc.) will be set to false; and so on. d. When this process completes, the array elements that are still true indicate that their indices are prime numbers. These indices can be displayed. Multiple sessions of sample screen display when the method is called are given below: Enter the value of n: 10 Prime numbers: 2, 3, 5, 7 4 primes found. Enter the value of n: 60 Prime numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59 17 primes found. Enter the value of n: 100 Prime numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 25 primes found. The input which is underlined is the user's input to a question. (Note: you don't have to repeatedly execute your program code using loop for this assignment.) 4. Compile your program and test it by executing your program.

image text in transcribed
Task A: 1. Create a New Project and give your project the name Lab4A. 2. Add a source file to your project, called FindPrimeNumbers.cpp. 3. Write a C++ program that uses an array of n elements (where n is a user input) to determine and display the prime numbers between 2 and n, including n using the method described below. Ignore array elements 0 and 1. A prime number is any integer greater than one that is divisible only by itself and 1. A list of prime numbers can be found by the following method. a. Create a primitive type bool array with all elements initialized to true. Array elements with prime indices will remain true. All other array elements will eventually be set to false. b. Starting with array index 2, determine whether a given element is true. If so, loop through the remaining of the array and set to false every clement whose index is a multiple of the index for the clement with value truc. c. Then continue the process with the next element with value true. For array index 2, all elements beyond element 2 in the array that have indices which are multiples of 2 (indices 4, 6, 8, 10, etc.) will be set to false; for array index 3, all clements beyond clement 3 in the array that have indices which are multiples of 3 (indices 6, 9, 12, 15, etc.) will be set to false; and so on. d. When this process completes, the array elements that are still true indicate that their indices are prime numbers. These indices can be displayed. Multiple sessions of sample screen display when the method is called are given below: Enter the value of n: 10 Prime numbers: 2, 3, 5, 7 4 primes found. Enter the value of n: 60 Prime numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59 17 primes found. Enter the value of n: 100 Prime numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 25 primes found. The input which is underlined is the user's input to a question. (Note: you don't have to repeatedly execute your program code using loop for this assignment.) 4. Compile your program and test it by executing your program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

ET575 C++ nested loops algorithms

Answered: 1 week ago