Presentation is loading. Please wait.

Presentation is loading. Please wait.

S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan.

Similar presentations


Presentation on theme: "S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan."— Presentation transcript:

1 S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

2 Overview (Midterm) Midterm Examination 30 % Data Type, Declaration, Display Interactive Input SelectionRepetition Analyze Problem Solving Problem Solving int float double char printf() int float double char printf() scanf() if…else switch…case if…else switch…case while statement for statement do-while statement while statement for statement do-while statement

3 Intro. C Programming 1 The Syntax 2 C Editor Tool 3 Today’s Overview 3

4 Algorithms A procedure for solving a problem in terms of the actions to be executed and the order. Algorithm can be written in 2 different ways Pseudo-code – English-like steps that describes the solution Flowcharts – Picture with specific blocks detailing out the logical flow of the solution Problem solving Understand problem statement and analysis Develop a high-level algorithm Detail out a low-level algorithm

5 Pseudo-Code An artificial and informal language that helps programmers develop algorithms. Pseudo code is a "text-based" detail (algorithmic) design tool.

6 Flow Chart A formalized graphic representation of a logic sequence, work or manufacturing process, organization chart, or similar formalized structure. Simple geometric symbols and arrows to define relationships.

7 Flowchart Symbols

8 A First Book of ANSI C, Fourth Edition 8 Flowchart Symbols (Full)

9 Programming Languages

10 A programming language is a way for humans to write instruction code for computers. Computer communicate in binary that is ones and zeros. An example would look like this...

11 Programming Languages This is very hard to read and under stand so we write code that looks more like human words and would look like this...

12 Program Translation

13 What is C ? C is a programming language originally developed for developing the Unix operating system (by Dennis Ritchie).Dennis Ritchie The pure programming language with no additional concept. 13

14 The first C Program 14

15 Functions

16

17 The main() Function Sometimes referred to as a driver function

18 C Libraries - #include …

19 C Libraries - #include … (cont.) Library fileFunctions #include Standard I/O functions – printf, scanf, getc, fopen, fclose #include Traditional file operations – lock, create, filelength, read, write #include Arithmetic – abs, floor, pow, sqrt Logarithmic – log, exp, Trignometric – sin, tan, cos, atan Floating point – fabs, fmod #include String operations – strcpy, strcat, strcmp, strlen, strrev, strlwr, strupr Without including these libraries, you cannot write C programs that need to use these standard function. Check your reference for details on the C libraries, their functions and how they can be used.

20 Blocks & Lines Each line of code end with ; (Only Instruction) Blocks are things that start with { and end with }. 20

21 Line Example 21

22 Block Example 22

23 The printf( ) Function Function arguments printf("Hello there world!");

24 Type of Functions 1. Standard-Library Function (built-in function): Already store in C library, a programmer can instantly use it. 2. Write by Programmer: A function created by individual programmer. 24

25 Explaining your Code - Comment Programmers can forgot their own code. Use them to explain sections of code To create a comment in C, you surround the text with /* and then */ or using // 25

26 E XPANDING THE CONCEPT OF C 26

27 Displaying Numerical Values Function arguments Arguments are separated with commas printf("The total of 6 and 15 is %d", 6 + 15); conversion control sequence

28 Displaying Numerical Values (cont.) Example printf("The average of 5,10 and 11 is %f",(5+10+11)/3); printf("The average of 5,10 and 11 is %f", avg); printf(" %d \n %f \n %c ", 10, 10.11, ‘a’);

29 Arithmetic Operations Arithmetic operators: operators used for arithmetic operations: – Addition + – Subtraction - – Multiplication * – Division / – Modulus Division % – minus sign (-) used in front of a single numerical operand negates the number

30 Operator Precedence

31 Operator Precedence (cont.) Find the result of 6 + 4 / 2 + 3 6 + 2 + 3 8 + 3 11 Find the result of 8 + 5 * 7 % 2 * 4 8 + 35 % 2 * 4 8 + 1 * 4 8 + 4 12 ***Suggestion: Use () to ensure your result. ***

32 Using Variables // Version 4 What will happen (Variable Concept) /* int main() { int sum = 5+6+7; int multi = 10*20; int total = sum+multi; printf("%d", total); getch(); return 0; } */ 32 Declaration

33 Data Types Data type: define how the compiler identifies variables. Four basic data types used in C – Integer (int) – Floating point (float) – Double precision (double) – Character (char)

34 Data Types: Integer are any positive or negative number without a decimal point may be signed (+ or -) or unsigned no commas, decimal points, or special symbols Valid integer constants 5 -10 +25 1000 253 -26351 +36 Invalid integer constants $255.62 2,523 3. 6,243,892 1,492.89 +6.0

35 Data Types: Character Characters are letters of the alphabet (uppercase & lowercase) char Data Type is used to store individual characters. Include the letters of the alphabet (both uppercase and lowercase), the ten digits 0-9, and special symbols such as +,&,! Examples of valid character constants ‘A’ ‘$’ ‘b’ ‘7’ ‘y’ ‘!’ ‘M’ ‘q’ enclosed by single quotes (‘ ’)

36 ASCII Codes.

37 Data Types: Character

38 Escape Sequences

39 Data Types: Floating Point & Double Precision umbers are any signed or unsigned numbers having a decimal point no special symbols Valid floating point and double precision constants +10.6255 5. -6.2 3251.92 0.0 0.33 -6.67 +2. Invalid floating point and double precision constants 5,326.25 24 123 6,459 $10.29 Floating Point VS Double Precision Numbers

40 Displaying Numerical Values (cont.) Example printf("The average of 5,10 and 11 is %f",(5+10+11)/3); printf("The average of 5,10 and 11 is %f", avg); printf(" %d \n %f \n %c ", 10, 10.11, ‘a’);

41 Variables and Declarations Variables are names given by programmers Variable names are case sensitive Rules of variable names – begin with letter or underscore (_) and may contain only letters, underscores, or digits – cannot contain any blanks, special symbols – use capital letters to separate names consisting of multiple words – cannot be a keyword – no more than 31 characters

42 Variables and Declarations (cont.)  general form function name( ) { declaration statements; other statements; }  simplest form dataType variableName;  Example void main() { int X,Y; float HOLA; char MyChar, mychar; other statements; }

43 Initialization and Assign value When a declaration statement provides an initial value, the variable is said to be initialized – int x = 10; – float y = 1.2; Assign value: right hand side to left hand side – int x; – x = 10; – x = 5+10;

44 Common Programming Errors Omitting the parentheses after main main  main( ) Omitting or incorrectly typing the opening brace { that signifies the start of a function body Omitting or incorrectly typing the closing brace } that signifies the end of a function Misspelling the name of a function print( )  printf( )

45 Common Programming Errors (cont.) Forgetting to close the message to printf( ) with a double quote (“ ”) symbol Omitting the semicolon (;) at the end of each statement Forgetting the \n to indicate a new line

46 ? || // 46

47 Program: Turbo C/DevC++/Etc. 47

48 DevC++ Demonstration 48

49 Y OUR T URN 49


Download ppt "S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan."

Similar presentations


Ads by Google