Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

USING C++ LANGUAGE. Please follow given knowledge and given example of output To convert an algorithm to code in your language of choice. An algorithm

USING C++ LANGUAGE. Please follow given knowledge and given example of output

image text in transcribedimage text in transcribed

To convert an algorithm to code in your language of choice. An algorithm is a finite sequence of steps to solve a problem. Algorithms should not be expressed in a specific programming language, but can use common language constructs, such as if, switch, for, while, etc. Here is an algorithm to print the prime factorization of a number. Because prime numbers are defined as positive integers greater than 1 that are divisible only by 1 and themselves, we must restrict the values we use. The algorithm describes reading the number, checking for a valid number, and then printing out the prime factors. - Read in an integer num - If num is negative, print an error message and exit - Set k equal to num - for i=2 to the square root of num - While the k modulo i equals zero - print out i - divide k by i and save the result back k - if k is greater than one, print k Here I've used very English like language. Often you will see more programming language like expressions called pseudocode. YOUR JOB Convert the algorithm above into C,C++, or Java. Add a short menu to see if someone wants to print the prime factorization for another number. Here is a pseudocode representation what that might look like: repeat print "Please enter an integer to determine the prime factorization of: " read into num if ( num 1) print k print "Factor another number?" read into answer until answer == no As you can see, psuedocode is closer to a programming language, but may have some constructs that your language doesn't have (such as a repeat-until... which is the same as a do-while). In addition, we still have a bit to do. We haven't declared variables, we also haven't included any external I/O libraries. We also didn't really say how "no" should be entered... a character, a number, a string... We have decisions to make in our implementation, but that doesn't change the algorithm. Sample output: This program prints the prime factorization of positive integers Please enter a positive integer for prime factorization: 8 The prime factorization of 8 is: 2,2,2 Would you like to try another number (N for no, anything else is yes): y Please enter a positive integer for prime factorization: 121 The prime factorization of 121 is: 11,11 Would you like to try another number ( N for no, anything else is yes): y Please enter a positive integer for prime factorization: 5003 The prime factorization of 5003 is: 5003 Would you like to try another number (N for no, anything else is yes): y Please enter a positive integer for prime factorization: 12345678 The prime factorization of 12345678 is: 2,3,3,47,14593 Would you like to try another number ( N for no, anything else is yes): n

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

Systems Analysis And Synthesis Bridging Computer Science And Information Technology

Authors: Barry Dwyer

1st Edition

0128054492, 9780128054499

More Books

Students also viewed these Databases questions

Question

3. Is IBMs program really a mentoring program? Why or why not?

Answered: 1 week ago