Friday, July 10, 2009

Let's Compute some Program in C Programming

Let's Compute

The following program, sine.c, computes a table of the sine function for angles between 0 and 360 degrees.


/************************/
/* Table of */
/* Sine Function */
/************************/

/* Michel Vallieres */
/* Written: Winter 1995 */
#include <>
#include <>

void main()
{
int angle_degree;
double angle_radian, pi, value;

/* Print a header */
printf ("\nCompute a table of the sine function\n\n");

/* obtain pi once for all */
/* or just use pi = M_PI, where
M_PI is defined in math.h */
pi = 4.0*atan(1.0);
printf ( " Value of PI = %f \n\n", pi );

printf ( " angle Sine \n" );

angle_degree=0; /* initial angle value */
/* scan over angle */

while ( angle_degree <= 360 ) /* loop until angle_degree > 360 */
{
angle_radian = pi * angle_degree/180.0 ;
value = sin(angle_radian);
printf ( " %3d %f \n ", angle_degree, value );

angle_degree = angle_degree + 10; /* increment the loop index */
}
}


The code starts with a series of comments indicating its the purpose, as well as its author. It is considered good programming style to identify and document your work (although, sadly, most people only do this as an afterthought). Comments can be written anywhere in the code: any characters between /* and */ are ignored by the compiler and can be used to make the code easier to understand. The use of variable names that are meaningful within the context of the problem is also a good idea.

The #include statements now also include the header file for the standard mathematics library math.h. This statement is needed to define the calls to the trigonometric functions atan and sin. Note also that the compilation must include the mathematics library explicitly by typing

 gcc sine.c -lm

Variable names are arbitrary (with some compiler-defined maximum length, typically 32 characters). C uses the following standard variable types:

int    -> integer variable
short -> short integer
long -> long integer
float -> single precision real (floating point) variable
double -> double precision real (floating point) variable
char -> character variable (single byte)
The compilers checks for consistency in the types of all variables used in any code. This feature is intended to prevent mistakes, in particular in mistyping variable names. Calculations done in the math library routines are usually done in double precision arithmetic (64 bits on most workstations). The actual number of bytes used in the internal storage of these data types depends on the machine being used.

The printf function can be instructed to print integers, floats and strings properly. The general syntax is

 printf( "format", variables );
where "format" specifies the converstion specification and variables is a list of quantities to print. Some useful formats are
%.nd integer (optional n = number of columns; if 0, pad with zeroes)
%m.nf float or double (optional m = number of columns,
n = number of decimal places)
%ns string (optional n = number of columns)
%c character
\n \t to introduce new line or tab
\g ring the bell (``beep'') on the terminal

other links
1. A First Program

2. Let's Compute

3. Loops

4. Symbolic Constants

5. Conditionals

6. Pointers

7. Arrays

8. Character Arrays

9. I/O Capabilities

10. Functions

11. Command-line Arguments

12. Graphical Interfaces: Dialog Boxes

No comments:

Blog List