Question

c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input:...

c++
#include <iostream>
#include <string>
using namespace std;


/* Write a function checkPrime such that
input: an int
output: boolean
function: Return true if the number is a prime number and return false otherwise
*/
//TO DO **************************************






/* Write a function checkPrime such that
input: an array of int and size of array
output: nothing
function: Display the prime numbers of the array
*/
//TO DO **************************************









/* Write a function SumofPrimes such that
input: an int
output: nothing
function: Check whethe a given number can be expressed as a sum of two prime number, then display (cout) two prime numbers if it is possible
If there are multiple prime numbers that can be sum up, you should display all of them. (check the main below)
*/
//TO DO **************************************











/* Write a function printArray such that
input: pointer to the last element of array, and int which is length of array
output: nothing
function: Display (cout) the element of array in order
Hint: array is the consequencial in the memory
*/
//To DO *****************************************











/* Write a function checkPalindrome_String such that
input: String
output: boolean
function: Check whethe a given string is palindrome. It means if I reverse it, it would be the same. like Hannah , Bob
Hint: string is like an array of character ; s.size() returns the size of string s
*/
//TO DO **************************************












/* Bonus Point: Write a function checkPalindrome_Integer such that
input: int
output: boolean
function: Display (cout) the reverse of the integer, and return true if the number and its reverse are equal
Hint: you can use % to get the digit of numbers
*/
//TO DO **************************************










