Homework 10: More Functions

Due: Tuesday 11/15 by 1pm

Objectives

More practice implementing functions.

Assignment

This assignment has two parts. In Part 1, you implement a function that checks if a number is prime. In Part 2, your function finds and returns a prime number greater than a given number. In both parts, you are given a file to modify. The files have a main program that you are not allowed to change.

Note, you must properly indent your programs. This is very important, because poorly indented programs are hard to read and hard to debug. Follow one of the styles listed in CMSC104 Indentation Styles. We will also deduct points from programs that do not have enough comments.

Part 1

In Part 1, your assignment is to implement a function, called isPrime(), that checks if the parameter it is given is a prime number. Recall that a number is prime if it is only divisible by 1 and itself. By definition the number 1 is not prime. You can check if n is divisible by m using the modulus operator % by checking whether n % m is zero. So, you simply have to iterate through all possible values of m to determine whether n is prime. (Actually, checking for m up to square root of n is sufficient.)

If n is indeed prime, then isPrime(n) must return 1. Otherwise, n is composite and isPrime(n) must return 0.

The function that you write must be compatible with the main program in this file: isprime.c.

/* File: isprime.c Implement and test the isPrime() function */ #include <stdio.h> #include <math.h> /* Function prototype */ /* Don't change the function prototype! */ int isPrime(int n) ; /* Do not change the main program!! */ int main() { int n ; printf("Enter a number: ") ; scanf("%d", &n) ; if ( isPrime(n) ) { printf("Yes, %d is prime.\n", n) ; } else { printf("No, %d is not prime.\n", n) ; } return 0 ; } /* End of main() */ /* Implement your isPrime() function here. */ /* Add function comment header here. */

First, download the main program in the file isprime.c. Make sure you save it with a filename that ends with .c. Then, add the implementation of your function at the bottom of the file.

Compile and run your program. Use the -Wall option with gcc. Your program should compile without any warnings or errors.


Part 2

In Part 2, your assignment is to write a function findPrime() that finds the first prime number larger than the parameter n. Number theory tells us that there is always a prime number between n and 2 × n. So, the search will always be successful. You simply have to iterate through numbers bigger than n until your isPrime() function from Part 1 tells you that you have found a prime.

The function that you implement must be compatible with the main program in this file: findPrime.c.

/* File: findprime.c Implement and test the findPrime() function */ #include <stdio.h> #include <math.h> /* Function prototype */ /* Do not change the function prototypes */ int isPrime(int n) ; int findPrime(int n) ; /* Do not change the main program!! */ int main() { int n ; printf("Enter a number: ") ; scanf("%d", &n) ; printf("%d is the first prime number greater than n.\n", findPrime(n) ) ; return 0 ; } /* End of main() */ /* Copy your isPrime() function here. */ /* Implement your findPrime function here. */ /* Add function header comment for findPrime() */

First, download the main program in the file findprime.c. Make sure you save it with a filename that ends with .c.

Below the main program, write the C code to implement the findPrime() function.

Compile and run your program. Use the -Wall option with gcc. Your program should compile without any warnings or errors.

Submitting

Use the script command to record yourself compiling and running the two programs. Run your programs several times! Submit your C source code files and the typescript file as usual:

submit cs104_chang hw10 isprime.c
submit cs104_chang hw10 findprime.c
submit cs104_chang hw10 typescript


Be sure to logout completely when you have finished!