Open main menu

High School Mathematics Extensions/Mathematical Programming/Sample C Program

< High School Mathematics Extensions‎ | Mathematical Programming

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:

  1. An explanation of the code provided on the page.
  2. Code that you should be able to paste into your editor and compile.
  3. A link to the next iteration in the development of the sample program.


What this program does.


  1. Define a character variable called command.
  2. Call the void function init to get ready to make sure the global variables are correctly set up.
  3. Call the void function input_message to prompt the user.
  4. Loop while the global variable done is set to false.
  1. Call the function input to initialze the variable command.
  2. Call the function execute_command to process the command.


  1. Sets done to FALSE.


  1. Prints the message "Press a key to continue: \n" to the console.


  1. Reads a character from the console.


  1. 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.

Code to copyEdit

  The following code is valid:
 #include <stdio.h>
 #include <conio.h>

 //function prototypes
 void init();
 void input_message();
 char input();
 void execute_command(char);

 //type definitions 

 //global variables
 char done;

 //function definitions
 void init()
 void input_message()
     cprintf("Press a key to continue: \n");
 char input()
     char read;
 void execute_command(char command)
 void main()
    char command;

Next StepEdit