int main()
{
        /* The output of this section should be
        This prime numbers are : 2, 7, 11
        */
        int A[5] = { 2,55, 7, 44, 11 };
        cout<< "The output should be 2,7,11, your output is "<<findPrime(A, 5);


        /* The output of this section should be 
        34 = 3 + 31 
        34 = 5 + 29
        34 = 11 + 23
        34 = 17 + 17
        */
        SumofPrimes(34);
        
        
        
        /* This should print the A in order from the beginning  2 55 7 44 11*/
        print_array(&A[4], 5);
        
        
        
        //This should return true
        cout << checkPalindrom_String("HANNAH");
        

        //This should return 654321
        cout << checkPalindrom_Integer(123456) << endl;

        return 0;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/******************* Your code is as follow **************************************************************************************/

#include <iostream>
#include <string>
using namespace std;


/* Write a function checkPrime such that
input: an int
output: boolean
function: Return true if the number is a prime number and return false otherwise
*/
bool checkPrime(int n){
bool check=true;
for(int i = 2; i <= n / 2; i++)
{
if(n % i == 0)
{
check = false;
break;
}
}
if (check)
return true;
else
return false;
}

/* Write a function checkPrime such that
input: an array of int and size of array
output: nothing
function: Display the prime numbers of the array
*/
void findPrime(int a[],int len){
for(int i = 0; i < len; i++){
bool check = checkPrime(a[i]);
if(check == true)
cout << a[i] << " ";
  
}
cout << endl;
  
}

/* Write a function SumofPrimes such that
input: an int
output: nothing
function: Check whethe a given number can be expressed as a sum of two prime number, then display (cout) two prime numbers if it is possible
If there are multiple prime numbers that can be sum up, you should display all of them. (check the main below)
*/
void SumofPrimes(int num){
int i;
for(i = 2; i <= num/2; i++)
{
if (checkPrime(i)) //check the 1st component of number
{
if (checkPrime(num - i)) //check the another part of number
{
cout << num << " = " << i << " + " << num-i << endl;
}
}
}

}


/* Write a function printArray such that
input: pointer to the last element of array, and int which is length of array
output: nothing
function: Display (cout) the element of array in order
Hint: array is the consequencial in the memory
*/
void print_array(int *num, int len){
int cnt = 0;
int *intPtr = num-(len-1);
while( *intPtr != NULL && cnt < len )
{
cout<<*intPtr<<" ";
intPtr++;
cnt++;
}
}

/* Write a function checkPalindrome_String such that
input: String
output: boolean
function: Check whethe a given string is palindrome. It means if I reverse it, it would be the same. like Hannah , Bob
Hint: string is like an array of character ; s.size() returns the size of string s
*/
bool checkPalindrom_String(string str){
int len = str.length();
int i;
bool check=true;
for(i=0; i < len; i++){
if(str[i] != str[len-i-1]){
check = false;
break;
}
}
return check;
}


/* Bonus Point: Write a function checkPalindrome_Integer such that
input: int
output: boolean
function: Display (cout) the reverse of the integer, and return true if the number and its reverse are equal
Hint: you can use % to get the digit of numbers
*/

bool checkPalindrom_Integer(int num){
int rem;
int revNum = 0;
while(num != 0)
{
rem = num%10;
revNum = revNum*10 + rem;
num = num/10;
}
  
cout << "Reverse of given integer:" << revNum << endl;
if(num == revNum)
return true;
else
return false;
}

int main()
{
/* The output of this section should be
This prime numbers are : 2, 7, 11
*/
int A[5] = { 2,55, 7, 44, 11 };
cout << "The output should be 2,7,11, your output is ";
findPrime(A, 5);


/* The output of this section should be
34 = 3 + 31
34 = 5 + 29
34 = 11 + 23
34 = 17 + 17
*/
SumofPrimes(34);
  
  
  
/* This should print the A in order from the beginning 2 55 7 44 11*/
print_array(&A[4], 5);
  
  
  
//This should return true
cout << endl;
cout << checkPalindrom_String("HANNAH");
  
cout << endl;
//This should return 654321
cout << checkPalindrom_Integer(123456) << endl;

return 0;
}

/*************************************************** Screen shot of output ******************************************************/

Add a comment
Know the answer?
Add Answer to:
c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input:...
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
  • In C++ #include<iostream> using namespace std; //Implement the function below. bool is_fibonacci_array(int*,int); //Param 1: pointer to...

    In C++ #include<iostream> using namespace std; //Implement the function below. bool is_fibonacci_array(int*,int); //Param 1: pointer to the beginning of an array of integers //Param 2: size of that array //Returns: true, is every element in the input array is a Fibonacci number //(the order of the numbers in the array need not be ordered //as per the Fibonacci sequence) //false, otherwise //A Fibonacci sequence is generated as follows. //The first two numbers in the sequence are 0 and 1. //The...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car {...

    #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...

  • #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the...

    #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the array"<<endl;    cin>>SIZE;     int *numlist = new int[SIZE];     // Read SIZE integers from the keyboard     for (int i = 0; i<SIZE; i++ )    {        cout << "Enter value #" << i+1 << ": ";        cin >> numlist[i];     }     // Display the numbers in a reverse order     for (int i = SIZE; i > 0; i--...

  • 4) What is the output if the input istom - Sawyer? #include <iostream> using namespace std;...

    4) What is the output if the input istom - Sawyer? #include <iostream> using namespace std; int main() { string playerName; cout << "Enter name"; cin >> playerName; cout << endl « playerName; return 0; } a. Tom - Sawyer b. Tom Sawyer c. Tom d. Sawyer 5) Which XXX generates "Adam is 30 years old." as the output? #include <iostream> using namespace std; int main() { string name = "Adam"; int age = 30; XXX return 0; } a....

  • #include <iostream> //write preprocessor file for string datatype here using namespace std; //write your function prototypes...

    #include <iostream> //write preprocessor file for string datatype here using namespace std; //write your function prototypes here: int main() {     cout << "Welcome to Mad Lib.\n\n";     cout << "Answer the following questions to help create a new story.\n";     string name = askText("Please enter a name: ");          string noun = askText("Please enter a plural noun: ");          int number = askNumber("Please enter a number: ");          string bodyPart = askText("Please enter a body part: ");          string...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • #include <iostream> #include <string> using namespace std; //Write a function that changes all characters in a...

    #include <iostream> #include <string> using namespace std; //Write a function that changes all characters in a string to dashes string to_dash(string s){ for(int i = 0; i < s.length(); i++){ } return s; } int main(){ string s; cin >> s; s = to_dash(s); cout << s << endl; }

  • C++ Only. Don't use other libraries other than #include <iostream> and using namespace std; Write a...

    C++ Only. Don't use other libraries other than #include <iostream> and using namespace std; Write a function maxTemp which takes a filename as string argument and returns the maximum temperature as float type for the week. Input file will contain temperature readings for each day of the week. Your function should read each line from the given filename, parse and process the data, and return the required information. Your function should return the highest temperature for the whole week. Empty...

  • C++ Write a recursive function that reverses the given input string. No loops allowed, only use...

    C++ Write a recursive function that reverses the given input string. No loops allowed, only use recursive functions. Do not add more or change the parameters to the original function. Do not change the main program. #include <iostream> #include <string> using namespace std; //Write a recursive function 'void reverse(string &str)' that reverses the given input string. void reverse(string &str) { /*Code needed*/ } int main() {    string name = "sherry";    reverse(name);    cout << name << endl; //should...

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
ADVERTISEMENT