High School Mathematics Extensions/Mathematical Programming/Sample C Program

From Wikibooks, open books for an open world
Jump to navigation Jump to search

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.

Explanation[edit | edit source]

What this program does.

Main()

  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 initialize the variable command.
  2. Call the function execute_command to process the command.

init()

  1. Sets done to FALSE.

input_message()

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

input()

  1. Reads a character from the console.

execute_command(command)

  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 copy[edit | edit source]

The following code is valid:
 //includes
 #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()
 {
     done=0; 
 }
 void input_message()
 {
     cprintf("Press a key to continue: \n");
 }
 char input()
 {
     char read;
     read=getche();
     return(read);
 }
 void execute_command(char command)
 {
     done=1; 
 }
 void main()
 {
    char command;
    init();
    input_message();
    while(!done)
       {
        command=input();
        execute_command(command);
       }
 }


Next Step[edit | edit source]