1 / 21

BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTING

BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTING. Character Input / Output Formatted Input / Output Whole Lines of Input and Output. INPUT/OUTPUT (I/O). This is the lowest level of input and output. It provides very precise control, but is usually too fiddly to be useful.

Télécharger la présentation

BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTING

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. BIL 102 • INTRODUCTION • TO • SCIENTIFIC • AND • ENGINEERING COMPUTING BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTINGBy Ergin TARI

  2. Character Input / Output Formatted Input / Output Whole Lines of Input and Output INPUT/OUTPUT (I/O) BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTINGBy Ergin TARI

  3. This is the lowest level of input and output. It provides very precise control, but is usually too fiddly to be useful. Most computers perform buffering of input and output. This means that they'll not start reading any input until the return key is pressed, and they'll not print characters on the terminal until there is a whole line to be printed. Character Input / Output BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTINGBy Ergin TARI

  4. getchar getchar returns the next character of keyboard input as an int. If there is an error then EOF (end of file) is returned instead. It is therefore usual to compare this value against EOF before using it. If the return value is stored in a char, it will never be equal to EOF, so error conditions will not be handled correctly. As an example, here is a program to count the number of characters read until an EOF is encountered. EOF can be generated by typing Control - d. #include <stdio.h> main() { int ch, i = 0; while((ch = getchar()) != EOF) i ++; printf("%d\n", i); } Character Input / Output BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTINGBy Ergin TARI

  5. Character Input / Output putchar putchar puts its character argument on the standard output (usually the screen). The following example program converts any typed input into capital letters. To do this it applies the function toupper from the character conversion library ctype.h to each character in turn. #include <ctype.h> /* For definition of toupper */ #include <stdio.h> /* For definition of getchar, putchar, EOF */ main() { int ch; while((ch = getchar()) != EOF) putchar(toupper(ch)); } BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTINGBy Ergin TARI

  6. Formatted Input / Output printf This offers more structured output than putchar. Its arguments are, in order; a control string, which controls what gets printed, followed by a list of values to be substituted for entries in the control string. It is also possible to insert numbers into the control string to control field widths for values to be displayed. For example %6d would print a decimal value in a field 6 spaces wide, %8.2f would print a real value in a field 8 spaces wide with room to show 2 decimal places. Display is left justified by default, but can be right justified by putting a - before the format information, for example %-6d, a decimal integer right justified in a 6 space field. BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTINGBy Ergin TARI

  7. Formatted Input / Output The following characters, placed after the \ character in a printf() statement, have the following effect. ModifierMeaning \b backspace \f form feed \n new line \r carriage return \t horizontal tab \v vertical tab \\ backslash \" double quote \' single quote \<enter> line continuation \nnn nnn = octal character value \0xnn nn = hexadecimal value (some compilers only) BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTINGBy Ergin TARI

  8. 1: /* Demonstration of frequently used escape sequences */ 2: 3: #include <stdio.h> 4: 5: #define QUIT 3 6: 7: int get_menu_choice( void ); 8: void print_report( void ); 9: 10: main() 11: { 12: int choice = 0; 13: 14: while (choice != QUIT) 15: { 16: choice = get_menu_choice(); 17: 18: if (choice == 1) 19: printf("\nBeeping the computer\a\a\a" ); 20: else 21: { 22: if (choice == 2) 23: print_report(); 24: } 25: } 26: printf("You chose to quit!\n"); 27: 28: return 0; 29: } 30: BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTINGBy Ergin TARI

  9. 31: int get_menu_choice( void ) 32: { 33: int selection = 0; 34: 35: do 36: { 37: printf( "\n" ); 38: printf( "\n1 - Beep Computer" ); 39: printf( "\n2 - Display Report"); 40: printf( "\n3 - Quit"); 41: printf( "\n" ); 42: printf( "\nEnter a selection:" ); 43: 44: scanf( "%d", &selection ); 45: 46: }while ( selection < 1 || selection > 3 ); 47: 48: return selection; 49: } 50: 51: void print_report( void ) 52: { 53: printf( "\nSAMPLE REPORT" ); 54: printf( "\n\nSequence\tMeaning" ); 55: printf( "\n=========\t=======" ); 56: printf( "\n\\a\t\tbell (alert)" ); 57: printf( "\n\\b\t\tbackspace" ); 58: printf( "\n...\t\t..."); 59: } 1 - Beep Computer 2 - Display Report 3 - Quit Enter a selection:1 Beeping the computer 1 - Beep Computer 2 - Display Report 3 - Quit Enter a selection:2 SAMPLE REPORT Sequence Meaning ========= ======= \a bell (alert) \b backspace ... ... 1 - Beep Computer 2 - Display Report 3 - Quit Enter a selection:3 You chose to quit! BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTINGBy Ergin TARI

  10. 1: /* Demonstration using printf() to display numerical values. */ 2: 3: #include <stdio.h> 4: 5: int a = 2, b = 10, c = 50; 6: float f = 1.05, g = 25.5, h = -0.1; 7: 8: main() 9: { 10: printf("\nDecimal values without tabs: %d %d %d", a, b, c); 11: printf("\nDecimal values with tabs: \t%d \t%d \t%d", a, b, c); 12: 13: printf("\nThree floats on 1 line: \t%f\t%f\t%f", f, g, h); 14: printf("\nThree floats on 3 lines: \n\t%f\n\t%f\n\t%f", f, g, h); 15: 16: printf("\nThe rate is %f%%", f); 17: printf("\nThe result of %f/%f = %f\n", g, f, g / f); 18: 19: return 0; 20: } Decimal values without tabs: 2 10 50 Decimal values with tabs: 2 10 50 Three floats on 1 line: 1.050000 25.500000 -0.100000 Three floats on 3 lines: 1.050000 25.500000 -0.100000 The rate is 1.050000% The result of 25.500000/1.050000 = 24.285715 BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTINGBy Ergin TARI

  11. Formatted Input / Output scanf scanf allows formatted reading of data from the keyboard. Like printf it has a control string, followed by the list of items to be read. However scanf wants to know the address of the items to be read, since it is a function which will change that value. Therefore the names of variables are preceeded by the & sign. Character strings are an exception to this. Since a string is already a character pointer, we give the names of string variables unmodified by a leading &. Control string entries which match values to be read are preceeded by the percentage sign in a similar way to their printf equivalents. BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTINGBy Ergin TARI

  12. Formatted Input / Output The following characters, after the % character, in a scanf argument, have the following effect. ModiferMeaning d read a decimal integer o read an octal value x read a hexadecimal value h read a short integer l read a long integer f read a float value e read a double value c read a single character s read a sequence of characters, stop reading when an enter key or whitespace character [tab or space] [...] Read a character string. The characters inside the brackets indicate the allow-able characters that are to be contained in the string. If any other character is typed, the string is terminated. If the first characteris a ^, the remaining characters inside the brackets indicate that typing them will terminate the string. * this is used to skip input fields BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTINGBy Ergin TARI

  13. 1: /* Demonstration of using scanf() */ 2: 3: #include <stdio.h> 4: 5: #define QUIT 4 6: 7: int get_menu_choice( void ); 8: 9: main() 10: { 11: int choice = 0; 12: int int_var = 0; 13: float float_var = 0.0; 14: unsigned unsigned_var = 0; 15: 16: while (choice != QUIT) 17: { 18: choice = get_menu_choice(); 19: 20: if (choice == 1) 21: { 22: puts("\nEnter a signed decimal integer (i.e. -123)"); 23: scanf("%d", &int_var); 24: } 25: if (choice == 2) 26: { 27: puts("\nEnter a decimal floating-point number\ 28 (i.e. 1.23)"); 29: scanf("%f", &float_var); 30: } BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTINGBy Ergin TARI

  14. 31: if (choice == 3) 32: { 33: puts("\nEnter an unsigned decimal integer \ 34 (i.e. 123)" ); 35: scanf( "%u", &unsigned_var ); 36: } 37: } 38: printf("\nYour values are: int: %d float: %f unsigned: %u \n", 39: int_var, float_var, unsigned_var ); 40: 41: return 0; 42: } 43: 44: int get_menu_choice( void ) 45: { 46: int selection = 0; 47: 48: do 49: { 50: puts( "\n1 - Get a signed decimal integer" ); 51: puts( "2 - Get a decimal floating-point number" ); 52: puts( "3 - Get an unsigned decimal integer" ); 53: puts( "4 - Quit" ); 54: puts( "\nEnter a selection:" ); 55: 56: scanf( "%d", &selection ); 57: 58: }while ( selection < 1 || selection > 4 ); 59: 60: return selection; 61: } 1 - Get a signed decimal integer 2 - Get a decimal floating-point number 3 - Get an unsigned decimal integer 4 - Quit Enter a selection: 1 Enter a signed decimal integer (i.e. -123) -123 1 - Get a signed decimal integer 2 - Get a decimal floating-point number 3 - Get an unsigned decimal integer 4 - Quit Enter a selection: 3 Enter an unsigned decimal integer (i.e. 123) 321 1 - Get a signed decimal integer 2 - Get a decimal floating-point number 3 - Get an unsigned decimal integer 4 - Quit Enter a selection: 2 Enter a decimal floating point number (i.e. 1.23) 1231.123 1 - Get a signed decimal integer 2 - Get a decimal floating-point number 3 - Get an unsigned decimal integer 4 - Quit Enter a selection: 4 Your values are: int: -123 float: 1231.123047 unsigned: 321 BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTINGBy Ergin TARI

  15. Whole Lines of Input and Output • We can read and write whole lines as character strings. This approach allows us to read in a line of input, and then use various string handling functions to analyse it at our leisure. • gets • gets reads a whole line of input into a string until a newline or EOF is encountered. It is critical to ensure that the string is large enough to hold any expected input lines. • When all input is finished, NULL as defined in stdio.h is returned. BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTINGBy Ergin TARI

  16. Whole Lines of Input and Output puts puts writes a string to the output, and follows it with a newline character. Example: Program which uses gets and puts to double space typed input. #include <stdio.h> main() { char line[256]; /* Define string sufficiently large to store a line of input */ while(gets(line) != NULL) /* Read line */ { puts(line); /* Print line */ printf("\n"); /* Print blank line */ } } Note that putchar, printf and puts can be freely used together. So can getchar, scanf and gets. BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTINGBy Ergin TARI

  17. 1. Use a printf statement to print out the value of the integer variable sum 2. Use a printf statement to print out the text string "Welcome", followed by a newline. 3. Use a printf statement to print out the character variable letter 4. Use a printf statement to print out the float variable discount 5. Use a printf statement to print out the float variable dump using two decimal places 6. Use a scanf statement to read a decimal value from the keyboard, into the integer variable sum 7. Use a scanf statement to read a float variable into the variable discount_rate 8. Use a scanf statement to read a single character from the keyboard into the variable operator. Skip leading blanks, tabs and newline characters. BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTINGBy Ergin TARI

  18. 1. Use a printf statement to print out the value of the integer variable sum printf("%d", sum); 2. Use a printf statement to print out the text string "Welcome", followed by a newline. printf("Welcome\n"); 3. Use a printf statement to print out the character variable letter printf("%c", letter); 4. Use a printf statement to print out the float variable discount printf("%f", discount); 5. Use a printf statement to print out the float variable dump using two decimal places printf("%.2f", dump); 6. Use a scanf statement to read a decimal value from the keyboard, into the integer variable sum scanf("%d", &sum); 7. Use a scanf statement to read a float variable into the variable discount_rate scanf("%f", &discount_rate); 8. Use a scanf statement to read a single character from the keyboard into the variable operator. Skip leading blanks, tabs and newline characters. scanf(" %c", &operator); BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTINGBy Ergin TARI

  19. Q Why should I use puts() if printf() does everything puts() does and more? • A Because printf() does more, it has additional overhead. When you're trying to write a small, efficient program, or when your programs get big and resources are valuable, you will want to take advantage of the smaller overhead of puts(). In general, you should use the simplest available resource. • Q Why do I need to include STDIO.H when I use printf(), puts(), or scanf()? • A STDIO.H contains the prototypes for the standard input/output functions. printf(), puts(), and scanf() are three of these standard functions. Try running a program without the STDIO.H header and see the errors and warnings you get. • Q What happens if I leave the address-of operator (&) off a scanf() variable? • A This is an easy mistake to make. Unpredictable results can occur if you forget the address-of operator. When you read about pointers on Days 9 and 13, you will understand this better. For now, know that if you omit the address-of operator, scanf() doesn't place the entered information in your variable, but in some other place in memory. This could do anything from apparently having no effect to locking up your computer so that you must reboot. BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTINGBy Ergin TARI

  20. HOMEWORK OR EXERCISE 1.      Write a program that calculates the sum and the average of float values entered by user. The number of values will be determined by counting the numbers until the user enters the value of zero. 2.      Write a program that reads four integers and compares them by using different logical and relational operators. At least five different functions should be written to perform the comparisons, of which structures are left you (means you can chose any comparison, e.g. less than and(&&)/or(||) greater than, etc.). The meaningful explanations of results (e.g. x is greater than y and z equals to y) should be printed on the screen after having the results from the functions. (continued) BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTINGBy Ergin TARI

  21. WWrite a program that prints the multiplication table as following by omitting lines for 3s and7s. Write the program second time without using any for statements. 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100 BIL 102 INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTINGBy Ergin TARI

More Related