feat(src/kr/ch1/s1.7/e1-15):Finish exercise
authorSteven Baltakatei Sandoval <baltakatei@gmail.com>
Wed, 14 Sep 2022 00:50:00 +0000 (00:50 +0000)
committerSteven Baltakatei Sandoval <baltakatei@gmail.com>
Wed, 14 Sep 2022 00:50:00 +0000 (00:50 +0000)
- Note: Fahrenheit to Celsius conversion program utilizing functions.

src/kr_exercises/ch1/s1.2/s1.2..f_to_c.c [new file with mode: 0644]
src/kr_exercises/ch1/s1.7/e1-15..c_to_f [new file with mode: 0755]
src/kr_exercises/ch1/s1.7/e1-15..c_to_f.c [new file with mode: 0644]
src/notes.tm

diff --git a/src/kr_exercises/ch1/s1.2/s1.2..f_to_c.c b/src/kr_exercises/ch1/s1.2/s1.2..f_to_c.c
new file mode 100644 (file)
index 0000000..e2ff1cf
--- /dev/null
@@ -0,0 +1,19 @@
+#include <stdio.h>
+
+/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */
+
+int main() {
+  int fahr, celsius;
+  int lower, upper, step;
+
+  lower = 0;   /* lower limit of temperature table */
+  upper = 300; /* upper limit */
+  step = 20;   /* step size */
+
+  fahr = lower;
+  while (fahr <= upper) {
+    celsius = 5 * (fahr-32) / 9;
+    printf("%d\t%d\n", fahr, celsius);
+    fahr = fahr + step;
+  }
+}
diff --git a/src/kr_exercises/ch1/s1.7/e1-15..c_to_f b/src/kr_exercises/ch1/s1.7/e1-15..c_to_f
new file mode 100755 (executable)
index 0000000..5806ed1
Binary files /dev/null and b/src/kr_exercises/ch1/s1.7/e1-15..c_to_f differ
diff --git a/src/kr_exercises/ch1/s1.7/e1-15..c_to_f.c b/src/kr_exercises/ch1/s1.7/e1-15..c_to_f.c
new file mode 100644 (file)
index 0000000..1210d38
--- /dev/null
@@ -0,0 +1,33 @@
+#include <stdio.h>
+
+int f_to_c(int degf);
+
+/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */
+
+int main() {
+  int fahr, celsius;
+  int lower, upper, step;
+
+  lower = 0;   /* lower limit of temperature table */
+  upper = 300; /* upper limit */
+  step = 20;   /* step size */
+
+  fahr = lower;
+  while (fahr <= upper) {
+    celsius = f_to_c(fahr);
+    /* celsius = 5 * (fahr-32) / 9; */
+    printf("%d\t%d\n", fahr, celsius);
+    fahr = fahr + step;
+  };
+};
+
+/* f_to_c: calculate celsius from fahrenheit */
+int f_to_c(int degf) {
+  int degc;
+  degc = 5 * (degf - 32) / 9;
+  return degc;
+};
+
+/* Author: Steven Baltakatei Sandoval
+   License: GPLv3+
+*/
index ebe2879f1d8309d422d36b54ff656c4d006e36f1..37db3497ec28b2373c55f727731c2854cc2142f5 100644 (file)
       a = ( b = ( c = 0 ) );
     </cpp-code>
 
-    <item*|call>The act of running (or \Pinvoking\Q) a <strong|function>. See
-    <em|function call>.
+    <label|term_call><item*|call>The act of running (or \Pinvoking\Q) a
+    <strong|function>. See <em|function call>.
 
-    <item*|declaration<label|term declaration>>A construct that establishes
+    <label|term declaration><item*|declaration>A construct that establishes
     an association between a particular variable, function, or type and its
     attributes<\footnote>
       See <hlinkv|https://en.cppreference.com/w/c/language/declarations>.
     with a variable (e.g. <cpp|int i = 0;>). Compare with
     <with|font-series|bold|definition>.
 
