Starting from:

$25

COMP1400-Lab 8 Prime Factors Solved

Objective: Working with functions 

 

Prime Factors: Any integer above 1 is either a Prime Number, or can be made by multiplying Prime Numbers together. For example:

16  = 2 x 2 x 2 x 2 = (2^4)

17  = (17^1)

18  = 2 x 3 x 3 = (2^1) x (3^2)

After reading an integer number from the user, named num, and given the first prime number 2, your program logic will: 

a.   Determine and display how many times this prime number will occur in num. After finding each prime number, the value of num should be updated by dividing num to the found prime number. For example, after looking for the first prime number of 2 in the input num of 12, num will change to 3 (12 / (2x2)).

b.   Then the program will determine what is the next prime number, and go back to step a. 

c.   Steps a. and b. will continue until the value of num is equal to 1. 

Note: Your program should implement at least the following 3 functions (procedures):

  

1)  readNum()to take a valid number (greater than 1) from the user. This function has no input and returns a valid integer number entered by the user.   

2)  IsPrime(…) to check if a number is a prime. This function has one integer variable as an input, named prime, and a boolean variable as an output, named p_flag.

3)  findPrimeCount(…) to count the number of a given prime in the input number and display the result in the format of (prime^freq). This function has two integer variables of num and prime as an input and returns the updated num. For example, calling findPrimeCount(12,2)returns the updated number of 3. Hint: The attached “findPrimeCount.png” provides a solution for this function.

 

A Sample interaction is as follow:

 

   Enter a valid number (1):
18
  18 = 1 x (2^1) x (3^3)

  Enter a valid number (1):
 

0
  Invalid number.

  Enter a valid number (1):

  19 = 1 x (19^1)
 

19

 
 

Part A: RAPTOR Exercise

a)    Understand more about RAPTOR by watching the video about Creating Procedures/Functions.

b)    Start RAPTOR and create the flowchart of Prime Factors.

c)     Save the flowchart to a file named “primeFactor.rap” in the working directory on the PC you are using.

d)    Demonstrate the primeFactor.rap file to GA/TAs and run with different input values. 

Hint: The mode of your Raptor should be “Intermediate” to be able to add a procedure.  

 

Part B: C Programming Exercise

a)    Implement the algorithm as represented by “primeFactor.rap”, and write an equivalent C program that accomplishes what the flowchart does.

b)    Save your program to a file named “primeFactor.c” in the working directory on the PC you are using. 

c)     Demonstrate the primeFactor.c file to GA/TAs and run with different input values.

 

More products