Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

using this information and fill in the needed code to determine whether a number is prime or not. in C language only, nothing below should

using this information and fill in the needed code to determine whether a number is prime or not.

in C language only, nothing below should be changed only added codes.image text in transcribedimage text in transcribed

#include #include #include

#define BUF 256

int main(int argc, char * argv[]) {

double num = -1;//value to start the loop char line[BUF]; //char array to hold user response int items; //holds the number of items read from input

//variables for number of total entries, prime entries, nonPrime entries int count = 0, numPrimes = 0, nonPrimes = 0;

//variables for primeness, smallest prime, and largest prime int smallest, largest;

//C does not have a specific boolean type, so typically people use ints int prime; //0 is false, any other number is true

// check last item wasn't zero; if not // read the line from input and copy it to memory as a string while ( num != 0 && fgets(line, BUF, stdin) != NULL) {

// scan the number from the line - extra stuff on line is ignored items = sscanf(line, "%lf", &num);

//HINT: num is a double because the user might enter a double and we don't //want our program to crash if that happens. However modulos op only works //on int types. You will have to perform a type-cast conversion to mod num //if one item was read from line, its a whole number, and its not 0 if (items == 1 && (floor(num) == num) && (num != 0)) {

//YOUR CODE GOES HERE //You need to figure out: //how to determine primeness of numbers //how to track the number of entries, primes, & nonPrimes //How to track the largest and smallest primes, if any are entered

} }

//then you need to figure out how to handle the printing of the summary //You should print the number of entries, primes, and nonPrimes //If there have been any prime numbers entered, print the largest and //the smallest entered

//your program's output MUST match the output displayed in the testing and //grading scripts (spelling and capitalization DO matter for points) if (count > 0) { } else { //print "No numbers entered" if there are no entries }

return 0;

}

Directions: Take a look at the comments that were left on your initial primeFinder assignment. Address any issues or inefficiencies that were identified in your algorithm and translate your code into a c program. A starting point/stub file named primeFinder.c has been provided for you to use. Error Checking: Any blank lines should be ignored. If the user enters a fractional number (anything with decimal value) it should be ignored. If the user enters text (words) the line should be ignored. In other words, bad input is ignored. Reminders 1) In this program, a prime number is any whole number greater than or equal to 2 that is not divisible by any values other than the number 1 and itself. 2) Some inputs will come in the form of a float. You may have to do some type casting in certain parts of the code Notes about the code: 1. sscanf() returns the number of successful assignments it made. So if items is 1, that means the function was able to parse 1 data type (char, int, double, etc) from the input. Otherwise, the input line was garbled/incorrect. The code already will skip invalid data (words). However it will parse numbers if it is possible. So input like 12.4g will be read as 12.4 2. since you should be accepting input of type double, you need a matching format specifier for sscanf, which is the one for long float (%lf). Remember that scanflsscanf returns the number of items it was able to read, or special value EOF if there is no data or some error occurs. When you perform modulus operations (int only) on the number you can use type casting to treat the num variable as int: (int) num % x (Read more about scanf by running man scanf on the command line) 3. When the input ends, fgets() will retum a special value EOF (for end of file). So if the input doesn't have a zero at the bottom, this loop means the program will still end correctly. Here are some sample runs of my program named primeFinder. Your output should match the output of the testing & grading scripts (which also matches this). Including punctuation. Explanation Nevy introdssign Finder ./primeFinder This executes primeFinder in my current folder (Unix syntax) A blank line is entered just enter/retum key) 4 numbers are entered (note the 1) and assessed 1 is not prime according to the given definition 2 is prime according to the given definition 4 is Non Prune! 4 is not prime according to the given definition 97 is prime according to the given definition 0 ends the series The total number of Prime unters entered: 3 The largest prime entered is 97 The total number of Non-Prire numbers entered: 2 The smallest prime entered is 2 The smallest prime Ner: 2 Fxnlanation 1 is Non-Prin! 2 is Print 3 Is Prin! 97 is Prime The total entries: 5 The largest prirebir 97

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What are the stages of project management? Write it in items.

Answered: 1 week ago

Question

why do consumers often fail to seek out higher yields on deposits ?

Answered: 1 week ago