[Back to PGM'g/SCRIPT'g MAIN page]  [^^Up to pizoig MAIN page]

pizoig: Pgming

On this page: {Introduction}

Intro

void main () { int i; /** Let's count to 10 **/ printf ("--- Hi, scriptors!\n"); for (i=1; i <= 10 ; i++) { printf (" %2d\n", i); } printf ("-- That's all folks!\n"); exit(0); } That's pretty much 90% of ALL scripting languages (well most of them). And of course the "and the rest" is the OTHER 90% of it all....

What is programming?

It's simply a way of systemaatically telling a computer how to do things. A program is like a receipe for mking a cake. Nicolas Wirth (the father of the Pascal Language) sez: Algorithms + Data Structures = Programs Algroithms are the directions Data Struction are the ingredients that we act on. But, more on that later....

Constants and Variables

If you are familiar withj algebra (which doesn;t really help in programming), then you already have an idea what these are. 1, 2, 3, pi, 1.6 are constants x, a, C0, C1 are variables (also known in mathspeak as "parameters") Unlike algebra, there are no real equations, but a tjhing callled statements: In algebra we have ax2 + bx + c = 0 (THE "quadratic equation") in the above equation "x" is the variable and a,b,c are the parameters of the equation. We usually, give values to the parameters; eg, 2x2 + 6x + 4 = 0 Thus, a=2 b=6 and c=4 We "solve" the equation for the values of x which make the equation "true" (it turns out that there are two values for x which "satify" the equation: x = -1 and x = -2 ) Note that the above is a special case of a more genereral equation. In algebra we write the general equation as: y = ax2 + bx + c Where we want to calculate y for a given value of x. We can (for a given set of parameters) make a table of x and y (or simply plug in a value of x and get the corresponding value of y). We can also make a graph of the function (also known as "plotting x vs y"). Formally in algebra we would say that "Y is a function of X" That is, given a value of x, we can calcualte a corresponding value of y. Most of the original motivation for programming was to solve equations like the above -- more so: to solve even more complicated equations. Note: Technically speaking, when we "solve" the equations, we are CALCULATING values for x and y -- not programming. Say we needed a table of all values of y corresponding to the values of x from 0.001 thru 199.999 (in increments of 0.001) -- before computer programming, a room full of people with old-style (mechanical) adding machines would have been busy for MONTHS creating the tables of x and y. Notice that the table will have 200,000 entries; and this is a very simple equation. We now abandon algebra. Programming attempts to make massive calcultions easier and more reliable. In the long hsitory of computation there are many extraordinary stories most interesting of which are the fact that over 200 years ago Charles babbage tried to make a mechanical computer (called the "analytical engine" -- you can immagine it next one of James Watt's steam engines "chugging along thru a complex eqaution"). Babbage's friend and confidant (most people at the time thought the entire project quite mad), was Lady Ada Augusta, Countess of Lovelace who was the daughter of Lord Byron (best know as a poet and friend of Percy and Mary Shelly). Ada was the Earth's first computer progammer; alas, Babbage's project fell thru and the engine was never completed (financial and enginering difficulties) -- oddly enough in the 20th century engineers DID construct the engine: Natch, it works. Meanwhile in the 20th century, Navy Commander Grace Hopper was put in charge of a computer (filling a room) to do calculations. It used relays and "electon tubes" (pretty much the size of one of those "candelabra" light bulbs in a candellabra). During one calculation, the computer stopped. Turns out that a moth had flown into a tube (or realay) and ZAPP! In her report, Commmander Hopper reported the problem and that to solve it, they had had to "de-bug the computer" -- true story. (The world's first bug zapper -- if only they had known to patent it! ;) Unlike algebra, there are no real equations, but a thing called statements. Here is an actual c program with statemetns that create our table for us (we'll be discussing it next)... void main (void) { /* calculate a table of values */ /* this is my program: so there! a "comment" Hello, Earthlings! */ /*--- create some variables ---*/ float x, y, a, b, c; /* variables (x and y) and parms a,b,c */ /*---- assign values to the quadratic equations co-efficients a,b,c */ a = 2.0; b = 6.0; c = 4.0; /*--- print a HEADING for the tab;e -----*/ printf ("== x == ==== Y === \n"); /*---- loop thru all of the values in the table ---*/ for (x=0.001 ; x += 0.001 ; x < 200.000) { y = a*x*x + b*x + c' printf ("%7f.3, 3x, %10f.3 \n", x, y); } printf ("\n That's all folks!! \n\n"); } It takes (a skilled) programmer about 2 minutes to write the above program (ie, tom type the c statements into an editor file) and it takes a (modern) computer about 30 seconds to calculate, put it might take about 1-1/2 hours to print out! (about 4,000 pages) And now for the "blow by blow"...

Funtions

Every program has a FUNCTION called "main" often written as main() A function returns a value. Just like when you punch in 2 on your calculator and hit SQUARE ROOT, it returns the value of the function; approx 1.414 or so. In c speak we would write: x = sqrt (2.0); printf ("The square root of 2 is %6f.4\n", x); In this case the argument is 2.0 -- that is, the value (parameter) that we send to the square root function. Let's take a brief aside about the printf function. "printf()" - print formated. Its format is: printf ( format-string, optional-variable-list ); printf ("\n"); /* prints a blank line \n = new-line character */ /* this is a comment and is ignored by the computer */ /* we put comments in so that the programmers (maybe */ /* even us) knows whats going on. wibble! */ i = 2; printf ("%5d, i); /* prints a DECIMAL (base 10) number into a "slot" */ /* 5 spaces wide from the current value of the */ /* variable "i" */ x = 2.004; printf ("6f.4", x); /* floating point slot, 6 spaces wide with four */ /* digits after the decimal point 2.0040 */ i = 7; j = 8; sum = i + j; printf (" %2d + %2d = %2d \n", i, j, sum); would print as 7 + 8 = 15 Got it? (well, hopefully for now). And back to maiN() The main() function is always the first function called -- every program MUST have one (and only one). Now remember, if every function returns a value, then what if we DON'T WANT it to return a value, we just want it to do something (like printf() -- we want it to print!!) pizoig: Programming WorkShop (pgming wk/s)

[pgm'g MAIN page] [^^pizoig HOME page]  [C ref manual]

pizoig

Gaming for yet another new millenium!

C - Programming Work/Shop

See also: On this page: {Introduction}

Intro

This set of files is designed...

What is programming?

It's simply a way of systemaatically telling a computer how to do things. A program is like a receip for mking a cake. Nicolas Wirth (the father of the Pascal Language) sez: Algorithms + Date Structures = Programs

Constants and Variables

If you are familiar withj algebra (which doesn;t really help in programming), then you already have an idea what these are. 1, 2, 3, pi, 1.6 are constants x, a, C0, C1 are variables (also known in mathspeak as "parameters") Unlike algebra, there are no real equations, but a tjhing callled statements: In algebra we have ax2 + bx + c = 0 (THE "quadratic equation") in the above equation "x" is the variable and a,b,c are the parameters of the equation. We usually, give values to the parameters; eg, 2x2 + 6x + 4 = 0 Thus, a=2 b=6 and c=4 We "solve" the equation for the values of x which make the equation "true" (it turns out that there are two values for x which "satify" the equation: x = -1 and x = -2 ) Note that the above is a special case of a more genereral equation. In algebra we write the general equation as: y = ax2 + bx + c Where we want to calculate y for a given value of x. We can (for a given set of parameters) make a table of x and y (or simply plug in a value of x and get the corresponding value of y). We can also make a graph of the function (also known as "plotting x vs y"). Formally in algebra we would say that "Y is a function of X" That is, given a value of x, we can calcualte a corresponding value of y. Most of the original motivation for programming was to solve equations like the above -- more so: to solve even more complicated equations. Note: Technically speaking, when we "solve" the equations, we are CALCULATING values for x and y -- not programming. Say we needed a table of all values of y corresponding to the values of x from 0.001 thru 199.999 (in increments of 0.001) -- before computer programming, a room full of people with old-style (mechanical) adding machines would have been busy for MONTHS creating the tables of x and y. Notice that the table will have 200,000 entries; and this is a very simple equation. We now abandon algebra. Programming attempts to make massive calcultions easier and more reliable. In the long hsitory of computation there are many extraordinary stories most interesting of which are the fact that over 200 years ago Charles babbage tried to make a mechanical computer (called the "analytical engine" -- you can immagine it next one of James Watt's steam engines "chugging along thru a complex eqaution"). Babbage's friend and confidant (most people at the time thought the entire project quite mad), was Lady Ada Augusta, Countess of Lovelace who was the daughter of Lord Byron (best know as a poet and friend of Percy and Mary Shelly). Ada was the Earth's first computer progammer; alas, Babbage's project fell thru and the engine was never completed (financial and enginering difficulties) -- oddly enough in the 20th century engineers DID construct the engine: Natch, it works. Meanwhile in the 20th century, Navy Commander Grace Hopper was put in charge of a computer (filling a room) to do calculations. It used relays and "electon tubes" (pretty much the size of one of those "candelabra" light bulbs in a candellabra). During one calculation, the computer stopped. Turns out that a moth had flown into a tube (or realay) and ZAPP! In her report, Commmander Hopper reported the problem and that to solve it, they had had to "de-bug the computer" -- true story. (The world's first bug zapper -- if only they had known to patent it! ;) Unlike algebra, there are no real equations, but a thing called statements. Here is an actual c program with statemetns that create our table for us (we'll be discussing it next)... void main (void) { /* calculate a table of values */ /* this is my program: so there! a "comment" Hello, Earthlings! */ /*--- create some variables ---*/ float x, y, a, b, c; /* variables (x and y) and parms a,b,c */ /*---- assign values to the quadratic equations co-efficients a,b,c */ a = 2.0; b = 6.0; c = 4.0; /*--- print a HEADING for the tab;e -----*/ printf ("== x == ==== Y === \n"); /*---- loop thru all of the values in the table ---*/ for (x=0.001 ; x += 0.001 ; x < 200.000) { y = a*x*x + b*x + c' printf ("%7f.3, 3x, %10f.3 \n", x, y); } printf ("\n That's all folks!! \n\n"); } It takes (a skilled) programmer about 2 minutes to write the above program (ie, tom type the c statements into an editor file) and it takes a (modern) computer about 30 seconds to calculate, put it might take about 1-1/2 hours to print out! (about 4,000 pages) And now for the "blow by blow"...

Funtions

Every program has a FUNCTION called "main" often written as main() A function returns a value. Just like when you punch in 2 on your calculator and hit SQUARE ROOT, it returns the value of the function; approx 1.414 or so. In c speak we would write: x = sqrt (2.0); printf ("The square root of 2 is %6f.4\n", x); In this case the argument is 2.0 -- that is, the value (parameter) that we send to the square root function. Let's take a brief aside about the printf function. "printf()" - print formated. Its format is: printf ( format-string, optional-variable-list ); printf ("\n"); /* prints a blank line \n = new-line character */ /* this is a comment and is ignored by the computer */ /* we put comments in so that the programmers (maybe */ /* even us) knows whats going on. wibble! */ i = 2; printf ("%5d, i); /* prints a DECIMAL (base 10) number into a "slot" */ /* 5 spaces wide from the current value of the */ /* variable "i" */ x = 2.004; printf ("6f.4", x); /* floating point slot, 6 spaces wide with four */ /* digits after the decimal point 2.0040 */ i = 7; j = 8; sum = i + j; printf (" %2d + %2d = %2d \n", i, j, sum); would print as 7 + 8 = 15 Got it? (well, hopefully for now). And back to maiN() The main() function is always the first function called -- every program MUST have one (and only one). Now remember, if every function returns a value, then what if we DON'T WANT it to return a value, we just want it to do something (like printf() -- we want it to print!!)