Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1061 C Programming Lecture 3: The Programming Environment + Introduction to the Concept of an Algorithm A. O’Riordan, 2004.

Similar presentations


Presentation on theme: "CS1061 C Programming Lecture 3: The Programming Environment + Introduction to the Concept of an Algorithm A. O’Riordan, 2004."— Presentation transcript:

1 CS1061 C Programming Lecture 3: The Programming Environment + Introduction to the Concept of an Algorithm A. O’Riordan, 2004

2 The Programming Environment Initially, a programmer writes a program in the C programming language. This form of the program is called the source program, or source code. A program can be written in one or more files. C has a pre-processor which modifies souce code (so-called directives trigger the preprocessor). The compiler translates the source code into a form called object code. (This will take a few moments.) a linker is a program that combines object files to form an executable program. Library routines are also “linked in.” Now an execuable is produced which can be run.

3 Phases of Program Construction Initially the program requirements need to be defined clearly and the program designed. Then programming begins. Phases of C Program Construction: Edit Preprocess Compile Link and Load (into memory) Execute This is a iterative process while the program is in development.

4 Compilation Process f1.cf2.c f2.of1.o prog.exe f3.c pre-processor compiler linker Library

5 Editing and Debugging Source code in written using an editor (simplied word processor suitable for programming). typical functionality includes cut-and-paste, go to line number, find and replace, etc. A special program called a debugger can used to find errors (bugs). A debugger allows a programmer to stop a program at any point and examine and change the values of variables. An error or defect in software that causes a program to malfunction.

6 Compiler Messages A compiler warning indicates you've done something “dangerous”, but will not prevent the code from being compiled. You should fix whatever causes warnings since they often lead to other problems that will not be so easy to find. A compiler error indicates something that must be fixed before the code can be compiled. Example: You forget a semi-colon ( ; ) at the end of a statement fix the first few errors since they may be causing all the rest.

7 Linker and Run-time errors If you receive a linker error, it means that your code compiles fine, but that some function or library that is needed cannot be found. Run-time errors only occur when you run a program, i.e. executable crashes. example: trying to divide by zero int scores = 500, num = 0, avg; avg = scores / num; example: memory error (segmentation fault) These occur when you try to access memory that your program is not allowed to use or that doesn't exist

8 concept of an Algorithm A formula or set of steps for solving a particular problem. For example, a recipe for baking a cake is an algorithm. Programming examples include: sorting a list of names searching for a record in a file record. Mathematics has many such procedures that are implemented as an algorithm: identify all prime numbers up to a given number n (Sieve of Eratosthenes) The word algorithm in English comes from the Arab mathematician al- Khowarizmi who flourished at the end of the 8th Century A.D.

9 Sieve of Eratosthenes 1. Write down the numbers 1, 2, 3,..., n. We will eliminate composites by marking them. Initially all numbers are unmarked. 2. Mark the number 1 as special (it is neither prime nor composite). 3. Set k=1. Until k exceeds or equals the square root of n do this: 1. Find the first number in the list greater than k that has not been identified as composite. (The very first number so found is 2.) Call it m. Mark the numbers 2m, 3m, 4m,... as composite. 2. m is a prime number. Put it on your list. 3. Set k=m and repeat. 4. The list gives all the primes less than n.

10 Algorithms in Engineering Engineers have many well-known algorithms for various problems. Solving Linear Equations - e.g. Gaussian Elimination Optimization - For a function A function f(x_1,...,x_n), what point p = (p_z,...,p_n) maximizes (or equivalently minimizes) the function f? Job scheduling - e.g. first in first out (FIFO) Transportation Problems - Vehicle Routing, “Travelling Salesman”, Network Flow Circuit Layout etc.


Download ppt "CS1061 C Programming Lecture 3: The Programming Environment + Introduction to the Concept of an Algorithm A. O’Riordan, 2004."

Similar presentations


Ads by Google