Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

OLA203 Exercise Due (by midnight): February 13, 2020 NOTE: Please remember that exercises are due by the stated time and cannot be late. OBJECTIVE: Practice

OLA203 Exercise Due (by midnight): February 13, 2020 NOTE: Please remember that exercises are due by the stated time and cannot be late.

OBJECTIVE: Practice using CodeLite and C++, especially functions, if statements, while loops, and opening/closing files. Again, this solo assignment should help you honestly assess your programming skill level and personal understanding of C++.

PROBLEM BACKGROUND: An integer N (greater than or equal to 2) is prime if and only if the only integer larger than 1 that divides N evenly is N itself. If a number is not prime it is composite. An emirp (prime spelled backwards) is a prime whose reversal is also prime BUT is not a palindromic prime. (Palindromic simply means "same backwards and forwards".) The first few eprimes are 13, 17, 31, 37, 71, 73, 79, 97, 107, 113, 149, 157.

TASK: Write a program that accepts as input positive odd integer numbers greater than or equal to three. Read these numbers from the file eCandidates.dat. (Yes, this means you have to open that file.) Reject any invalid input, that is, reject even integers or values less than three. (For this program, reject simply means an invalid number is skipped/ignored. You may assume only integer numbers are in the input stream.) The program should categorize these numbers as it gets them as to whether they are emirp, or just plain prime, or composite. There is no sentinel number at the end of the file; use the end-of-file method to determine when to terminate the program. Do not forget to close the file before terminating.

IMPLEMENTATION NOTES: Use CodeLite to complete this assignment. Do all your work under the directory ~/OLA/ola203 that you will create via CodeLite. For the completed version of this program, use as your source file name "emirp.cpp". Use as your executable file name "ola203". Use the following program shell as your starting point. A file containing the shell is available as $PUB/emirpStart.cpp. Modify as needed to complete the program. Don't forget to use the Tab key when indenting program source code. Use the textbook's program style conventions; do not "invent" your own. (In particular, do not cozy braces.) When you are ready to test run your program, symbolically link the file $PUB/eCandidates.dat to your account and use it.

 $ cd ~/OLA/ola203 $ ln -s $PUB/eCandidates.dat eCandidates.dat 

HINT: The modulus (%) operator is apt to be useful.

REQUIRED: Your program must be submitted electronically by midnight of the due date shown above. No late submissions can be accepted. You are to electronically submit the source program, a source program listing, compilation results, and the output obtained from running your program against the contents of the file eCandidates.dat. Something like the following UNIX commands will let you generate what is required:

 $ cd ~/OLA/ola203 $ script ola203.log $ rm eCandidates.dat $ ln -s $PUB/eCandidates.dat eCandidates.dat $ pr -n -t -e4 emirp.cpp $ c++ emirp.cpp -o ola203 $ ola203 $ exit (Be sure to exit the script session before trying to handin ola203!) $ handin "ola203" emirp.cpp ola203.log 

GRADING: As always, in addition to examining your source code and the execution results obtained by running your program against the given test data, the grader may run your program against some additional (and unknown to you) data. Your program must work properly for any valid input to be considered correct.

SOME OBSERVATIONS: As before, do not look stuff up on this problem and rob yourself of a chance to exercise your talents. This problem is completely self-contained and requires no outside information to solve. Needing assistance with CodeLite or C++ syntax is not unusual, but you should be able to figure out the logic for such a straightforward program on your own. Neither offer nor accept any additional help, except on a one-to-one basis with the instructor or lab assistant or official CS tutor.

// ola203 BY student name, CSCI 2170-sec, Due: mm/dd/yy // PROGRAM ID: emirp.cpp / The Emirp Number Program // AUTHOR: student name // INSTALLATION: MIDDLE TENNESSEE STATE UNIVERSITY // REMARKS: This program accepts as valid input positive odd integers (>=3) // and categorizes them as 'emirp', 'prime', or 'composite'. // // . 1 . 2 . 3 . 4 . 5 . 6 . 7 . //3456789012345678901234567890123456789012345678901234567890123456789012345 #include  #include  #include  #include  using namespace std; // Constants const int END_OF_DATA = -1; // EOD sentinel // Function prototypes: int ObtainNumber(ifstream& input); void PrintAnalysis(int testedNumber); bool IsDivisible(int number); int Reverse(int number); int main() { ifstream candidates("eCandidates.dat"); int testNumber; // Number to be evaluated as emirp, prime, or composite cout << "ola203 by student name" << endl;; if (!candidates) return 1; testNumber = ObtainNumber(candidates); while (testNumber!=END_OF_DATA) { PrintAnalysis(testNumber); testNumber = ObtainNumber(candidates); } candidates.close(); return 0; } // end main // Return the next odd number (>=3) in the input stream OR the // "END_OF_DATA" sentinel value if nothing left in input stream. int ObtainNumber(ifstream& input) { // . . . your code here . . . } // end ObtainNumber // Print message indicating if testNumber is emirp, prime, or composite. void PrintAnalysis(int testNumber) { bool isPrime; // Flag denoting 'testNumber' is prime int numberReversed; // testNumber with numerals in reverse order cout << setw(10) << testNumber; isPrime = !IsDivisible(testNumber); if (isPrime) { numberReversed = Reverse(testNumber); if (testNumber!=numberReversed && !IsDivisible(numberReversed)) cout << " is an Emirp number." << endl; else cout << " is a Prime number." << endl; } else cout << " is a Composite number." << endl; } // end PrintAnalysis // Determine if number is divisible by any divisors other than itself and 1. bool IsDivisible(int number) { // . . . your code here . . . } // end IsDivisible // Evaluate to the number formed by reversing the numerals of the parameter int Reverse(int number) { // . . . your code here . . . } // end Reverse

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

Students also viewed these Databases questions