Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "BIL 104E Introduction to Scientific and Engineering Computing Lecture 2."— Presentation transcript:

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

2 Scientific Notation Floating-point value: Can represent integer and non-integer values such as 2.5, -0.004, 15.0. 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 10.0. Example: 25.6 = 2.56x10^1-0.004 = -4.0x10^-3 1.5 = 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.56e1-0.004 = -4.0e-3 1.5 = 1.5e0 Precision: The number of digits allowed by the computer for the decimal portion of the mantissa determines the precision or accuracy. Example: 35.004 has 4 digits of precision Range: The number of digits allowed for the exponent determines the range. 13.06.2016Lecture 22

3 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. 13.06.2016Lecture 23

4 13.06.2016Lecture 24 For most systems ranges are: INTEGERSMinMax short-3276832767 int-3276832767 long-2147483648-2147483647 unsigned short065535

5 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. 13.06.2016Lecture 25

6 13.06.2016Lecture 26 Floating point numbers FLOATING POINT NUMBERS PrecisionMax Exponent Maximum Value float6 digits383.402823e+38 double15 digits3081.797693e+308 long double19 digits49321.189731e+4932

7 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. 13.06.2016Lecture 27

8 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 13.06.2016Lecture 28

9 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. 13.06.2016Lecture 29

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

11 precision Specifier SpecifierValue Printed (□ represents blank) %f157.892600 %6.2f157.89 %+8.2f□+157.89 %7.5f157.89260 %e1.578926e+02 %.3E1.579E+02 %g157.893 13.06.2016Lecture 211

12 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 13.06.2016Lecture 212

13 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); 13.06.2016Lecture 213

14 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 13.06.2016Lecture 214

15 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. 13.06.2016Lecture 215

16 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, \n000101010 %010010137 3011001151 A100000165 13.06.2016Lecture 216

17 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: 97 13.06.2016Lecture 217

18 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 13.06.2016Lecture 218

19 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: 13.06.2016Lecture 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; }

20 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: 13.06.2016Lecture 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; }


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

Similar presentations


Ads by Google