Presentation is loading. Please wait.

Presentation is loading. Please wait.

S CCS 200: I NTERACTIVE I NPUT Lect. Napat Amphaiphan.

Similar presentations


Presentation on theme: "S CCS 200: I NTERACTIVE I NPUT Lect. Napat Amphaiphan."— Presentation transcript:

1 S CCS 200: I NTERACTIVE I NPUT 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 More Syntax 1 ConstantConstant 2 Interactive Input 3 Today’s Overview 3

4 Identifiers Identifiers in C consist of three types: – Reserved words – Standard identifiers – Programmer-created identifiers

5 Identifiers Reserved Word

6 Identifiers Standard Identifiers

7 Identifiers Programmer-created identifiers Programmer-created identifiers: selected by the programmer – Also called programmer-created names – Used for naming data and functions – Must conform to C’s identifier rules – Can be any combination of letters, digits, or underscores (_) subject to the following rules: First character must be a letter or underscore (_) Only letters, digits, or underscores may follow the initial character Blank spaces are not allowed Cannot be a reserved word

8 F ORMATTED O UTPUT - H OW TO FORMAT THE OUTPUT PRINT BY PRINTF FUNCTION (I/O)

9 Formatted Output Field width specifiers – to control the format of numbers Example of integer numbers printf(“The sum of%3d and%4d is%5d.”, 6, 15, 21); The sum of __6 and __15 is ___21. – Each integer is right-justified within the specified field

10 Formatted Output (cont.)

11 Formatted Floating Point Numbers Require two field width specifiers – determines the total display width, including the decimal point – determines how many digits are printed to the right of the decimal point Example printf(“|%10.3f|”, 25.67);  |____25.670| The bar character (|) is used to mark the beginning and end of the display field

12 Formatted Output (cont.)  Left Justification  to force the output to left-justify  uses a minus sign (-) format modifier  Example printf(“|%-10d|”,59); |59________|

13 Formatted Output (cont.)  Explicit Sign Display  to force both positive and negative signs to be displayed  uses a plus (+) format modifier  Example printf(“|%+10d|”,59);  printf(“|%-+10d|”,59);  |_______+59| |+59_______|

14 Assignment Variations

15 Assignment Variations (cont.) Assignment expressions like sum = sum + 10 can be written using the following operators: – += -= *= /= %= sum = sum + 10 can be written as sum += 10 price *= rate is equivalent to price = price * rate price *= rate + 1 is equivalent to price = price * (rate + 1)

16 Mathematical Library Functions Must include #include

17 Mathematical Library Functions (cont.)

18 Symbolic Constants C allows you to define the value once by equating the number to a symbolic name – #define SALESTAX 0.05 – #define PI 3.1416 – Also called symbolic constants and named constants Syntax: #define identifier value

19 I NTERACTIVE I NPUT - F OR THE PAST PROBLEM, YOU ‘FIX’ VALUE OF EACH VARIABLE IN THE CODE.

20 A NY CHANGE IN THE VARIABLE VALUE, YOU NEED TO OPEN YOUR CODE AND RE - WRITTEN IT !!! 20

21 “I WANT THE USER TO INSERT THE VALUE WHILE KEYBOARD ” 21

22 Interactive Input scanf() is used to enter data into a program while it is executing; the value is stored in a variable – It requires a control string as the first argument inside the function name parentheses

23 Interactive Input (cont.) scanf() will receive inputs from the user via keyboard, the user must press ‘enter’ to tag the end of data. scanf() requires that a list of variable addresses follow the control string Syntax: scanf("%d", &num1);

24 Interactive Input (cont.) Syntax: scanf(control, argument-list) ; 1.argument-list: a variable to keep an input value (must be preceded with ‘&’). More than 1 value, must be used with ‘,’ 24

25 Interactive Input (cont.) 2. Control: a format as in the follow table, it must be preceded with ‘%’. 25 SymbolFormat %dInteger %fFloat %cCharacter %sText %uUnsigned integer

26 Interactive Input (DEMO) EX1. Using scanf() with %d – scanf ("%d", &x); EX2. Using scanf() with 2 numbers using %d (integer) – scanf ("%d,%d",&x1,&x2); 26

27 Interactive Input (DEMO) EX3. Using scanf() with format %d with a text massage to describe input (a prompt) – printf("Input first number:"); – scanf("%d",&x1) EX4. Using scanf() to receive 3 characters – scanf("%c%c%c",&i, &j, &k); 27

28 Interactive Input (DEMO) EX5. Using scanf () to receive a text massage to describe input – printf("Enter your name:\n"); scanf("%s",&name); 28

29 ? || // 29


Download ppt "S CCS 200: I NTERACTIVE I NPUT Lect. Napat Amphaiphan."

Similar presentations


Ads by Google