Question

In: Computer Science

Rewrite the function so the formal parameter x is an IO pointer-to int parameter. The function’s...

Rewrite the function so the formal parameter x is an IO pointer-to int parameter. The function’s prototype is changed as shown below. Please explain code with comments

//--------------------------------------------------

void NextPrime(int *x)

//--------------------------------------------------

{

bool IsPrime(const int x);

}

Solutions

Expert Solution

//C++ program

#include<iostream>
#include<math.h>
using namespace std;


bool IsPrime(const int x){
   //function to check whether a number given in argument is prime or not
   //if any number >=2 and less than equal to its square root not divides x then x is prime
for(int i=2;i<=sqrt(x);i++){
    if(x%i == 0)return false;
   }
   return true;
}

void NextPrime(int *x){
   //increasing value of x by 1 to check whether next element is prime or not
   *x = *x+1;
   //we keep increasing value x by 1 until we find x is a prime
   while(IsPrime(*x) == false){
       *x = *x + 1;
   }
}

//Driver main function
int main(){
   int x = 13;
  
   NextPrime(&x);
  
   cout<<"Next Prime Number : "<<x;
   return 0;
}

//sample output


Related Solutions

C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is...
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is positive, the function will assign it the next leap year after it; otherwise, the function will assign 4 to it.
Reimpelment a function called bubble_sort that has the following prototype. bubble_sort(int *array, int size, pointer to...
Reimpelment a function called bubble_sort that has the following prototype. bubble_sort(int *array, int size, pointer to a function) Pre condition array - a pointer to a an array of size element. pointer to function - a pointer to a function that compares two values (depending on sorting in ascending order or descending order) Post condition Sort the array in ascending or descending based on the the pointer to a function. Call the function bubble_sort to sort the array in ascending...
1a. Eliminate the parameter t to rewrite the parametric equation as a Cartesian equation. x(t) =...
1a. Eliminate the parameter t to rewrite the parametric equation as a Cartesian equation. x(t) = cos(t) + 1 y(t) = 7 sin2(t) 1b. A dart is thrown upward with an initial velocity of 68 ft/s at an angle of elevation of 52°. Consider the position of the dart at any time t. Neglect air resistance. (Assume t is in seconds.) Find parametric equations that model the problem situation. x(t) = y(t) = 1c. A dart is thrown upward with...
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {  ...
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {    cout<<"? ";    if (n > 0)      mystery (n - 1); }
8.20 Person Pointer Function Make a function that will accept a pointer to a vector of...
8.20 Person Pointer Function Make a function that will accept a pointer to a vector of Person pointers as a parameter. Return a pointer to the Person with the highest salary. The function must be signatured like: Person* getBestPaid(vector*); The class definition is: class Person { public: double salary; string name; }; You may include code in your main() to test, but the tests in this assignment will ensure your code is correct. You may assume there will ways be...
array • First, create a function called addNumber, which has a formal parameter for an array...
array • First, create a function called addNumber, which has a formal parameter for an array of integers and increase the value of each array element by a random integer number between 1 to 10. o Add any other formal parameters that are needed. • Second, create another function called printReverse that prints this array in reverse order. • Then, you need to write a C++ program to test the use of these two functions.
Apart from the formal parameter(s) and the body of the function, what else does the closure...
Apart from the formal parameter(s) and the body of the function, what else does the closure ADT encapsulate? Why is this necessary for correct evaluation of the function at call-time? Give a concrete example if possible
Code should be written in C Use pointer variables for parameter passing, where appropriate and pointer...
Code should be written in C Use pointer variables for parameter passing, where appropriate and pointer arithmetic when working with arrays. 1) Using initialization lists, create 5 one-dimensional arrays, one for each of these: hold the names of the people running in the election hold the names of the subdivision hold the vote counts in the Brampton subdivision for each candidate hold the vote counts in the Pickering subdivision for each candidate hold the vote counts in the Markham subdivision...
In C++, type a function function(int n, int base) that converts a positive integer x to...
In C++, type a function function(int n, int base) that converts a positive integer x to any base between 2 and 9. The function HAS to do this using a stack, and using methods from below: +isEmpty(): boolean +push(newEntry: ItemType): boolean +pop(): boolean +peek(): ItemType (Also, type a program to test the function). Hint: could use simple iteration continually divides the decimal number by base and keeps track of the remainder by using a stack.
Overload and test function getNum(int &) to work with a parameter of type double. 2. Overload...
Overload and test function getNum(int &) to work with a parameter of type double. 2. Overload and test function doubleNum(int) to also work with a parameter of type double. 3. Both functions for collecting input should validate such that negative values would not be allowed. Perform the following steps: a. Add the function prototypes at the top. b. In main(), add a new variable of type double. You can name it "value2" to distinguish from the other variable. c. Write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT