High School Mathematics Extensions/Mathematical Programming/Sample C Program
Programming is an iterative process. This sample will walk you through creating a C program that allows you to evaluate the function X^2 using intervals that you define. This program will serve as a basis for exploring issues in the Mathematical Programming section of the High School Mathematics Extensions Wikibook.
The following pages will be divided into three parts:
- An explanation of the code provided on the page.
- Code that you should be able to paste into your editor and compile.
- A link to the next iteration in the development of the sample program.
What this program does.
- Define a character variable called command.
- Call the void function init to get ready to make sure the global variables are correctly set up.
- Call the void function input_message to prompt the user.
- Loop while the global variable done is set to false.
- Call the function input to initialze the variable command.
- Call the function execute_command to process the command.
- Sets done to FALSE.
- Prints the message "Press a key to continue: \n" to the console.
- Reads a character from the console.
- Sets the global variable done to true.
This sample program is divided into five sections
- 1. Header Files
- Header files can be user defined or linked to a library. One purpose of user defined include files is to provide modularity and re-use. That is currently not an objective for this program.
- 2. Function Prototypes
- Function prototypes tell the compiler to create a definition for a function that hasn't been defined yet. Stylistically I find it easier to add prototypes for my functions at the head of the file. This allows me to add functions on the fly as I develop. If I have a project with multiple project files I move the function prototype into a header file that I include in the files that use the functions I defined.
- 3. Type Definitions
- Place where to put typedef decelerations.
- 4. File Variables
- File variables are a style choice. Your style will probably be different than mine. If you decide to use file variables in your programs I recommend declaring them in one place in your source file.
- 5. function definitions
- Implementation of the functions you declared as prototypes.