-    <item*|definition<label|term definition>>A construct that establishes the
+    <label|term definition><item*|definition>A construct that establishes the
     same associations as a declaration but also causes storage to be
     allocated for the variable<\footnote>
       See <hlinkv|https://docs.microsoft.com/en-us/cpp/c-language/c-declarations-and-definitions?view=msvc-170>.
     </footnote>.
 
-    <item*|delinter><label|term_delinter> A source code analysis program
-    designed to detect common syntactic errors.
+    <label|term_linter><item*|linter> A source code analysis program designed
+    to detect common syntactic errors.
 
-    <item*|enumeration<label|term enumeration>><hlink|Enumeration|https://en.wikipedia.org/wiki/Enumeration>
+    <label|term enumeration><item*|enumeration><hlink|Enumeration|https://en.wikipedia.org/wiki/Enumeration>
     (or <code*|enum>) is a user defined data type in <name|C>. It is mainly
     used to assign names to integral constants. For example, the declaration
     <code*|enum year{Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov,
@@ -85,7 +85,7 @@
     i\<less\>=Dec; i++)> to cycle <code*|i> through the integers <code*|0, 1,
     2, 3, <text-dots>, 10, 11>.
 
-    <item*|escape sequence<label|term escape_sequence>>A set of characters
+    <label|term escape_sequence><item*|escape sequence>A set of characters
     used to represent hard-to-type or invisible characters. Some commonly
     used escape sequences in <name|C> include:
 
       }
     </indent>>
 
-    <item*|function call>An expression containing the <strong|function> name
-    followed by the function call operator \P<cpp|()>\Q.
+    <label|term_function_call><item*|function call>An expression containing
+    the <strong|function> name followed by the function call operator
+    \P<cpp|()>\Q.
+
+    <label|term_function_prototype><item*|function prototype>A declaration of
+    a <strong|function> consisting only of the function name (a.k.a.
+    \Pidentifier\Q) and parenthesized <strong|parameter> types so the
+    compiler knows how to perform <strong|type>-based conversions of
+    <strong|arguments> (e.g. truncating a <cpp|float> into an <cpp|int>) in
+    <strong|calls> of the function before the compiler knows the function
+    <strong|definition><\footnote>
+      See <hlinkv|https://en.cppreference.com/w/c/language/function_declaration>.
+    </footnote>. The function prototype parameter names do not have to agree
+    with the function definition parameter names<\footnote>
+      K&R 2nd Ed., Sec 1.7 \PFunctions\Q.
+    </footnote>.
 
     <label|term garbage_collection><item*|garbage collection>The process of
     freeing memory allocated by a program but which is no longer referenced.
     followed by a newline character. (See K&R 2nd Ed. Section 1.5 \PCharacter
     Input and Output\Q)
 
-    <item*|type>A way of differentiating data stored for use in a program.
-    Some types include <cpp|int> (integers), <cpp|char> (single-byte
-    characters), <cpp|short> (a short integer), <cpp|long> (a long integer),
-    <cpp|double> (a double-precision floating point number).
+    <label|term_type><item*|type>A way of differentiating data stored for use
+    in a program. Some types include <cpp|int> (integers), <cpp|char>
+    (single-byte characters), <cpp|short> (a short integer), <cpp|long> (a
+    long integer), <cpp|double> (a double-precision floating point number).
   </description-compact>
 
   \;
     <associate|auto-6|<tuple|2.1|?>>
     <associate|auto-7|<tuple|2.2|?>>
     <associate|footnote-1.1.1|<tuple|1.1.1|?>>
+    <associate|footnote-1.1.10|<tuple|1.1.10|?>>
     <associate|footnote-1.1.2|<tuple|1.1.2|?>>
     <associate|footnote-1.1.3|<tuple|1.1.3|?>>
     <associate|footnote-1.1.4|<tuple|1.1.4|?>>
     <associate|footnote-1.1.6|<tuple|1.1.6|?>>
     <associate|footnote-1.1.7|<tuple|1.1.7|?>>
     <associate|footnote-1.1.8|<tuple|1.1.8|?>>
