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

Slides:



Advertisements
Similar presentations
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Advertisements

Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
TDBA66, VT-03 Lecture - Ch. 21 A complete C-program Display Fig. 2.1 and comment on different things such as Preprocessor directives Header files Identifiers.
CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
41 A Depth Program #include int main(void) { int inches, feet, fathoms; //declarations fathoms = 7; feet = 6 * fathoms; inches = 12 * feet; printf(“Wreck.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
The scanf Function The scanf function reads input from the standard input device into one or more variables Example: scanf(“%lf”, &miles); Reads a real.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
Basic Input/Output and Variables Ethan Cerami New York
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Chapter 2 Getting Started in C Programming
A First Book of ANSI C Fourth Edition
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
1 IPC144 Session 11 The C Programming Language. 2 Objectives To format a #define statement correctly To use a #define statement in a C program To construct.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Chapter 2: Using Data.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Chapter 7 Formatted input and output. 7.1 introduction Tax: This result is correct; but it would be better Maybe as $13, Make formatting.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Overview of C. C—a high-level programming language developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories. We will discuss: –the elements of a.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
Arithmetic Expressions
Chapter Topics The Basics of a C++ Program Data Types
Chapter 3 Assignment and Interactive Input.
Input/output.
Basic Elements of C++.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Basic Elements of C++ Chapter 2.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Formatted Input/Output
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
INPUT & OUTPUT scanf & printf.
A First Book of ANSI C Fourth Edition
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Formatted Input/Output
Formatted Input/Output
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Getting Started With Coding
Presentation transcript:

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

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

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

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

Identifiers Reserved Word

Identifiers Standard Identifiers

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

F ORMATTED O UTPUT - H OW 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

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 – Also called symbolic constants and named constants Syntax: #define identifier value

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

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

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

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

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

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

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

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

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

? || // 29