s ccs 200: i nteractive i nput lect. napat amphaiphan

29
SCCS200: INTERACTIVE INPUT Lect. Napat Amphaiphan

Upload: eustacia-payne

Post on 06-Jan-2018

221 views

Category:

Documents


3 download

DESCRIPTION

More Syntax 1 ConstantConstant 2 Interactive Input 3 Today’s Overview 3

TRANSCRIPT

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

SCCS200: INTERACTIVE INPUT

Lect. Napat Amphaiphan

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

Overview (Midterm)Midterm Examination

30 %

Data Type,Declaration,

Display

InteractiveInput Selection Repetition

Analyze

ProblemSolving

intfloatdoublechar

printf()

scanf()if…elseswitch…case

while statementfor statementdo-while statement

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

3

• More Syntax1

• Constant2

• Interactive Input3

Today’s Overview

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

Identifiers

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

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

IdentifiersReserved Word

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

IdentifiersStandard Identifiers

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

IdentifiersProgrammer-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

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

FORMATTED OUTPUT- HOW TO FORMAT THE OUTPUT PRINT BY PRINTF FUNCTION (I/O)

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

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

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

Formatted Output (cont.)

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

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

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

Formatted Output (cont.) Left Justification

to force the output to left-justify uses a minus sign (-) format modifier Example

printf(“|%-10d|”,59);

|59________|

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

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_______|

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

Assignment Variations

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

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)

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

Mathematical Library Functions

Must include #include <math.h>

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

Mathematical Library Functions (cont.)

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

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

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

INTERACTIVE INPUT- FOR THE PAST PROBLEM, YOU ‘FIX’ VALUE OF EACH VARIABLE IN THE CODE.

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

20

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

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

21

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

Company Logo

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

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

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

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

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

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 ‘,’

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

25

Interactive Input (cont.)

2. Control: a format as in the follow table, it must be preceded with ‘%’.

Symbol Format

%d Integer

%f Float

%c Character

%s Text

%u Unsigned integer

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

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

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

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

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

28

Interactive Input (DEMO)

• EX5. Using scanf () to receive a text massage to describe input– printf("Enter your name:\n");

scanf("%s",&name);

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

29

? || //