Question

OLA203 Exercise Due (by midnight): February 13, 2020 NOTE: Please remember that exercises are due by...

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 <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
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
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream

#include<fstream>

using namespace std;

int main()

{

int n;

ifstream inFile;

inFile.open("eCandidates.dat.txt");

if (!inFile) {

cout << "Unable to open eCandidates.dat.txt";

exit(1)

}

while (inFile >=3 && inFile%2!=0)

{

cou<<"" n"";

}

inFile.close();

cout<<"n= "<<n<<end1;

return 0;

}

Add a comment
Know the answer?
Add Answer to:
OLA203 Exercise Due (by midnight): February 13, 2020 NOTE: Please remember that exercises are due by...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assignment Develop a program to analyze one or more numbers entered by a user. The user...

    Assignment Develop a program to analyze one or more numbers entered by a user. The user may enter one or more numbers for analysis. Input should be limited to numbers from 1 through 1000. Determine if a number is a prime number or not. A prime number is one whose only exact divisors are 1 and the number itself (such as 2, 3, 5, 7, etc.). For non-prime numbers, output a list of all divisors of the number. Format your...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • //This program is your final practice exam. //Please fill in the functions at the bottom of...

    //This program is your final practice exam. //Please fill in the functions at the bottom of the file. (sum and removeItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream>...

  • C++ Programming question For this exercise, you will receive two string inputs, which are the names...

    C++ Programming question For this exercise, you will receive two string inputs, which are the names of the text files. You need to check if the input file exists. If input file doesn't exist, print out "Input file does not exist." You are required to create 4 functions: void removeSpace (ifstream &inStream, ofstream &outStream): removes white space from a text file and write to a new file. If write is successful, print out "Text with no white space is successfully...

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE INTO VISUAL STUDIO Program 4:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE INTO VISUAL STUDIO Program 4: A palindromic prime number is a number that is both prime number and a palindrome number. For example, 131, 313, and 757 are palindromic prime numbers. Design (pseudocode) and implement (source code) a program (name it PalindromicPrime) to display the first 50 palindromic prime numbers, 10 per line separated by one space. The program defines the following methods: Method isPalindome() to check if a...

  • USING C# AND PSUDOCODE PLEASE : A palindromic prime number is a number that is both...

    USING C# AND PSUDOCODE PLEASE : A palindromic prime number is a number that is both prime number and a palindrome number. For example, 131, 313, and 757 are palindromic prime numbers. Design (pseudocode) and implement (source code) a program (name it PalindromicPrime) to display the first 50 palindromic prime numbers, 10 per line separated by one space. The program defines the following methods: Method isPalindome() to check if a number is palindrome or not. Method isPrime() to check if...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • Create a program named Lab14B. You will create a nested loop in this program by putting...

    Create a program named Lab14B. You will create a nested loop in this program by putting Lab14A’s for loop inside a while loop. In the new while loop, you will read integers from the file, Lab14B.txt, and determine if the number is prime (using the copied loop from Lab14A). Stop the while loop when you reach the end of file. Copy your code from Lab14A into your new program Add a while not eof loop around that code, and also...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT