120 likes | 134 Views
Understanding theory, algorithms, hardware/software, errors, and programming tips in C language for efficient problem-solving.
E N D
Basic Concepts for The Numerical Calculationand Programming with C Woochang Lim Computation: Writing your program to solve your problem. When you know the problem exactly, the computation is very easy. SNPL
What is The Computation ? Computation: Doing your works with computer. Problems (Theory) Implementations (Algorithm) Transforming a theory into an algorithm requires: - significant theoretical insight, - detailed physical and mathematical understanding, - mastery of the art of programming. “The computer always do exactly as told.” Calculation using a calculator is very easy. If you think the computer as a calculator, then the calculation using a computer becomes very easy, too. SNPL
Hardware Storage Devices Input Devices HDD Permanent storage unit (Slow, Low cost) Input Devices Keyboard, Mouse, Tablet, etc. Image/Sound Input Devices Scanner, Camera, Sound card, etc. Network Devices NIC, Hub, etc. Memory Temporary storage unit (Fast, High cost) CPU Central processing unit Output Devices Backup Devices Floppy diskettes, CD-R/RW, Tapes, etc. Graphic Outputs CRT Monitors, LCD Monitors, etc. Printing Outputs Printers, Plotters, etc. SNPL
Software Operating systems (OS): Basic software for operating the systems e.g. MS-DOS, Windows 98/NT, Linux, IRIX, etc. Utilities: Small programs for specific objects e.g. WinZip, WS FTP, V3, etc. Applications: A group of programs for specific objects e.g. Word processor, spreadsheet, database, etc. Programming languages: Software to make a software e.g. C, BASIC, FORTRAN, PASCAL, etc. UI(User Interface): Similar usages of the applications those have the same UI - CUI (Character User Interface): MS-DOS, cshell, etc. - GUI (Graphic User Interface): Windows environment (MS-Word, Explorer, etc.) X-window environment (gimp, Netscape, etc.) Computer User Hardware Kernel Nucleus of the OS Shell Command interpreters Applications, Programming languages SNPL
Numbers in The Computer The size and numeric range of the data types depends on the architecture of the host computer. In the 64 bit architecture, the long double precision (128 bits) are available. SNPL
Errors “Errors are a part of computation.” Types of errors Blunders: Typographical errors, running the wrong program, using the wrong data file, and so on. Approximation errors: Those arising from simplifying the mathematics Roundoff errors: Those arising like the uncertainty in the measurement of a physical quantity encountered in an physics laboratory. Because any stored number is represented by a finite number of bits the set of numbers that the computer can store exactly, machine numbers, is much smaller than the set of real numbers. Random errors: Those caused by events such as fluctuation in electronics due to power surges, cosmic rays, etc. SNPL
Programming Tips Tips for the design of program Modularity: Break up the tasks of a program into subprograms Write many small subprograms. Give each subunit well-defined input and output Make each subprogram independent of the others Choose the most reliable and simple algorithm Be clear and simple Keep the flow linear with a minimal amount of jumping around Easy to use Tips for writing codes Easy to read: Use descriptive names for variables and subroutines. Insert comments. Easy and safe to modify for different systems: Use the standard version of the Programming languages. Avoid the global variables. Give the correct answer. SNPL
Programming with C Advantages of C Languages Powerful and flexible languages Portable languages Modularity Processes of program development 1. Edit and save the source code using the editor (ASCII) 2. Compile the source code: make an object code e.g. Turbo C: Ctrl+F9 (Compile and Run), Unix: cc filename (a.out) 3. Link: make an executable file 4. Run it. e.g. Unix: ./a.out main() function: main body of program written by C main() { } SNPL
Generic Program Written by C Comments /* . . . */ Compiler ignores them. Include header files: #include < . . . .h> #include “ . . . .h” Variables declaration: - Global variables - Local variables Subroutines - Return the value - void type Print the output on the screen printf(“%20.16lf, var) /* This is an example */ #include <stdio.h> #include <math.h> double x,y; double ADD(double x,double y); void PRINT(double sum); void main() { double sum; x=3.0;y=2.0; sum=ADD(x,y); PRINT(sum); } /* Add x and y */ double ADD(double x,double y) { double c; c=x+y; return c; } /* Print the output on the screen */ void PRINT(double sum) { printf(“%20.16lf\n”,sum); } SNPL
Control Structures in C if statements: If “conditions” is TRUE, then run “statements.” If “conditions” is FALSE, then run “statements2.” if (conditions) {statements;} if (conditions) {statements;} else {statements2;} for statements: i starts at 1, and run “statements” until i is imax. for (i=1; i<=imax; i++) {statements;} while statements: Run “statements” while “conditions” is TRUE. while (conditions) {statements;} do … while statements: Run “statements” while “conditions” is TRUE. do {statements;} while (condition); SNPL
Input and Output in C Input and output to screen scanf(“%f”, &var); printf(“output text”); printf(“text=%f\n”, var); Input and output to files FILE *fout, *fin; /* Declare the I/O stream. */ fout = fopen(“out.dat”,”w”); /* Create “out.dat” for writing. */ fin = fopen(“in.dat”, “r”); /* Open “in.dat” for reading only. */ fprintf(fout, “%f\n”, var); /* Write data to output file stream */ fscanf(fin, “%f”, &var); /* Write data to input file stream */ SNPL
Operators in C ==: equal &&: AND !=: not equal ||: OR !: NOT ++: increase 1 --: decrease 1 Format Specifiers and Escape Sequences Escape sequences \a: beep, alert. \b: backspace \n: new line \t: tab \\: back slash \?: ? (question mark) \’: ’ (quotation mark) Format specifiers %c: char %d: int, short %ld: long %f: float, double %lf: long double SNPL