Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

High School Mathematics Extensions/Mathematical Programming/Sample C Program

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

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. Explanation What this program does. Main() 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 initialize the variable command. Call the function execute_command to process the command. init() Sets done to FALSE. input_message() Prints the message "Press a key to continue: \n" to the console. input() Reads a character from the console. execute_command(command) 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 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 ../Processing Commands/ ../The Complete Program/