09-05-2012, 11:45 AM
C Programming Array-String-Pointer
c-C2.ppt (Size: 152 KB / Downloads: 35)
Beauty is truth and truth beauty
A program which is neatly formatted is easier to understand
If you're staring in confusion at a load of {s and }s and don't know whether you need another } your program is probably UGLY
Indenting consistently is especially important. (in functions, for, while if and else).
One of our most important goals as a programmer is to write something that other people can read.
This lecture's program
The sieve of Eratosthenes is an algorithm of greek origin.
It is a relatively efficient way of constructing a list of small prime numbers
It is defined in your notes in the section "going from algorithm to program".
It is also going to be used in the next worksheet.
Choosing a data representation
When going from an algorithm to a program, one of the first questions is "How can I represent my data?".
If the program has to store anything then what is the best way to store it?
In the case of the sieve of Eratosthenes, we need to store a table of numbers.
In this case, a good storage mechanism is an array – a list of numbers.
Strings in C
When we want to store and print text in C we use a string
A string is an array of type char which has '\0' as the last character.
'\0' is a special character (like '\n') which means "stop now".
We can initialise a string using = but we can't set a string using this at other times (use sprintf)!