snpl1 woochang lim computation: writing your program to solve your problem. when you know the...

12
SNPL 1 Woochang Lim Computation: Writing your program to solve your problem. When you know the problem exactly, the computation is very easy. Basic Concepts for The Numerical Calculation and Programming with C

Upload: thomasine-pierce

Post on 13-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SNPL1 Woochang Lim Computation: Writing your program to solve your problem. When you know the problem exactly, the computation is very easy. Basic Concepts

SNPL 1

Woochang Lim

Computation: Writing your program to solve your problem.When you know the problem exactly, the computation is very easy.

Basic Concepts for The Numerical Calculationand Programming with C

Page 2: SNPL1 Woochang Lim Computation: Writing your program to solve your problem. When you know the problem exactly, the computation is very easy. Basic Concepts

SNPL 2

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.

Page 3: SNPL1 Woochang Lim Computation: Writing your program to solve your problem. When you know the problem exactly, the computation is very easy. Basic Concepts

SNPL 3

Output Devices

Input DevicesStorage Devices

Hardware

CPUCentral processing

unit

MemoryTemporary storage

unit(Fast, High cost)

HDDPermanent storage

unit(Slow, Low cost)

Backup DevicesFloppy diskettes,

CD-R/RW, Tapes, etc.

Input DevicesKeyboard, Mouse,

Tablet, etc.

Image/Sound Input DevicesScanner, Camera, Sound card, etc.

Graphic OutputsCRT Monitors, LCD

Monitors, etc.

Printing OutputsPrinters, Plotters, etc.

Network DevicesNIC, Hub, etc.

Page 4: SNPL1 Woochang Lim Computation: Writing your program to solve your problem. When you know the problem exactly, the computation is very easy. Basic Concepts

SNPL 4

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.)

HardwareKernel

Nucleus of the OS

Shell

Command interpreters

Applications,Programming

languages

UserComputer

Page 5: SNPL1 Woochang Lim Computation: Writing your program to solve your problem. When you know the problem exactly, the computation is very easy. Basic Concepts

SNPL 5

Numbers in The Computer

The size and numeric range of the data types depends on the architectureof the host computer.

Data types 16 bit architecture 32 bit architecture

Fixed point(Int)

16 bits(-32,768 to 32,767)

32 bits(-2,147,483,648 to

2,147,483,647)

Floating point(float)

32 bits(3.4x10-38 to 3.4x1038)

32 bits(3.4x10-38 to 3.4x1038)

Floating point(double)

64 bits(1.7x10-308 to 1.7x10308)

64 bits(1.7x10-308 to 1.7x10308)

In the 64 bit architecture, the long double precision (128 bits) are available.

Page 6: SNPL1 Woochang Lim Computation: Writing your program to solve your problem. When you know the problem exactly, the computation is very easy. Basic Concepts

SNPL 6

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.

Nxen

x

n

xe x

N

n

n

n

nx ,

!! 00

Page 7: SNPL1 Woochang Lim Computation: Writing your program to solve your problem. When you know the problem exactly, the computation is very easy. Basic Concepts

SNPL 7

Programming Tips

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

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.

Tips for the design of program

Tips for writing codes

Page 8: SNPL1 Woochang Lim Computation: Writing your program to solve your problem. When you know the problem exactly, the computation is very easy. Basic Concepts

SNPL 8

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 file4. Run it. e.g. Unix: ./a.out

main() function: main body of program written by C main()

{}

Page 9: SNPL1 Woochang Lim Computation: Writing your program to solve your problem. When you know the problem exactly, the computation is very easy. Basic Concepts

SNPL 9

Generic Program Written by C

/* 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);}

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)

Page 10: SNPL1 Woochang Lim Computation: Writing your program to solve your problem. When you know the problem exactly, the computation is very easy. Basic Concepts

SNPL 10

Control Structures in C

if statements: If “conditions” is TRUE, then run “statements.” If “conditions” is FALSE, then run “statements2.”

for statements: i starts at 1, and run “statements” until i is imax.

while statements: Run “statements” while “conditions” is TRUE.

do … while statements: Run “statements” while “conditions” is TRUE.

if (conditions) {statements;}if (conditions) {statements;} else {statements2;}

for (i=1; i<=imax; i++){statements;}

do {statements;} while (condition);

while (conditions) {statements;}

Page 11: SNPL1 Woochang Lim Computation: Writing your program to solve your problem. When you know the problem exactly, the computation is very easy. Basic Concepts

SNPL 11

Input and Output in C

Input and output to screen

Input and output to files

scanf(“%f”, &var);printf(“output text”);printf(“text=%f\n”, var);

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 */

Page 12: SNPL1 Woochang Lim Computation: Writing your program to solve your problem. When you know the problem exactly, the computation is very easy. Basic Concepts

SNPL 12

Operators in C

==: equal &&: AND!=: not equal ||: OR

!: NOT++: increase 1--: decrease 1

Format Specifiers and Escape Sequences

Format specifiers

%c: char %d: int, short %ld: long %f: float, double %lf: long double

Escape sequences

\a: beep, alert. \b: backspace \n: new line \t: tab \\: back slash \?: ? (question mark) \’: ’ (quotation mark)