+    <associate|footnote-1.1.9|<tuple|1.1.9|?>>
     <associate|footnote-1.2.1|<tuple|1.2.1|?>>
     <associate|footnote-1.2.2|<tuple|1.2.2|?>>
     <associate|footnote-1.2.3|<tuple|1.2.3|?>>
     <associate|footnote-1.2.4|<tuple|1.2.4|?>>
     <associate|footnote-1.2.5|<tuple|1.2.5|?>>
     <associate|footnr-1.1.1|<tuple|1.1.1|?>>
+    <associate|footnr-1.1.10|<tuple|1.1.10|?>>
     <associate|footnr-1.1.2|<tuple|1.1.2|?>>
     <associate|footnr-1.1.3|<tuple|1.1.3|?>>
     <associate|footnr-1.1.4|<tuple|1.1.4|?>>
     <associate|footnr-1.1.6|<tuple|1.1.6|?>>
     <associate|footnr-1.1.7|<tuple|1.1.7|?>>
     <associate|footnr-1.1.8|<tuple|1.1.8|?>>
+    <associate|footnr-1.1.9|<tuple|1.1.9|?>>
     <associate|footnr-1.2.1|<tuple|1.2.1|?>>
     <associate|footnr-1.2.2|<tuple|1.2.2|?>>
     <associate|footnr-1.2.3|<tuple|1.2.3|?>>
     <associate|ref includehelp-oct-hex|<tuple|1.1.3|?>>
     <associate|term declaration|<tuple|call|?>>
     <associate|term definition|<tuple|1.1.1|?>>
-    <associate|term enumeration|<tuple|delinter|?>>
-    <associate|term escape_sequence|<tuple|enumeration<label|term
-    enumeration>|?>>
-    <associate|term garbage_collection|<tuple|function call|?>>
+    <associate|term enumeration|<tuple|linter|?>>
+    <associate|term escape_sequence|<tuple|enumeration|?>>
+    <associate|term garbage_collection|<tuple|1.1.9|?>>
     <associate|term heap|<tuple|garbage collection|?>>
     <associate|term machine-independent|<tuple|<label|term heap>heap|?>>
     <associate|term operand|<tuple|<label|term
     machine-independent>machine-independent|?>>
     <associate|term operator|<tuple|<label|term operand>operand|?>>
     <associate|term stack|<tuple|parameter|?>>
-    <associate|term struct|<tuple|1.1.8|?>>
+    <associate|term struct|<tuple|1.1.10|?>>
     <associate|term structure_assignment|<tuple|<label|term struct>struct|?>>
     <associate|term text_stream|<tuple|structure assignment|?>>
     <associate|term_argument|<tuple|1.1|?>>
-    <associate|term_delinter|<tuple|delinter|?>>
+    <associate|term_call|<tuple|assignment|?>>
+    <associate|term_delinter|<tuple|1.1.2|?>>
     <associate|term_expression|<tuple|<with|mode|<quote|prog>|prog-language|<quote|cpp>|font-family|<quote|rm>|\\x><with|font-shape|<quote|italic>|hh>|?>>
     <associate|term_function|<tuple|1.1.7|?>>
+    <associate|term_function_call|<tuple|function|?>>
+    <associate|term_function_prototype|<tuple|function call|?>>
+    <associate|term_linter|<tuple|1.1.2|?>>
     <associate|term_parameter|<tuple|<label|term operator>operator|?>>
     <associate|term_statement|<tuple|<label|term stack>stack|?>>
-    <associate|term_variable|<tuple|<label|term text_stream>text stream|?>>
+    <associate|term_type|<tuple|<label|term text_stream>text stream|?>>
   </collection>
 </references>