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

Post on 06-Jan-2018

221 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

SCCS200: INTERACTIVE INPUT

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

3

• More Syntax1

• Constant2

• Interactive Input3

Today’s Overview

Identifiers

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

IdentifiersReserved Word

IdentifiersStandard Identifiers

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

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

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

Formatted Output (cont.)

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

Formatted Output (cont.) Left Justification

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

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

|59________|

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

Assignment Variations

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)

Mathematical Library Functions

Must include #include <math.h>

Mathematical Library Functions (cont.)

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

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

20

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

21

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

Company Logo

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

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

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

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

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

28

Interactive Input (DEMO)

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

scanf("%s",&name);

29

? || //

top related