Question: Problem: Convert a C program to a Goto-C program. Read the following for information on how Goto-C works and for examples: Goto-C refers to a

Problem: Convert a C program to a "Goto-C" program.

Read the following for information on how "Goto-C" works and for examples:

"Goto-C refers to a programming language that is C with the following modifications: The only kind of if statement allowed in Goto-C is the following: if (expression) goto label ; Goto-C does not have the following keywords and operators: else while for do switch && || Goto-C does not have ?: the conditional operator.

Problem: Convert a C program to a "Goto-C" program. Read the followingfor information on how "Goto-C" works and for examples: "Goto-C refers to a programming language that is C with the following modifications: The only

kind of if statement allowed in Goto-C is the following: if (expression)

goto label ; Goto-C does not have the following keywords and operators:

else while for do switch && || Goto-C does not have ?:

MAKE SURE TO TRANSLATE ANY EXPRESSIONS THAT USE else, for, while, do, switch &&, ||. THESE ARE ILLEGAL KEYWORDS AND MUST BE REMOVED. CONFIRM THAT THE ORGINAL C CODE AND "Go-to C" CODE PRODUCE THE SAME OUTPUT! PLEASE PROVIDE THE "Go-to C" CODE AND SCREENSHOTS OF THE OUTPUTS WITH THE TEST INPUTS PROVIDED ABOVE.

The original c file is provided below

// lab1exG.c

#include

#include

#include

#define MAX_ABS_F (5.0e-10)

#define POLY_DEGREE 4

double polyval(const double *a, int n, double x);

/* Return a[0] + a[1] * x + ... + a[n] * pow(x, n). */

int main(void)

{

double f[] = {1.45, 0.72, -3.07, -1.15, 1.00};

double dfdx[POLY_DEGREE];

double guess;

int max_updates;

int update_count;

int n_scanned;

int i;

double current_x, current_f, current_dfdx;

printf("This program demonstrates use of Newton's Method to find "

"approximate roots of the polynomial f(x) = ");

printf("%.2f", f[0]);

for (i = 1; i

if (f[i] >= 0)

printf(" + %.2f*pow(x,%d)", f[i], i);

else

printf(" - %.2f*pow(x,%d)", -f[i], i);

printf(" Please enter a guess at a root, and a maximum number of "

"updates to do, separated by a space. ");

n_scanned = scanf("%lf%d", &guess, &max_updates);

if (n_scanned != 2)

{

printf("Sorry, I couldn't understand the input. ");

exit(1);

}

if (max_updates

{

printf("Sorry, I must be allowed do at least one update. ");

exit(1);

}

printf("Running with initial guess %f. ", guess);

for (i = POLY_DEGREE - 1; i >= 0; i--)

dfdx[i] = (i + 1) * f[i + 1]; // Calculus!

current_x = guess;

update_count = 0;

while (1)

{

current_f = polyval(f, POLY_DEGREE, current_x);

printf("%d update(s) done; x is %.15f; f(x) is %.15e ",

update_count, current_x, current_f);

if (fabs(current_f)

break;

if (update_count == max_updates)

break;

current_dfdx = polyval(dfdx, POLY_DEGREE - 1, current_x);

current_x -= current_f / current_dfdx;

update_count++;

}

if (fabs(current_f) >= MAX_ABS_F)

printf("%d updates performed, |f(x)| still >= %g. ",

update_count, MAX_ABS_F);

else

printf("Stopped with approximate solution of %.10f. ",

current_x);

return 0;

}

double polyval(const double *a, int n, double x)

{

double result = 0.0;

int i = n;

while (1)

{

result += a[i];

if (i == 0)

break;

result *= x;

i--;

}

return result;

}

Consider the following simple fragment of normal C: if (x >= 0) y = x; else y = -x; The above is illegal in Goto-C for two reasons: the if statement is not in an acceptable form, and the banned keyword else is used. One way to translate the fragment to Goto-C is as follows: if (x = 0){ printf("%d ", i); i--; } can be written as: loop-top: if (i 0 && x / y >- 10) { foo(x); bar(y); } can be coded as if (y

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!