Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following pseudocode describes the steps. You can use it as the basis to wri

ID: 3870049 • Letter: T

Question

The following pseudocode describes the steps. You can use it as the basis to write a working function. 1. Define useful constants for later use const double pi = 4.0*atan2(1.0, 1.0): const double inv_root2pi = 1.0/sqrt(2.0 * pi): 2. Initialize num_iter = 0 (obviously). 3. I y_target lessthanorequalto 0.0 or y_target greaterthanorequalto 1.0, it is invalid input. Set x = 0.0 and a function return value of 1 (fail). 4. Allocate two internal arrays "x_iter" and "double f_iter" of type double length at least (max_iter + 1). Remember to release any allocated memory on exit. 5. Initialize x_iter [0] = 0.0. (We could ask the calling function to supply an initial iterate, but let us not be too clever.) 6. Write a loop "for (i = 0: I

Explanation / Answer

#include <iostream>
#include <math.h>

using namespace std;

const double pi = 4.0*atan2(1.0,1.0);
const double inv_root2pi=1.0/sqrt(2.0*pi);

int main()
{
int num_iter=0, max_iter;
double y_target, x, tol;

if(y_target < 0 || y_target > 1) {
x = 0;
return 1;
}

double *x_iter = new double[max_iter+1];
double *f_iter = new double[max_iter+1];

x_iter[0] = 0.0;

for(int i=0; i<max_iter; i++) {
f_iter[i] = cum_norm(x_iter[i]) - y_target;
double fprime = exp(-0.5*x_iter[i] *x_iter[i]) * inv_root2pi;

if(abs(f_iter[i]) <= tol) {
x = x_iter[i];
num_iter = i;
delete [] x_iter;
delete [] f_iter;
return 0;
}
if(fprime == 0) {
x = 0;
delete [] x_iter;
delete [] f_iter;
return 1;
}
double delta_x = f_iter[i]/fprime;
if(abs(delta_x) <tol) {
x = x_iter[i];
num_iter = i;
delete [] x_iter;
delete [] f_iter;
return 0;
}
  
x_iter[i+1] = x_iter[i] - delta_x;
  
}

return 0;
}