BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.

Slides:



Advertisements
Similar presentations
Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Advertisements

CSC Programming for Science Lecture 5: Actual Programming.
Lecture 2 Introduction to C Programming
Outline 2.1 Introduction 2.2 Basics of C Programs
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
1 CSE1301 Computer Programming: Lecture 9 Input/Output.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
Spring 2005, Gülcihan Özdemir Dağ Lecture 4, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 4 Outline 4.0 Reading.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Introduction to programming Language C, Data Types, Variables, Constants. Basics of C –Every program consists of one or more functions and must have main.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© 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.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
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.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 Pearson Education, Inc. All rights reserved Formatted Output.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Input & Output: Console
C Programming Lecture 4 : Variables , Data Types
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
Course «Programming in C++» Introduction To Programming for Engineering & Science for students of DonNTU Prykhodko Tatyana Alexandrovna docent of CST,
Week 1 Algorithmization and Programming Languages.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
PHY 107 – Programming For Science. The Week’s Goal.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
CSE1301 Computer Programming: Lecture 6 Input/Output.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
28 Formatted Output.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Formatted Input and Output
ECE Application Programming
Input/output.
TMF1414 Introduction to Programming
Introduction to C CSE 2031 Fall /3/ :33 AM.
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.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Formatted Input/Output
Chapter 9 - Formatted Input/Output
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Introduction to C EECS May 2019.
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Presentation transcript:

BIL 104E Introduction to Scientific and Engineering Computing Lecture 2

Scientific Notation Floating-point value: Can represent integer and non-integer values such as 2.5, , Scientific notation: A floating-point number is expressed as a mantissa times a power of ten, where mantissa has an absolute value greater than or equal to 1.0 and less than Example: 25.6 = 2.56x10^ = -4.0x10^ = 1.5x10^0 In exponential notation letter e is used to separate the mantissa from the exponent of the power of ten. Example: 25.6 = 2.56e = -4.0e = 1.5e0 Precision: The number of digits allowed by the computer for the decimal portion of the mantissa determines the precision or accuracy. Example: has 4 digits of precision Range: The number of digits allowed for the exponent determines the range Lecture 22

Numeric Data Types In C, numeric values are either integers or floating-point values. There are also non-numeric data types (such as characters) which will be discussed later. Integers: Specified by short, int and long according to the required range. Ranges of values are system dependent. C also allows unsigned qualifier where unsigned integer represents only positive values. Signed and unsigned integers represent same number of values but the ranges are different Lecture 23

Lecture 24 For most systems ranges are: INTEGERSMinMax short int long unsigned short065535

Numeric Data Types Floating point numbers: Specified by float (single-precision), double (double- precision), and long double (extended precision) according to the required precision and range which are also system dependent Lecture 25

Lecture 26 Floating point numbers FLOATING POINT NUMBERS PrecisionMax Exponent Maximum Value float6 digits e+38 double15 digits e+308 long double19 digits e+4932

printf Function The preprocessor directive #include gives the compiler the information that it needs to check referenced to the input/output functions in the Standard C library. printf function allows to print to the screen. Example: printf("Angle = %f radians \n",angle); The first argument which is enclosed in double quotation marks is the control string. The control string can contain text or conversion specifiers or both. – The conversion specifier ( in the example it is %f ) describes the format to use in printing the value of a variable. – The newline indicator (\n) causes a skip to a new line on the screen after the information has been printed. The second argument is the variable which is matched to the conversion specifier in the control string Lecture 27

Specifiers for Output Variable TypeOutput TypeSpecifier for output INTEGER VALUES short, intint%i (integer), %d (decimal) intshort%hi, %hd long %li, %ld intunsigned int%u intunsigned short%hu longunsigned long%lu FLOATING- POINT VALUES float, doubledouble%f (floating-point), %e (exponential form), %E (exponential form), %g (general), %G (general) long double %Lf, %Le, %LE, %Lg, %LG Lecture 28

minimum field width Specifier minimum field width specifier, which may be given between the percent sign (%) and the letter in a format specifier, ensures that the output reaches the minimum width. For example, %10f ensures that the output is at least 10 character spaces wide. If the field width specifies more positions than are needed for the value, the value is printed right-justified, which means that the extra positions are filled with blanks on the left of the value. To left-justify a value, a minus sign is inserted before the field width Lecture 29

