Organize your design into three parts main General display

Organize your design into three parts: main() – General display misc., get user input, etc. reportPrimes() – Function that loops through the numbers up to the max (input by user) isPrime() – Function that checks whether a single number given to it is prime. It can return the number if prime or zero if not prime. To avoid pitfalls and running over your allotted lab time, be sure to work out the algorithm and program logic in pseudo-code before you begin writing actual code! 1) Your lab instructor has provided the code for main() below (see lab-5.c for more complete listing): int main (void) { int n; showTitleScreen(); n = getUserInput(); reportPrimes(n); return(0); } 2) Write the supporting functions that will enable the above main() function to work. You are not authorized to alter main(). You will need three functions: getUserInput() – This function has no parameters. Its job is to ask for the user’s maximum (an integer). Error checking should be included within this function to ensure it receives valid input before returning. (Returning a number less than 2 could be a way to indicate the user wishes to quit the program.) reportPrimes(n) – This function takes one argument (int). Its task is to iterate through all numbers 2 to n, and to display only the prime numbers on screen. NOTE: This function checks whether a number is prime by calling the isPrime() function (below). It does not contain the actual algorithm to test whether a number is prime. isPrime(i) – This function takes one argument (int). The only purpose of this function is to check whether i is prime. The function returns the number i if prime or zero (0) otherwise. If you’re NOT working on isPrime(), use this function as a placeholder to test your work: int isPrime(int p) { return p; } ESET 269 Laboratory #5 Spring 2017 Programming Guidelines Keep your functions clean and simple. Each function should perform only its specified task. The goal is to end up with functions, when written to their specifications, that could work in anyone else’s project without any modification to their code or your functions.

Given the following started code. this program is in C

Solution

#include <stdio.h>
#include <stdlib.h>

// Function prototypes
void showTitleScreen(void);

int main(void) {
int n;

// display welcome screen
showTitleScreen();

// get user input
n = getUserMax();

// find and display prime numbers
displayPrimes(n);

return EXIT_SUCCESS;
}

/* Function: showTitleScreen()
* Purpose: Display welcome screen
* Parameters: void
* Returns: void
*/
void showTitleScreen(void) {
printf(\"\ \");
printf(\"\ \");
printf(\"Prime Number Finder\ \");
printf(\"\ \");
printf(\"\\t- Finds prime numbers from 1 up to your limit\ \");
printf(\"\ \");
return;
}

/* Function: getUserMax()
* Purpose: Ask user for prime search upper limit
* Parameters: void
* Returns: int - the user\'s maximum
*/
int getUserMax()
{
int max = 0;
printf(\"Enter number to see list of primes from 1 to that number: \");
scanf(\"%d\", &max);

if (max < 2)
{
printf(\"Please enter a integer greater than 1.\ \");
return getUserMax();
}
else
{
return max;
}
}

/* Function: displayPrimes()
* Purpose: Display prime numbers between 1 and n
* Parameters: int n - the user\'s max
* Returns: void
*/

void displayPrimes(int n)
{
int i = 1;
printf(\"List of primes from 1 to %d\ \", n);
for(i = 1; i <= n; i++)
{
if (isPrime(i) != 0)
{
printf(\"%d \", i);
}
}
printf(\"\ \");
}

/* Function: isPrime()
* Purpose: Test whether a number is prime
* Parameters: int n - the number to test
* Returns: int - the number n if prime, zero if not prime
*/

int isPrime(int n)
{
// Corner cases
if (n <= 1) return 0;
if (n <= 3) return n;

// This is checked so that we can skip
// middle five numbers in below loop
if (n%2 == 0 || n%3 == 0) return 0;

int i = 5;
for (i=5; i*i<=n; i=i+6)
if (n%i == 0 || n%(i+2) == 0)
return 0;

return n;
}

Organize your design into three parts: main() – General display misc., get user input, etc. reportPrimes() – Function that loops through the numbers up to the m
Organize your design into three parts: main() – General display misc., get user input, etc. reportPrimes() – Function that loops through the numbers up to the m

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site