226k views
0 votes
Devise an algorithm to scan an input array a of n elements, and output all prime numbers in the array that have a digit 7. For example, given array 27, 7, 13,17, 31, 37, where prime number are 7, 13, 17,31,37, the program should output 7, 17, 37.

Your solution should have a main algorithm and two sub-algorithms, let's call them prime(num) and has7(num). You need to trace your main algorithm and the has 7) algorithm. You should provide pre- and post-conditions for all your 3 (sub)algorithms. Note that since the sub-program has? is called several times, it's better that you use separate trace tables, one for each call.

Hint, in has 70), you may want to use a variable flag and a while loop so that you can terminate the loup early as long as a 7 is detected. Specifically, flag is initially false, and one of the while condition is that g is false. When a 7 is detected, set flag to be true. This will terminate the loop. Finally return flag

User Dontoo
by
8.3k points

1 Answer

6 votes

Final answer:

The algorithm determines and outputs prime numbers containing the digit 7 from an input array by utilizing two sub-algorithms: one to check for prime numbers and another to check for the digit 7.

Step-by-step explanation:

The problem at hand involves developing an algorithm to process an array of integers, identify prime numbers, and further filter those prime numbers that contain the digit 7. A main algorithm will call upon two sub-algorithms: prime(num) to check if a number is prime, and has7(num) to check if the number contains the digit 7.



Main Algorithm

If the number is prime, call the has7(num) sub-algorithm.

If has7(num) returns true, output the number.



Sub-Algorithm: prime(num)

Pre-condition: num is an integer. Post-condition: Returns true if num is a prime number; otherwise, returns false.



Sub-Algorithm: has7(num)

Pre-condition: num is an integer. Post-condition: Returns true if num contains the digit 7; otherwise, returns false.



To trace the has7(num) algorithm, create a separate trace table for each call to record the execution flow and determine the output for different input numbers. The main algorithm loop checks both conditions using the specified sub-algorithms before outputting the qualifying prime numbers with the digit 7.

User Mark Mcmurray
by
8.3k points