minimum field width Specifier SpecifierValue Printed (□ represents blank) %i-145 %4d-145 %3i-145 %6i□□-145 %06i %-6i-145 □□ Lecture 210

precision Specifier SpecifierValue Printed (□ represents blank) %f %6.2f %+8.2f□ %7.5f %e e+02 %.3E1.579E+02 %g Lecture 211

Escape Character, backslash (\) SequenceCharacter Represented \b backspace, moves cursor to the left one character \f formfeed, goes to the top of a new page \n newline \r carriage return, returns to the beginning of the current line \t horizontal tab \v vertical tab \\ backslash \"\" double quote Lecture 212

scanf Function scanf function allows to enter values from the keyboard while the program is being executed. The first argument of the scanf function is a control string that specifies the types of the variables whose values are to be entered from the keyboard. The remaining arguments are the memory locations that correspond to the specifiers in the control string. The memory locations are indicated with the address operator (&). Example: scanf("%i",&year); printf("Enter the distance (m) and velocity (m/s): \n"); scanf("%lf %lf", &distance, &velocity); Lecture 213

Specifiers for Input Variable TypeSpecifier of Input int%i, %d short%hi, %hd long int%li, %ld unsigned int%u unsigned short%hu unsigned long%lu float%f, %e, %E, %g, %G double%lf, %le, %lE, %lg, %lG long double%Lf, %Le, %LE, %Lg, %LG Lecture 214

Character Data Numeric information is represented in a C program as integers or floating-point values. Numeric values are often used in arithmetic computations. Nonnumeric information may consist of alphabetic characters, digits, and special characters. Each character corresponds to a binary code value. The most commonly used binary codes are ASCII (American Standard Code for Information Interchange) and EBCDIC (Extended Binary Coded Decimal Interchange Code). A total of 128 characters can be represented in the ASCII code Lecture 215

char Data Type Note that the binary representation for a character digit is not equal to the binary representation for an integer digit. Nonnumeric information can be represented by constants or by variables. – A character constant is enclosed in single quotes, as in 'A', 'b', and '3'. – A variable that is going to contain a character can be defined as an integer or as a character data type (char). CharacterASCII CodeInteger Equivalent newline, \n % A Lecture 216

Character Initialization – The binary representation for a character can be interpreted as a character or as an integer. – To print a value as an integer, the %i or %d specifier is used; to print a value as a character, the %c specifier is used. – Example: int k=97; char c='a'; printf("value of k: %c; value of c: %c \n",k,c); printf("value of k: %,i; value of c: %i \n",k,c); Output: value of k: a; value of c: a value of k: 97; value of c: Lecture 217

Reading and Printing Characters – A text stream is composed of sequence of characters. – The end of a text stream is indicated with a special value, EOF, which is a symbolic constant defined in stdio.h. – stdin:The standard input for reading (usually keyboard) – stdout:The standard output for writing. (usually monitor) – stderr:The standard error for writing error messages. (always monitor) – Although the printf and scanf functions can be used to read characters using the %c specifier, there are special functions for reading and printing characters: The getc() function The putc() function The getchar() function The putchar() function Lecture 218

getc() and getchar() – The int getc(FILE *stream) function reads the next character from a file stream, and returns the integer value of the character as the function value. – The int getchar(void) function reads a character from the standard input and returns the integer value of the character as the function value. It is equivalent to getc(stdin). – Example: Lecture 219 #include main() { int ch1, ch2; printf("Enter two characters from the keyboard:\n "); ch1=getc(stdin); ch2=getchar(); printf("The first character you entered is: %c\n",ch1); printf("The second character you entered is: %c\n ",ch2); return 0; }

putc() and putchar() – The int putc(int c, FILE *stream) function prints the character that corresponds to the integer argument to the specified file stream. It then returns the same character as the function value. – The int putchar(int) function prints the character that corresponds to the integer argument to the computer screen. It then returns the same character as the function value. – Example: Lecture 220 #include main() { int ch1=65, ch2=98; printf("The character that has numeric value of %d is: ",ch1); putc(ch1,stdout); putc('\n',stdout); printf("The character that has numeric value of %d is: ",ch2); putchar(ch2); putchar('\n'); return 0; }