Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The followingfunction named check_prime is supposed to check andreturn true if i

ID: 3613289 • Letter: T

Question

The followingfunction named check_primeis supposed to check

andreturn trueif its argument is greater than theinteger 2and is

a primenumber. The function returns falseotherwise. The code

for check_prime compiles butis not logically correct. Which of the

followinganswers correctly fixes this code?

(NOTE: Aprime number is a positive integer that isonly

evenlydivisible by itself and the integer1.)

bool check_prime(int num)

{

(1) int i(2);

(2) if( num < 2) return false;

(3) while(i < num) {

(4) if(num % i) return false;

(5) i++;

(6) };

(7) return true;

}

a) Change line (1) to int i(1);

b) Change line (2) to if( num <= 2) returnfalse;

and change line (3) to while(i <= num){

c) Change line (4) to if(!num % i) returntrue;

and change line (7) to return false;

d) Change line (4) to if(!(num % i) ) returntrue;

and change line(2) to if( num <= 2) returnfalse;

e)None of the above will fix the function.

Explanation / Answer

The followingfunction named check_primeis supposed to check

andreturn trueif its argument is greater than theinteger 2and is

a primenumber. The function returns falseotherwise. The code

for check_prime compiles butis not logically correct. Which of the

followinganswers correctly fixes this code?

(NOTE: Aprime number is a positive integer that isonly

evenlydivisible by itself and the integer1.)

bool check_prime(int num)

{

(1) int i(2);

(2) if( num < 2) return false;

(3) while(i < num) {

(4) if(num % i) return false;

(5) i++;

(6) };

(7) return true;

}

a) Change line (1) to int i(1);

b) Change line (2) to if( num <= 2) returnfalse;

and change line (3) to while(i <= num){

c) Change line (4) to if(!num % i) returntrue;

and change line (7) to return false;

d) Change line (4) to if(!(num % i) )return true;

and change line(2) to if( num <= 2)return false;

e)None of the above will fix the function.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote