Jump to content

High School Mathematics Extensions/Mathematical Programming/Output Routines

From Wikibooks, open books for an open world

Explanation

[edit | edit source]

Two new global variables were added: double x_val and double delta.

Two new functions were added: void output() and double process().

One function was changed: void execute_command(char command).

Global Variables

double x_val: the value for x that is passed to f(x).

double delta: the value by which x is changed when the + or - commands are executed.

Function Additions

void output()

  • This function calls process to get the value of f(x) and then prints the message f(x)=value.

double process()

  • Executes the C implementation for f(x).

Function Changes

void execute_command(char command)

  • Called the new function output to display f(x)=value.
  • Implements the '-' and '+' commands for the variables x_val and delta.

Code to change

[edit | edit source]
Add and replace the following code:
 //function prototypes
 //...
 float process();
 void output();

 //global variables
 //...
 float x_val;
 float delta;

 //function definitions
 //...
 void execute_command(char command)
{
    switch (command)
    {
        case '=':   output();
                    break;
        case '+':   output();
                    break;
        case '-':   output();
                    break;
        case 'd':
        case 'D':   break;
        case 'x':
        case 'X':   done++;
                    break;
    }
}
float process()
{
    return x_val * x_val;
}
void output()
{
    float f_val;
    f_val=process();
    cprintf("f(%f)=%6f.\n",x_val,f_val);

}


Next Step

[edit | edit source]