INSTRUCTIONS: Write a program acts as a utility to allow users to enter a test n
ID: 3529827 • Letter: I
Question
INSTRUCTIONS:
Write a program acts as a utility to allow users to enter a test number and determine its sign, even/odd state, and status as a prime number. The program should begin with the number unset (indicated with a boolean flag). An option should be present that allows the user to enter a number to test (option #1). Once a number is set, the menu is redisplayed, except now options to test the number for the previously mentioned properties are present. The user makes a selection and an appropriate message is displayed on the screen. Place the main logic for your program in main. Be sure to include separate functions for displaying the menu, setting the test number and performing the tests. A starting shell and sample output are provided below. Make your program models the sample as closely as possible. Pay special attention to the way the menus display in an intelligent fashion.
// INCLUDES AND NAMESPACES
#include <iostream>
using namespace std;
// DEFINES
#ifndef __TRUE_FALSE__
#define __TRUE_FALSE__
#define TRUE 1
#define FALSE 0
#endif
// FUNCTION PROTOTYPES
int DisplayMenuSelections();
void SetTestNumber();
void SignTest();
void EvenOddTest();
void PrimalityTest();
// GLOBALS
int gTestNumber = 0;// global variable that holds the number we are testing
bool gTestNumberSet = FALSE;// global variable that contains a boolean state reflecting
// whether or not the user has set the test number
// MAIN
int main() {
return 0;
}
// FUNCTION IMPLEMENTATIONS
// returns the user's menu selection code (-1 if invalid selection is made)
int DisplayMenuSelections() {
}
void SetTestNumber() {
}
void SignTest() {
}
void EvenOddTest() {
}
void PrimalityTest() {
}
Explanation / Answer
// INCLUDES AND NAMESPACES
#include <iostream>
using namespace std;
// DEFINES
#ifndef __TRUE_FALSE__
#define __TRUE_FALSE__
#define TRUE 1
#define FALSE 0
#endif
// FUNCTION PROTOTYPES
int DisplayMenuSelections();
void SetTestNumber();
void SignTest();
void EvenOddTest();
void PrimalityTest();
// GLOBALS
int gTestNumber = 0;// global variable that holds the number we are testing
bool gTestNumberSet = FALSE;// global variable that contains a boolean state reflecting
// whether or not the user has set the test number
// MAIN
int main()
{
int k = 0;
do
{
k = DisplayMenuSelections();
if(gTestNumberSet)
cout << " current test number is " << gTestNumber << endl;
switch(k)
{
case 1: SetTestNumber(); break;
case 2:SignTest(); break;
case 3:EvenOddTest(); break;
case 4:PrimalityTest();break;
case 5: return 0;
default: cout << " invalid input "<<endl;
}
}while(k!=5);
return 0;
}
// FUNCTION IMPLEMENTATIONS
// returns the user's menu selection code (-1 if invalid selection is made)
int DisplayMenuSelections() {
int k;
cout << "1. set the test number " << endl;
cout << "2. test for Sign " << endl;
cout << "3. test for even / odd " << endl;
cout << "4. tset for primaility" << endl;
cout << "5. exit program " << endl;
cin >> k;
return k;
}
void SetTestNumber() {
cout <<" Enter the number you would like to test " << endl;
cin >> gTestNumber;
gTestNumberSet = TRUE;
}
void SignTest() {
if(gTestNumber <0) cout <<" the Number is negative " << endl;
else if(gTestNumber >0) cout <<" the Number is positive " << endl;
else cout <<" the Number is ZERO " << endl;
}
void EvenOddTest() {
if(gTestNumber %2 ==0) cout <<" number is even" <<endl;
else cout <<" number is Odd" <<endl;
}
void PrimalityTest() {
int i,temp;
int cnt = 0;
for(i=1; i<gTestNumber; i++)
{
temp = gTestNumber % i;
if (temp==0)
cnt++;
}
if(gTestNumber == 1)cout << " Number is neither prime nor composite" << endl;
else if(temp ==0) cout << " Number is prime " << endl;
else cout << " Number is composite " << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.