From 89dd70778b262cf9a9d4a90ddc8b925457671321 Mon Sep 17 00:00:00 2001 From: Ahmad Rokay Date: Fri, 5 Nov 2021 15:56:20 -0400 Subject: [PATCH] Audit input-functions.md * Add alt text to images * Encase key words in backticks * Add missing '-' * Remove extra indentation from code blocks * Add missing comma * Remove empty bullet point * Add title to frontmatter * Improve image centering * Add descriptive text to link * Add div.mdImg class for image centering Alter CSS for markdown images * Combine .mdImg with div.mdImg * Use div instead of span in functions.md --- docs/D-Modularity/functions.md | 12 +-- docs/D-Modularity/input-functions.md | 120 +++++++++++++++------------ src/css/custom.css | 5 +- 3 files changed, 75 insertions(+), 62 deletions(-) diff --git a/docs/D-Modularity/functions.md b/docs/D-Modularity/functions.md index 754be2cf..d432af46 100644 --- a/docs/D-Modularity/functions.md +++ b/docs/D-Modularity/functions.md @@ -45,11 +45,11 @@ The module named `hello.c` starts executing at statement `int main(void)`, outpu We can sub-divide a programming project in different ways. We select our modules so that each one focuses on a narrower aspect of the project. Our objective is to define a set of modules that simplifies the complexity of the original problem. - +
![modules](https://ict.senecacollege.ca//~ipc144/pages/images/modules.png) - +
Some general guidelines for defining a module include: @@ -95,11 +95,11 @@ Consider a module that receives a flag from another module and performs a calcul The C language is a procedural programming language. It supports modular design through function syntax. Functions transfer control between one another. When a function transfers control to another function, we say that it ***calls*** the other function. Once the other function completes its task and transfers control to the caller function, we say that that other function ***returns*** control to its ***caller***. - +
![function](https://ict.senecacollege.ca//~ipc144/pages/images/function.png) - +
In the example from the introductory chapter on [compilers](../A-Introduction/compilers.md) listed above: 1. the `main()` function calls the `printf()` function @@ -231,11 +231,11 @@ identifier(argument, ..., argument) `identifer` specifies the function being called, while `argument` specifies a value being passed to the function being called. - +
![calling-functions](https://ict.senecacollege.ca//~ipc144/pages/images/calling.png) - +
An argument may be a constant, a variable, or an expression (with certain exceptions). The number of arguments in a function call should match the number of parameters in the function header. ### Pass By Value diff --git a/docs/D-Modularity/input-functions.md b/docs/D-Modularity/input-functions.md index c360c3dd..8bfc2800 100644 --- a/docs/D-Modularity/input-functions.md +++ b/docs/D-Modularity/input-functions.md @@ -1,5 +1,6 @@ --- sidebar_position: 4 +title: Input Functions --- # Input Functions @@ -11,16 +12,20 @@ After reading this section, you will be able to: ## Introduction -Some programming languages leave input and output support to the libraries developed for the languages. For instance, the core C language does not include input and output specifications. These facilities are available in a set of functions, which are defined in the stdio module. This module ships with the C compiler. Its name stands for standard input and output. Typically, standard input refers to the system keyboard and standard output refers to the system display. The system header file that contains the prototypes for the functions in this module is ``. +Some programming languages leave input and output support to the libraries developed for the languages. For instance, the core C language does not include input and output specifications. These facilities are available in a set of functions, which are defined in the `stdio` module. This module ships with the C compiler. Its name stands for standard input and output. Typically, standard input refers to the system keyboard and standard output refers to the system display. The system header file that contains the prototypes for the functions in this module is ``. -This chapter describes some of the input facilities supported by the stdio module, introduces buffered input, describes two library functions that accept formatted and unformatted buffered input and demonstrates how to validate user input. +This chapter describes some of the input facilities supported by the `stdio` module, introduces buffered input, describes two library functions that accept formatted and unformatted buffered input and demonstrates how to validate user input. ## Buffered Input A buffer is a small region of memory that holds data temporarily and provides intermediate storage between a device and a program. The system stores each keystroke in the input buffer, without passing it to the program. The user can edit their data before submitting it to the program. only by pressing the \n key, the user signals the program to start extracting data from the buffer. The program then only retrieves the data that it needs and leaves the rest in the buffer for future retrievals. The figure below illustrates the buffered input process. -![](https://ict.senecacollege.ca//~ipc144/pages/images/buffer.png) +
+ +![Input buffer diagram](https://ict.senecacollege.ca//~ipc144/pages/images/buffer.png) + +
Two functions accept buffered input from the keyboard (the standard input device): @@ -29,11 +34,15 @@ Two functions accept buffered input from the keyboard (the standard input device ## Unformatted Input -The function getchar() retrieves the next unread character from the input buffer. +The function `getchar()` retrieves the next unread character from the input buffer. + +
+ +![getchar() diagram](https://ict.senecacollege.ca//~ipc144/pages/images/getchar.png) -![](https://ict.senecacollege.ca//~ipc144/pages/images/getchar.png) +
-The prototype for getchar() is +The prototype for `getchar()` is ```c int getchar(void); @@ -43,7 +52,7 @@ int getchar(void); * the character code for the retrieved character * EOF -The character code is the code from the collating sequence of the host computer. You can find the ASCII collating sequence [here](../Resources-Appendices/ascii-collating-sequence.md). If the next character in the buffer waiting to be read is `'j'` and the collating sequence is ASCII, then the value returned by `getchar()` is 106. +The character code is the code from the [collating sequence](../Resources-Appendices/ascii-collating-sequence.md) of the host computer. If the next character in the buffer waiting to be read is `'j'` and the collating sequence is ASCII, then the value returned by `getchar()` is 106. `EOF` is the symbolic name for end of data. It is assigned the value -1 in the `` system header file. On Windows systems, the user enters the end of data character by pressing `Ctrl-Z`; on UNIX systems, by pressing `Ctrl-D`. @@ -56,8 +65,8 @@ To synchronize user input with program execution the buffer should be empty. Th // void clear(void) { - while (getchar() != '\n') - ; // empty statement intentional + while (getchar() != '\n') + ; // empty statement intentional } ``` @@ -72,9 +81,9 @@ To pause execution at a selected point in a program, consider the following func // void pause_(void) { - printf("Press enter to continue ..."); - while (getchar() != '\n') - ; // empty statement intentional + printf("Press enter to continue ..."); + while (getchar() != '\n') + ; // empty statement intentional } ``` @@ -84,7 +93,11 @@ This function will not return control to the caller until the user has pressed ` The `scanf()` function retrieves the next set of unread characters from the input buffer and translates them according to the conversion(s) specified in the format string. `scanf()` extracts only as many characters as required to satisfy the specified conversion(s). -![](https://ict.senecacollege.ca//~ipc144/pages/images/scanf.png) +
+ +![scanf() diagram](https://ict.senecacollege.ca//~ipc144/pages/images/scanf.png) + +
The prototype for scanf() is: @@ -128,13 +141,13 @@ The following program converts two input fields into data values of int type and int main(void) { - int items; - float price; + int items; + float price; + + printf("Enter items, price : "); + scanf("%d%f", &items, &price); - printf("Enter items, price : "); - scanf("%d%f", &items, &price); - - return 0; + return 0; } ``` @@ -158,7 +171,7 @@ We may insert control characters between the `%` and the `conversion character`. The three control characters are: -* **\*** suppresses storage of the converted data (discards it without storing it) +* **\*** - suppresses storage of the converted data (discards it without storing it) * ***width*** - specifies the maximum number of characters to be interpreted * ***size*** - specifies the size of the storage type @@ -184,7 +197,7 @@ A conversion specifier that includes an **\*** does not have a corresponding add ### Problems with %c (Optional) -Because `scanf()` only extracts the characters that it needs from the input buffer, problems arise with `%c` conversions. If you encounter such difficulty see the section with this title in the chapter entitled [More Input and Output](../F-Refinements/more-input-and-output.md). +Because `scanf()` only extracts the characters that it needs from the input buffer, problems arise with `%c` conversions. If you encounter such difficulty, see the section with this title in the chapter entitled [More Input and Output](../F-Refinements/more-input-and-output.md). ### Plain Characters (Optional) @@ -199,7 +212,6 @@ To input `%` as a plain character (and distinguish it from the symbol identifyin * 0 indicates that `scanf()` did not fill any address * 1 indicates that `scanf()` filled the first address successfully * 2 indicates that `scanf()` filled the first and second addresses successfully -* ... * `EOF` indicates that `scanf()` did not fill any address **AND** encountered an end of data character The return code from `scanf()` does not reflect success of **%\*** conversions or any successful reading of plain characters in the format string. @@ -232,12 +244,12 @@ void clear(void); int main(void) { - int input; + int input; - input = getInt(MIN, MAX); - printf("\nProgram accepted %d\n", input); + input = getInt(MIN, MAX); + printf("\nProgram accepted %d\n", input); - return 0; + return 0; } // getInt accepts an int between min and max @@ -245,40 +257,40 @@ int main(void) // int getInt(int min, int max) { - int value, keeptrying = 1, rc; - char after; - - do { - printf("Enter an integer in range [%d,%d] : ", min, max); - rc = scanf("%d%c", &value, &after); - - if (rc == 0) - { - printf("**Bad char(s)!**\n"); - clear(); - } - else if (after != '\n') - { - printf("**Trail char(s)!**\n"); - clear(); - } - else if (value < min || value > max) - { - printf("**Out of range!**\n"); - } - else - keeptrying = 0; - } while (keeptrying == 1); - - return value; + int value, keeptrying = 1, rc; + char after; + + do { + printf("Enter an integer in range [%d,%d] : ", min, max); + rc = scanf("%d%c", &value, &after); + + if (rc == 0) + { + printf("**Bad char(s)!**\n"); + clear(); + } + else if (after != '\n') + { + printf("**Trail char(s)!**\n"); + clear(); + } + else if (value < min || value > max) + { + printf("**Out of range!**\n"); + } + else + keeptrying = 0; + } while (keeptrying == 1); + + return value; } // clear empties the input buffer // void clear(void) { - while (getchar() != '\n') - ; // empty statement intentional + while (getchar() != '\n') + ; // empty statement intentional } ``` The above program produces the following output: diff --git a/src/css/custom.css b/src/css/custom.css index 438ec112..370cac33 100644 --- a/src/css/custom.css +++ b/src/css/custom.css @@ -45,6 +45,7 @@ html[data-theme='dark'] .docusaurus-highlight-code-line { background-color: rgba(0, 0, 0, 0.3); } -.mdImg > p > img{ +div.mdImg { background-color: #ffffff; -} \ No newline at end of file + text-align: center; +}