{"id":427,"date":"2020-08-21T17:56:18","date_gmt":"2020-08-21T22:56:18","guid":{"rendered":"https:\/\/www.brezeale.com\/?p=427"},"modified":"2021-01-23T20:18:20","modified_gmt":"2021-01-24T02:18:20","slug":"c-programming-a-self-learning-guide","status":"publish","type":"post","link":"https:\/\/www.brezeale.com\/?p=427","title":{"rendered":"C Programming: A Self-Learning Guide"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The purpose of this page is to guide someone wishing to learn to program using the C programming language, specifically the ANSI Standard of 1989. Each section consists of<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>a link to a video that teaches an element of the language<\/li><li>sample problems for you to try. <span class=\"has-inline-color has-vivid-red-color\">My solutions to the problems can be found at the bottom of the page. <\/span>It\u2019s OK if your solution isn\u2019t the same as mine.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Some sections also include a link to a video demonstrating an application of the topic or a link to another page in which I go into more detail about my design process.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note that the practice problems build upon your knowledge. Some of the practice problems assume you worked the earlier problems.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">start here:&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.youtube.com\/watch?v=XiS2nBFNZ_I\" target=\"_blank\">Introduction<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now you need to install the necessary software:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Microsoft Windows: <a href=\"https:\/\/www.youtube.com\/watch?v=_B2pQFrnhTg\">Compiling with Visual Studio<\/a><\/li><li>Mac OS or Linux: you may already have a C compiler, so watch <a href=\"https:\/\/www.youtube.com\/watch?v=F6Ou2hruvKQ\">Compiling with GCC<\/a><\/li><li>(optional) I use the vim editor on Linux in my videos. If you want a better understanding of what I am doing, then watch&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.youtube.com\/watch?v=VbvSgDWLF9A\" target=\"_blank\">Brief overview of vim and the command line<\/a><\/li><\/ul>\n\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Statements<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">start here: <a href=\"https:\/\/www.youtube.com\/watch?v=3YU2ficXvpM\" data-type=\"URL\" data-id=\"https:\/\/www.youtube.com\/watch?v=3YU2ficXvpM\">Statements<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now try the following practice problems:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #1:<\/strong>&nbsp;<code>printf()<\/code> has a very large number of format specifiers. &nbsp;There are a number of options that can be used with the format specifiers for altering the way that information is displayed. &nbsp;Run the following program with different values for the format specifiers (sorry about using loops before officially discussing them, but it makes this much simpler).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For the first <code>printf()<\/code>, some values to try are %2d and %4d.  For the second <code>printf()<\/code>, some values to try are %2lf, %4.2lf, and %8.5lf (those are the letters &#8216;L&#8217; and &#8216;F&#8217; in lowercase). <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n    \nint main(void){\n    int i;\n    double x;\n    \n    for(i = 1; i &lt; 10000; i *= 10)\n        printf(&quot;%d\\n&quot;, i);\n    \n    for(x = 1.234; x &lt; 10000; x *= 10)\n        printf(&quot;%lf\\n&quot;, x);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #2: <\/strong>Write a program that stores the values a = 4, b = 13, and c = 6 as variables of type <code>int<\/code> and produces d = (a + b + c)\/5.  Then print d to 2 decimal places.<\/p>\n\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Common Mistakes<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Mistakes in syntax or logic are a part of programming.  I have put together a video demonstrating many of the mistakes new (and not so new) C programmers make, with solutions.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There is a catch-22 to this video.  If you are new to C programming and watch it now, you won&#8217;t understand much of it.  If you wait until you have reached the end of this self-learning guide to watch the video, then you have likely already made some of these mistakes.  I would encourage you to watch the video early on to at least plant some ideas in your head and then revisit the video as you gain experience.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">video: <a href=\"https:\/\/www.youtube.com\/watch?v=WS8aHMK-a9c\" data-type=\"URL\" data-id=\"https:\/\/www.youtube.com\/watch?v=WS8aHMK-a9c\">Common C Mistakes<\/a><\/p>\n\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Conditionals<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">start here: <a href=\"https:\/\/www.youtube.com\/watch?v=IF0KEiqn73s\" data-type=\"URL\" data-id=\"https:\/\/www.youtube.com\/watch?v=IF0KEiqn73s\">Conditionals<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">then here: <a href=\"https:\/\/www.youtube.com\/watch?v=8LAZ9KcbtvE\" data-type=\"URL\" data-id=\"https:\/\/www.youtube.com\/watch?v=8LAZ9KcbtvE\">Logical Operators<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">then here: <a href=\"https:\/\/www.youtube.com\/watch?v=-dp-JXUNucw\" data-type=\"URL\" data-id=\"https:\/\/www.youtube.com\/watch?v=-dp-JXUNucw\">application of conditionals<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #1:<\/strong> Compare the output from the following programs. &nbsp;Why is it different?<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n\/* version 1*\/\n#include &lt;stdio.h&gt;\n    \nint main(void) {\n    int x = 8;\n    \n    if(x &lt; 6)\n        printf(&quot;%d &lt; 6\\n&quot;, x);\n    else if(x &lt; 8)\n        printf(&quot;%d &lt; 8\\n&quot;, x);\n    else if(x &lt; 10)\n        printf(&quot;%d &lt; 10\\n&quot;, x);\n    else if(x &lt; 12)\n        printf(&quot;%d &lt; 12\\n&quot;, x);\n    else if(x &lt; 14)\n        printf(&quot;%d &lt; 14\\n&quot;, x);\n}\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n\/* version 2 *\/\n#include &lt;stdio.h&gt;\n    \nint main(void) {\n    int x = 8;\n    \n    if(x &lt; 6)\n        printf(&quot;%d &lt; 6\\n&quot;, x);\n    if(x &lt; 8)\n        printf(&quot;%d &lt; 8\\n&quot;, x);\n    if(x &lt; 10)\n        printf(&quot;%d &lt; 10\\n&quot;, x);\n    if(x &lt; 12)\n        printf(&quot;%d &lt; 12\\n&quot;, x);\n    if(x &lt; 14)\n        printf(&quot;%d &lt; 14\\n&quot;, x);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #2: <\/strong>The following program doesn&#8217;t print anything. &nbsp;Why? &nbsp;Using only a set of curly braces, change the program such that the second <code>printf()<\/code> statement will print if age is less than or equal to 50.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n     \nint main(void) {\n    int age = 40;\n     \n    if(age &gt; 50)\n        if(age &lt; 75)\n            printf(&quot;first printf\\n&quot;);\n        else\n            printf(&quot;second printf\\n&quot;);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #3: <\/strong>Write a program that prompts a user for an integer, checks for divisibility by 2 and 3, and then prints the statements given by the truth table below.  To prompt the user, you will need the following code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n     \nint main(void) {\n    int x;\n    \n    printf(&quot;enter an integer: &quot;);\n    scanf(&quot;%d&quot;, &amp;x);\n    \n    \/* put your logic here *\/\n}\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndivisible  divisible\n   by 2       by 3    prints\n---------------------------------------------------\n   True       True    both 2 and 3 are divisors\n   True       False   only 2 is a divisor\n   False      True    only 3 is a divisor\n   False      False   neither 2 nor 3 is a divisor\n<\/pre><\/div>\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Loops<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">start here: <a href=\"https:\/\/www.youtube.com\/watch?v=4GwGJ5YdONM\" data-type=\"URL\" data-id=\"https:\/\/www.youtube.com\/watch?v=4GwGJ5YdONM\">Loops<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">then here: <a href=\"https:\/\/www.youtube.com\/watch?v=ZHsrR--LLso\">application of loops<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #1:<\/strong> Rewrite the following program, replacing the <code>for<\/code> loop with a <code>while<\/code> loop.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint main(void) {\n    int i;\n\n    for(i = 8; i &gt; 5; i--)\n        printf(&quot;%d\\n&quot;, i);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #2: <\/strong>You have a program that prompts a user for two integers, which are stored in the variables <code>start<\/code> and <code>stop<\/code>. &nbsp;Write the code that will produce an integer <code>x<\/code> in the range of <code>start<\/code> to <code>stop<\/code>, inclusive.  If <code>x<\/code> is even then your program will print &#8220;x is Even&#8221;.  Otherwise, print the value of <code>x<\/code>.  You can assume that the starting integer is less than or equal to the stopping integer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #3: <\/strong>Write a program that prints the integers from 1 to 100 as a 10&#215;10 table with the values right-aligned within their column. &nbsp;Each row will begin with the character &#8216;X&#8217;.  The results will be<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nX  1   2   3   4   5   6   7   8   9  10 \nX 11  12  13  14  15  16  17  18  19  20 \nX 21  22  23  24  25  26  27  28  29  30 \nX 31  32  33  34  35  36  37  38  39  40 \nX 41  42  43  44  45  46  47  48  49  50 \nX 51  52  53  54  55  56  57  58  59  60 \nX 61  62  63  64  65  66  67  68  69  70 \nX 71  72  73  74  75  76  77  78  79  80 \nX 81  82  83  84  85  86  87  88  89  90 \nX 91  92  93  94  95  96  97  98  99 100\n<\/pre><\/div>\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Functions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">start here: <a href=\"https:\/\/www.youtube.com\/watch?v=f6URL3cmuvM\">Functions, part 1<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">then here: <a href=\"https:\/\/www.youtube.com\/watch?v=v2r9ywkCpRc\" data-type=\"URL\" data-id=\"https:\/\/www.youtube.com\/watch?v=v2r9ywkCpRc\">Functions, part 2<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">then here: <a href=\"https:\/\/www.youtube.com\/watch?v=mNI1M8amMms\" data-type=\"URL\" data-id=\"https:\/\/www.youtube.com\/watch?v=mNI1M8amMms\">application of functions, part 1<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">then here: <a href=\"https:\/\/www.youtube.com\/watch?v=Sl8BhDMSeTw\">application of functions, part 2<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #1:<\/strong> Write a function declaration for a function with the name<code>calcAge<\/code> that will receive a variable of type <code>int<\/code> and has a return type of <code>float<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #2:<\/strong> The following program makes a function call to a function called <code>multNums<\/code>. &nbsp;<code>multNums<\/code> takes two arguments, multiplies them together, and then returns the result.  Write the function declaration and function definition for <code>multNums<\/code>. &nbsp;The return type and variable types that you write for <code>multNums<\/code> should match those of the variables involved in the function call of the sample program.  Your function definition will include whatever code is necessary to perform the calculation.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\n\/* function declaration goes here *\/\n\nint main( void ) {\n   int hours = 3;\n   float rate = 5;\n   double cost;\n\n   cost = multNums(hours, rate);\n   \n   printf(&quot;cost is %2.f\\n&quot;, cost);\n}\n\n\/* function definition goes here *\/\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #3:<\/strong> Write a function that when given a two positive integers <code>a<\/code> and <code>b<\/code> determines how many of the integers in the range of <code>a<\/code> to <code>b<\/code> inclusive are divisible by 3 and how many are divisible by 5.  The function will print these two counts as well as how many of the integers in the range are not divisible by 3 or 5.  If a number is divisible by 3 and 5, then update the counts for each of 3 and 5.  As the function is performing the counts, have it print the matching number.  For example, if the current number is 9 then the count for divisibility by 3 will be incremented and you will print that 9 is divisible by 3.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the function is called with the code below<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include&lt;stdio.h&gt;\n\nint main( void ) {\n    int a = 6, b = 20;\n\n    f(a, b);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">then the output will be<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n6 is divisible by 3\n7 is not divisible by 3 nor 5\n8 is not divisible by 3 nor 5\n9 is divisible by 3\n10 is divisible by 5\n11 is not divisible by 3 nor 5\n12 is divisible by 3\n13 is not divisible by 3 nor 5\n14 is not divisible by 3 nor 5\n15 is divisible by 3\n15 is divisible by 5\n16 is not divisible by 3 nor 5\n17 is not divisible by 3 nor 5\n18 is divisible by 3\n19 is not divisible by 3 nor 5\n20 is divisible by 5\n\ndivisible by 3 = 5\ndivisible by 5 = 3\ndivisible by neither = 8\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #4:<\/strong> Write a function&nbsp;that when given an integer x, returns the second smallest divisor of x (not counting 1). &nbsp;For example, if x = 12, then the function would return 3 since the divisors are 1, 2, 3, 4, 6, and 12 (remember, we are not counting 1). &nbsp;If x does not have a second smallest divisor, then the function should return zero.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #5:<\/strong> Write a program that produces the integers in the range of 2 to 100, but only prints those that are prime. &nbsp;If the number produced is not prime, print two periods instead. &nbsp;Everything printed should result in a 10&#215;10 table of either values printed or spaces. &nbsp; The results will be<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n  ..   2   3  ..   5  ..   7  ..  ..  ..\n  11  ..  13  ..  ..  ..  17  ..  19  ..\n  ..  ..  23  ..  ..  ..  ..  ..  29  ..\n  31  ..  ..  ..  ..  ..  37  ..  ..  ..\n  41  ..  43  ..  ..  ..  47  ..  ..  ..\n  ..  ..  53  ..  ..  ..  ..  ..  59  ..\n  61  ..  ..  ..  ..  ..  67  ..  ..  ..\n  71  ..  73  ..  ..  ..  ..  ..  79  ..\n  ..  ..  83  ..  ..  ..  ..  ..  89  ..\n  ..  ..  ..  ..  ..  ..  97  ..  ..  ..\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Practice #6: Reading code is an invaluable skill as a programmer.  You need to be able to read your own code as well as others.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What does this program print?  Try constructing a table and filling it in as you process the loop.  Your table might start like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n  i    alpha   beta    prints\n-----------------------------\n  12     ?       ?        ?\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint alpha(int a);\nint beta(int b);\n\nint main(void) {\n    int i;\n    \n    for(i = 12; i &lt; 26; i = i + 3)\n        if( alpha(i) &amp;&amp; beta(i) )\n            printf(&quot;%d\\n&quot;, i);\n}\n\nint alpha(int a) {\n    if(a%3 == 0)\n        return 1;\n    else\n        return 0;\n}\n\nint beta(int b) {\n    if(b%2 == 0)\n        return 1;\n    else\n        return 0;\n}\n<\/pre><\/div>\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Arrays<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">start here: <a href=\"https:\/\/www.youtube.com\/watch?v=x7NnhYH48Ks\" data-type=\"URL\" data-id=\"https:\/\/www.youtube.com\/watch?v=x7NnhYH48Ks\">One-Dimensional Arrays<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">then here: <a href=\"https:\/\/www.youtube.com\/watch?v=yKSbQzVb2UQ\">Multidimensional Arrays<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">then here: <a href=\"https:\/\/www.youtube.com\/watch?v=vDnG4Bh-bv4\">application of arrays, part 1<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">design discussion: <a href=\"https:\/\/www.brezeale.com\/?p=192\">Simulating a Perfect Shuffle<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #1:<\/strong> Write a program that stores the numbers 1, 2, 7, 18, 23, and 40 in an array.  Then move through the array, producing the sum of the even integers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #2:<\/strong> For the program below, write the code that will store the values in <code>array1<\/code> in <code>array2<\/code>, but in reverse order.  Also write <code>printArray()<\/code> to print the arrays.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n    \nvoid printArray(int &#x5B;], int);\n\nint main(void) {\n    int array1&#x5B;5] = {17, 23, 9, 4, 12};\n    int array2&#x5B;5];\n    int length = 5;\n    int i;\n\n      \/* code to store array1 in array2 in \n         reverse order goes here            *\/\n\n    printf(&quot;array1: &quot;);\n    printArray(array1, length);\n    printf(&quot;array2: &quot;);\n    printArray(array2, length);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #<\/strong>3<strong>:<\/strong> Modify the program below to swap the elements between the two arrays.  That is, after you are done then array1[0] = 99 and array2[0] = 1 and so on for each corresponding array element.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n    \nvoid printArray(int &#x5B;], int);\n\nint main(void) {\n    int array1&#x5B;5] = {1, 2, 3, 4, 5};\n    int array2&#x5B;5] = {99, 88, 77, 66, 55};\n    int temp;\n    int length = 5;\n    int i;\n\n      \/* code for swapping goes here *\/\n\n    printf(&quot;array1: &quot;);\n    printArray(array1, length);\n    printf(&quot;array2: &quot;);\n    printArray(array2, length);\n}\n\nvoid printArray(int data&#x5B;], int length) {\n    int i;\n\n    for(i = 0; i &lt; length; i++)\n        printf(&quot;%2d &quot;, data&#x5B;i]);\n\n    printf(&quot;\\n&quot;);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #<\/strong>4<strong>:<\/strong> Modify the looping structure of the program below so that it produces the output shown. &nbsp;Hint: this can be done by changing only two lines.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n    \nint main(void){\n    int data&#x5B;2]&#x5B;3] = { {15, 72,  9},\n                       { 4,  8, 11} };\n    int i, k;\n    \n    for(i = 0; i &lt; 2; i++) {\n        for(k = 0; k &lt; 3; k++) {\n            printf(&quot;%2d &quot;, data&#x5B;i]&#x5B;k]);\n        }\n        printf(&quot;\\n&quot;);\n    }\n}\n    \n\/*****************\n ***   Output   ***\n    15  4 \n    72  8 \n     9 11 \n*****************\/\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #5:<\/strong> Write a program that declares a 2D array with three columns.  The program passes the array to a function that replaces each element in the center column with the sum of the other two elements on the same row.  For example, if the first row of the array is<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n5, 8, 10\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">then after the call to your function this row would be<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n5, 15, 10\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Note that this can be done with a single loop that generates the subscript of the row.  Also create a second function that prints a 2D array with three columns.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #6:<\/strong> Finish the program below by writing the code to change any number in the array to 99 if, and only if, the values immediately before and after the current number are both even. The first and last elements of the array should not be changed. The array should be processed beginning with the element at index 1. Note that when processing a number, if the value before it has been changed to 99 that is what should be considered, not the value in the initial version of the array. Your program should still work if I changed the array values.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint main(void)\n{\n    int d&#x5B;] = {9, 8, 2, 15, 6, 32, 10, 4};\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #7:<\/strong> Write a function that when given a 2D array with three columns, prints the numbers in each row as a sum and then prints the actual sum of the row values.  For example, given<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint main(void) {\n    int d&#x5B;]&#x5B;3] = {{ 1,  2,  3},\n                  { 4,  5,  6},\n                  { 7,  8,  9},\n                  {-1, -2, -3}};\n    int rows;\n\n    rows = sizeof(d)\/sizeof(d&#x5B;0]);\n    printRowSums(d, rows);\n} \n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# output\n  1 +  2 +  3 =  6\n  4 +  5 +  6 = 15\n  7 +  8 +  9 = 24\n -1 + -2 + -3 = -6\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #8:<\/strong> The program below calls the function <code>findNum()<\/code> to search an array for <code>number<\/code>.  If it finds <code>number<\/code> in the array, it returns the index of the first occurrence (in case there are multiple occurrences) of <code>number<\/code>.  If <code>number<\/code> is not in the array, the function returns -1.  Write <code>findNum()<\/code>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint main(void) {\n     int d&#x5B;] = {-2, 14, 7, 61, 99, 7, 0, -10};\n     int number;\n     int index, size = sizeof(d)\/sizeof(d&#x5B;0]);\n     \n     number = 7; \/* number to search for *\/\n     index = findNum(d, size, number);\n     \n     if(index &gt;= 0)\n         printf(&quot;%d is at index %d\\n&quot;, number, index);\n     \n     number = 123; \/* number to search for *\/\n     index = findNum(d, size, number);\n     \n     if(index &gt;= 0)\n         printf(&quot;%d is at index %d\\n&quot;, number, index);\n} \n<\/pre><\/div>\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Variable Types<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">start here: <a href=\"https:\/\/www.youtube.com\/watch?v=aJ5V_UL_NHo\" data-type=\"URL\" data-id=\"https:\/\/www.youtube.com\/watch?v=aJ5V_UL_NHo\">Bits, Bytes, and Numbers Bases<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">then here: <a href=\"https:\/\/www.youtube.com\/watch?v=pf1Xq1ATuAE\">Integer Variable Types<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">then here: <a href=\"https:\/\/www.youtube.com\/watch?v=CsMP1aO5Lw4\">Floating-Point Variable Types<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #1:<\/strong> or the following program, which of the following is true? If you choose one of the first three answer, explain what the problem is and the solution to it.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The value produced by y = w + x will be incorrect.<\/li><li>The value produced by z = w &#8211; x will be incorrect.<\/li><li>The values produced by y = w + x and z = w &#8211; x will both be incorrect.<\/li><li>The values produced by y = w + x and z = w &#8211; x will both be correct.<\/li><\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/*\n  on this machine, a short int is 2 bytes, an int \n  is 4 bytes, and a long int is 8 bytes\n*\/\n    #include &amp;lt;stdio.h&gt;\n    \n    int  main(void) {\n        short w = 20000;\n        short x = 15000;\n        short y;\n        short z;\n    \n        y = w + x;\n        z = w - x;\n    \n        printf(&quot;w + x = %hd\\n&quot;, y);\n        printf(&quot;w - x = %hd\\n&quot;, z);\n    }\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #2: <\/strong>Run the following program.  Is the output what you expected?<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &amp;lt;stdio.h&gt;\n\nint main(void) {\n    short int x = 32765;\n    int i;\n\n    for(i = 0; i &amp;lt; 5; i++) {\n        printf(&quot;%d\\n&quot;, x);\n        x++;\n    }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #3:<\/strong> Why does the number of bytes reported from f() differs from in  main()?<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nvoid f(int data&#x5B;]) {\n    printf(&quot;here data has a size of %d bytes\\n&quot;, sizeof(data) );\n}\n\nint main(void) {\n    int data&#x5B;] = {1, 2, 3};\n\n    printf(&quot;data has a size of %d bytes\\n&quot;, sizeof(data) );\n\n    f(data);\n}\n\n\/**************************************\n***             Output              ***\n  data has a size of 12 bytes\n  here data has a size of 8 bytes\n*\/\n<\/pre><\/div>\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Pointers<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">start here: <a href=\"https:\/\/www.youtube.com\/watch?v=sBrrBEglBgI\" data-type=\"URL\" data-id=\"https:\/\/www.youtube.com\/watch?v=sBrrBEglBgI\">Introduction to Pointers<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">then here: <a href=\"https:\/\/www.youtube.com\/watch?v=yiw6S-25gxo\">Pointers and Arrays, part 1<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">then here: <a href=\"https:\/\/www.youtube.com\/watch?v=dW4QrRJsXYc\">Pointers and Arrays, part 2<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Pointers is difficult, yet very important, topic in C.  I have put together a separate page that goes into a lot of detail: <a href=\"https:\/\/www.brezeale.com\/?p=230\">Pointers<\/a> <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #1:<\/strong> What format specifiers (in order) should be used in the printf() statement in the following program? &nbsp;Note that in the program the correct format specifiers have been replaced by Z.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &amp;lt;stdio.h&gt;\n\nint main(void) {\n    int x = 5;\n    int* x_ptr = &amp;x;\n\n    printf(&quot;%Z, %Z, %Z, %Z\\n&quot;, x, *x_ptr, &amp;amp;x, x_ptr);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #2:<\/strong> We have the following code snippet<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nint main(void) {\n    int x = 5;\n    int *ptrA = &amp;x;\n    int **ptrB = &amp;ptrA;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">What value does each of the following represent? &nbsp;If the answer is an address, state such and include which variable the address is for.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>x<\/li><li>ptrA<\/li><li>ptrB<\/li><li>*ptrA<\/li><li>*ptrB<\/li><li>**ptrB<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #3:<\/strong> On a machine in which addresses are 8 bytes, what is printed by the following program:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint  main(void) {\n                    \/*          1         2         3 *\/\n                    \/* 123456789012345678901234567890 *\/\n    char fun&#x5B;]      = &quot;Programming is fun.&quot;;\n    char favorite&#x5B;] = &quot;I love programming in C.&quot;;\n    char *x = fun;\n\n    printf(&quot;%d, &quot;, sizeof(fun));\n    printf(&quot;%d, &quot;, sizeof(favorite));\n    printf(&quot;%d\\n&quot;, sizeof(x));\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #4:<\/strong> Rewrite the following program, replacing the subscript notation with the corresponding pointer notation. &nbsp;Only change the parts of the program necessary for the replacement.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n    \nint main(void) {\n    int data&#x5B;] = {1, 2, 3, 4, 5, 6}; \n    int i;\n    \n    for(i = 0; i &lt; 6; i++)\n        printf(&quot;%2d&quot;, data&#x5B;i]);\n} \n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #5:<\/strong> What does the following program print?  Creating a table for the values of i and ptr will help.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &amp;lt;stdio.h&gt;\n\nint main(void) {\n    int data&#x5B;] = {1, 2, 3, 4, 5, 6, 7};\n    int *ptr = data;\n    int i;\n\n    printf(&quot; i  *ptr\\n--------\\n&quot;);\n\n    for(i = 2; i &gt; -4; i--) {\n        printf(&quot;%2d,  %2d\\n&quot;, i, *ptr);\n        ptr += i;\n    }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #6:<\/strong> Rewrite the following program such that the function has a return type of <code>void<\/code> and the variable <code>y<\/code> gets its value using pointers.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &amp;lt;stdio.h&gt;\n\nint dbl(int num);\n\nint main(void) {\n    int x = 13;\n\n    x = dbl(x);\n    printf(&quot;x doubled is %d\\n&quot;, x);\n}\n\nint dbl(int num) {\n    return 2*num;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #7:<\/strong> In this program is a function call to a function called <code>getRange()<\/code>. &nbsp;<code>getRange()<\/code> is passed the addresses of two variables, <code>low<\/code> and <code>high<\/code>, as well as a variable called <code>mid<\/code>. &nbsp;<code>getRange()<\/code> calculates a value for <code>low<\/code> of 75% of the value of <code>mid<\/code> and a value of 125% of the value of <code>mid<\/code> for the value of <code>high<\/code>. &nbsp;The values of all three variables are printed from <code>main() <\/code>after the function call.  Write <code>getRange()<\/code>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &amp;lt;stdio.h&gt;\n\nint main(void) {\n    double low, high, mid = 40;\n\n    getRange(&amp;amp;low, &amp;amp;high, mid);\n\n    printf(&quot;low = %5.2f, mid = %5.2f, high = %5.2f\\n&quot;, \\ \n            low, mid, high);\n}\n\/**********************************************\n***                output                   ***\n  low = 30.00, mid = 40.00, high = 50.00\n*\/\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #8:<\/strong> The following program prints out the elements of the array. Why does this work since there is no row offset in the expression in the <code>printf()<\/code> statement?<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &amp;lt;stdio.h&gt;\n    \nint main(void) {\n    int data&#x5B;3]&#x5B;4] = { {11, 12, 13, 14},\n                       {15, 16, 17, 18},\n                       {19, 20, 21, 22} };\n    int i;\n        \n    for(i = 0; i &amp;lt; 12; i++)\n        printf(&quot;%d &quot;, *(*data + i) );\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #9:<\/strong> Write the function change() such that it changes the value of x from 5 to 99.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n  \nint main(void) {\n    int x = 5;\n    int *p1 = &amp;x;\n    int **p2 = &amp;p1;\n\n    printf(&quot;before: x = %d\\n&quot;, x);\n    change(&amp;p1);\n    printf(&quot; after: x = %d\\n&quot;, x);\n}\n<\/pre><\/div>\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Strings<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">start here: <a href=\"https:\/\/www.youtube.com\/watch?v=ijgLBRC8voM\">Introduction to Strings<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">then here: <a href=\"https:\/\/www.youtube.com\/watch?v=Md3nRS3zZQA\">String Library Functions<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #1:<\/strong> Change the program below to print &#8220;I love programming.&#8221;&nbsp;You<br>should do this by using the values in <code>lovetext<\/code> to change <code>hatetext<\/code>. &nbsp;Hint: &nbsp;think about the relationship between the index values of the letters in &#8220;love&#8221; and the index values for the word &#8220;hate&#8221;.  This can be done without creating any new variables.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint  main(void) {\n    int i;\n    char hatetext&#x5B;] = &quot;I hate programming.&quot;;\n    char lovetext&#x5B;] = &quot;love&quot;;\n\n    \/* Your code goes here. *\/\n\n    printf(&quot;%s\\n&quot;, hatetext);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #2:<\/strong> What does the following program print?<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &amp;lt;stdio.h&gt;\n\nint  main(void) {\n                \/*           1         2         3     *\/\n                \/* 01234567890123456789012345678901234 *\/\n    char text&#x5B;] = &quot;D   kfa i ifr s nfi   gfn   .&quot;;\n    int i = 0;\n\n    while(text&#x5B;i] != &#039;\\0&#039;)\n        if(text&#x5B;i] == &#039;f&#039;)\n            printf(&quot;\\n&quot;, text&#x5B;i++]);\n        else\n            printf(&quot;%c&quot;, text&#x5B;i++]);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #3:<\/strong> Modify the program below such that it prints<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\none\ntwo\nthree\nfour\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nint main(void) {\n    char* s&#x5B;] = {&quot;four&quot;, &quot;two&quot;, &quot;three&quot;, &quot;one&quot;};\n    int i;\n\n    \/* put your code here *\/\n\n    for(i = 0; i &lt; 4; i++)\n       printf(&quot;%s\\n&quot;, s&#x5B;i]);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #4:<\/strong> The following program has a bug. &nbsp;What is it and how do you correct it?<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nint main(void) {\n    char s&#x5B;] = &quot;Bob,53&quot;;\n    char *token1, *token2, *del = &quot;,&quot;;\n\n    token1 = strtok(s, del);\n    token2 = atoi( strtok(NULL, del) );\n\n    printf(&quot;%s is %d years old.\\n&quot;, token1, token2);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #5:<\/strong> The following program has a bug. &nbsp;What is it and how do you correct it?<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nint main(void) {\n    char x1&#x5B;] = &quot;cat&quot;;\n    char x2&#x5B;] = &quot;cadillac&quot;;\n    \n    \/* are the first letters the same? *\/\n    if( strcmp(x1&#x5B;0], x2&#x5B;0]) == 0 )\n        printf(&quot;the same&quot;);\n    else\n        printf(&quot;different&quot;);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #6:<\/strong> The program below calls a function, <code>f()<\/code>, that when given an array of strings will put the address of the shortest string in the first position (i.e., the one with index 0) and the address of the longest string in the last array position. &nbsp;Assume that the array will not be used after printing the shortest and longest strings and therefore the other strings in the array are unimportant.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Write <code>f()<\/code> without hard-coding it to this particular set of strings.<br>The types of the function parameters should match their use in the program.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nint main(void) {\n    char* t&#x5B;] = {&quot;horse&quot;, &quot;elephant&quot;, &quot;cat&quot;, &quot;rabbit&quot;};\n    int n;\n\n    n = sizeof( t )\/ sizeof( t&#x5B;0] );\n    f(t, n);\n\n    printf(&quot;shortest is %s, longest is %s\\n&quot;, t&#x5B;0], t&#x5B;n-1]);\n}\n<\/pre><\/div>\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">File Input\/Output<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">start here: <a href=\"https:\/\/www.youtube.com\/watch?v=HoGHEyCvxzs\" data-type=\"URL\" data-id=\"https:\/\/www.youtube.com\/watch?v=HoGHEyCvxzs\">File I\/O, part 1<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">then here: <a href=\"https:\/\/www.youtube.com\/watch?v=SpZjwX0KbL4\" data-type=\"URL\" data-id=\"https:\/\/www.youtube.com\/watch?v=SpZjwX0KbL4\">File I\/O, part 2<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">design example: <a rel=\"noreferrer noopener\" href=\"https:\/\/www.brezeale.com\/?p=590\" target=\"_blank\">Choosing an Electrical Plan<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #1:<\/strong> You have a file, <code>payments.txt<\/code>, that contains a list of people and the amount of payments made by each person.  Write a program that can read each line, printing the name and payment of each line. After all of the lines have been printed, print the sum of all payments.  Here is what <code>payments.txt<\/code> looks like:&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nBob 250\nGeorge 300\nSuzanne 250\nTony 200\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #2:<\/strong> You have a file, <code>people.txt<\/code>, with an unknown number of lines and four words per line, with the words separated by spaces. &nbsp;The size of the words can differ from line to line, but none will ever contain more than 30 characters and all of them plus the spaces separating the strings will not total more than 100 characters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Write a program that can read each line of the file and, if the second word is &#8220;Smith&#8221;, writes the words from that line in reverse order  to the file <code>smith.txt<\/code>.  Otherwise, do nothing with the line.  For example, if  <code>people.txt <\/code>is<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nJohn Wiliams 45 Main\nMary Smith 33 Elm\nRalph Wiggum 12 Oak\nBart Simpson 10 King\nJohn Smith 50 Sunset\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">then <code>smith.txt<\/code> will be<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nElm 33 Smith Mary\nSunset 50 Smith John\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #3:<\/strong> Write a program that can read a file, <code>names.txt<\/code>, that contains a single name on each line. &nbsp;If the name begins with a capital letter, print the name. &nbsp;If it begins with a lowercase letter, don&#8217;t print it. &nbsp;Each name contains less than 20 letters.<\/p>\n\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Structures<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">start here: <a href=\"https:\/\/www.youtube.com\/watch?v=TDvkYQQP9nA\">Structures<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">then here: <a href=\"https:\/\/www.youtube.com\/watch?v=-ANsiQ_BqmM\">application of structures<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #1:<\/strong> Create a structure that could be used to store the following<br>book information:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The C Programming Language, 2nd Edition<br>by Brian W. Kernighan and Dennis M. Ritchie<br>ISBN 9780131103627<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #2:<\/strong> Write a program that stores the following information in an array of structures (note you will hard-code these values in your program).  The values on a row represent a department, course number, section number, and enrollment.  Print the structures.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCSE,1310,1,45\nCSE,1310,2,50\nMATH,1426,1,75\nMATH,3330,2,60\nIE,3301,1,40\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #<\/strong>3<strong>:<\/strong> Store the course information from the previous problem into a CSV file and then read the file to construct the array of structures.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #<\/strong>4<strong>:<\/strong> Create a structure definition for storing a person&#8217;s first name and their age. &nbsp;Then write a program that creates an array of three structures and populates the values by prompting the user using a loop.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #<\/strong>5<strong>:<\/strong> The program below wants to add the cars from the array <code>new<\/code> to the array <code>old<\/code>, but only if the car is not already in the array (it&#8217;s in the array if both the make and model match).  Write the function <code>addCars<\/code>().<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nstruct car {\n    char make&#x5B;20];\n    char model&#x5B;20];\n};\n\nint main(void) {\n    struct car old&#x5B;20] = {{&quot;Ford&quot;, &quot;Explorer&quot;}, \n                          {&quot;Honda&quot;, &quot;Civic&quot;},\n                          {&quot;Honda&quot;, &quot;Accord&quot;},  \n                          {&quot;Chevy&quot;, &quot;Malibu&quot;},\n                          {&quot;Jeep&quot;, &quot;Cherokee&quot;}};\n    struct car new&#x5B;]   = {{&quot;Ford&quot;, &quot;Mustang&quot;}, \n                          {&quot;Honda&quot;, &quot;Accord&quot;},  \n                          {&quot;Chevy&quot;, &quot;Malibu&quot;}, \n                          {&quot;Mazda&quot;, &quot;RX-8&quot;}};\n    int i, oldCount = 5, newCount = 4;\n\n    addCars(old, &amp;oldCount, new, newCount);\n\n    for(i = 0; i &lt; oldCount; i++)\n        printf(&quot;%s %s\\n&quot;, old&#x5B;i].make, old&#x5B;i].model);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #<\/strong>6<strong>:<\/strong> For the program below, write the function <code>caps()<\/code> that changes the first letter of each animal name and type to uppercase.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nstruct animal {\n    char name&#x5B;20];\n    char type&#x5B;20];\n};\n\n\nint main(void) {\n    struct car a&#x5B;] = { {&quot;zebra&quot;, &quot;mammal&quot;},\n                       {&quot;gorilla&quot;, &quot;mammal&quot;},\n                       {&quot;shark&quot;, &quot;fish&quot;},\n                       {&quot;vulture&quot;, &quot;bird&quot;},\n                       {&quot;rhino&quot;, &quot;mammal&quot;}};\n    int size = 5;\n\n    caps(a, size);\n\n    for(i = 0; i &lt; size; i++)\n        printf(&quot;%s, %s\\n&quot;, a&#x5B;i].name, a&#x5B;i].type);\n}\n<\/pre><\/div>\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Dynamic Memory Allocation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">start here: <a href=\"https:\/\/www.youtube.com\/watch?v=uJ8XGn9lY1k\">Variable Scope, Storage Duration, and Dynamic Memory<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">then here: <a href=\"https:\/\/www.youtube.com\/watch?v=Ip26guUUWrU\">Examples of Dynamic Memory Allocation<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">then here: <a href=\"https:\/\/www.youtube.com\/watch?v=HhTy4h-Plzs\">application of dynamic memory<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #1:<\/strong> Using <code>malloc()<\/code>, allocate the memory for a 1D array<br>of type <code>int<\/code> that can store 5 integers. &nbsp;Then prompt the user for integers and store them in this array. &nbsp;The program should stop when either the user enters a negative integer or has provided 5 nonnegative integers. &nbsp;Print the array values that were stored.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #2:<\/strong> Using <code>malloc()<\/code>, allocate the memory for a 2D array of type <code>int<\/code> with 3 rows and 4 columns. &nbsp;The positions in the array should be assigned the sum of the array indices, for example, the [2][3] position will be assigned 2 + 3 = 5.  Print the array values that were stored.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #3:<\/strong> Using <code>malloc()<\/code>, allocate the memory for a 1D array<br>of type <code>int<\/code> that can store 5 integers. &nbsp;Then prompt the user for integers and store them in this array, stopping when the user enters a negative integer.  Each time you need to store a number but there is insufficient space to do so, double the amount of space using <code>realloc()<\/code>.  Print the array values that were stored.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can help yourself understand if the reallocation is working by printing a message each time you do it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #4:<\/strong> Write a program that can read a file, storing each line of the file in a dynamically-allocated array of strings.  You an assume that the number of lines in the file will not exceed 50 and that each line can be stored in 100 bytes.  Test what you have stored by printing the lines in reverse order from how they appear in the file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #5:<\/strong> The program below calls a function, <code>changeArray()<\/code>, that when given the address of a dynamically-allocated array will free this memory and then allocate a new array with enough space to store <code>size<\/code> ints.  Write this function and have it also store the integers 11 through 15 in the new array.  The purpose of this problem is to see if you can change the contents of a pointer created in one function from another function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nvoid printArray(int d&#x5B;], int size) {\n    int i;\n\n    for(i = 0; i &lt; size; i++)\n        printf(&quot;%d  &quot;, d&#x5B;i]);\n\n    printf(&quot;\\n&quot;);\n}\n  \nint main(void) {\n    int i, size = 5;\n    int* d = malloc(size * sizeof(int));\n\n    for(i = 0; i &lt; size; i++)\n        d&#x5B;i] = i+1;\n\n    printArray(d, size);\n\n    changeArray(&amp;d, size);\n    printArray(d, size);\n\n    free( d );\n}   \n<\/pre><\/div>\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Recursion<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">start here: <a href=\"https:\/\/www.youtube.com\/watch?v=w0cYHX34Nvk\">Introduction to Recursion<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #1:<\/strong> Write a function that when given an integer <code>n<\/code> returns the sum of the integers from 1 to n. &nbsp;The function should use recursion to produce the sum. &nbsp;No printing will occur in the function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #2:<\/strong> We can search a sorted array using a binary search.  Implement the function to search an array based upon this slightly modified pseudocode (from <a href=\"https:\/\/rosettacode.org\/wiki\/Binary_search\">https:\/\/rosettacode.org\/wiki\/Binary_search<\/a>).   The function either returns the index where it found an occurrence of <code>value<\/code> or returns -1 to indicate <code>value<\/code> is not in the array.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n\/\/ initially called with low = 0, high = N-1\nBinarySearch(A&#x5B;0..N-1], value, low, high) {\n    if (high &lt; low)\n        return -1 \n    mid = (low + high) \/ 2\n    if (A&#x5B;mid] &gt; value)\n        return BinarySearch(A, value, low, mid-1)\n    else if (A&#x5B;mid] &lt; value)\n        return BinarySearch(A, value, mid+1, high)\n    else\n        return mid\n}\n<\/pre><\/div>\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Linked Lists<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">start here: <a href=\"https:\/\/www.youtube.com\/watch?v=rs-KQvv7CiA\">Introduction to Linked Lists<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">then here: <a href=\"https:\/\/www.youtube.com\/watch?v=Unf-d7kNCjQ\">Constructing a Stack using a Linked List<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">then here: <a href=\"https:\/\/www.youtube.com\/watch?v=j8knzkEfpZM\">Constructing a Queue using a Linked List<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #1:<\/strong> The program below constructs a linked list.  Write the functions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>printList() &#8212; given the head of the linked list, print it<\/li><li>findNum() &#8212; given the head of the linked list and a number x, counts how many times x occurs in the linked list.  This count is returned by the function.<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nstruct node {\n    int value;\n    struct node *next;\n};\n\nstruct node* addNode(struct node *h, int n) {\n    struct node *temp = malloc( sizeof(struct node) );\n\n    temp-&gt;value = n;    \n    temp-&gt;next = h;    \n\n    return temp;\n}\n  \nint main(void) {\n    struct node *head = NULL;\n    int d&#x5B;] = {5, -8, 99, 7, 13, 99, 0, 123};\n    int i, len = sizeof(d)\/sizeof(d&#x5B;0]);\n    int x, y;\n\n    for(i = 0; i &lt; len; i++)\n        head = addNode(head, d&#x5B;i]);\n\n    printList(head);    \n\n    x = 20;\n    y = findNum(head, x);\n    printf(&quot;%d occurs %d times\\n&quot;, x, y);\n    \n    x = 13;\n    y = findNum(head, x);\n    printf(&quot;%d occurs %d times\\n&quot;, x, y);\n    \n    x = 99;\n    y = findNum(head, x);\n    printf(&quot;%d occurs %d times\\n&quot;, x, y);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #2:<\/strong> Write a program that reads a file in which each line contains a single name.  Store the names in a linked list and then print the list.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #3:<\/strong> Write a program that reads a file in which each line contains a single name.  Store the names in a linked list, but only if they are not already there.  Print the list afterwards.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #4:<\/strong> Write a program that creates two linked lists (using the same node definition) and then merges the two lists such that no values are duplicated.  For example, if the list values are<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n\/\/ both lists contain 2 and 4\nlist1 = {1, 2, 3, 4, 5}\nlist2 = {99, 2, 13, 4, 7, 123}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">then the merged list would have the following values (the order could differ)<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nmerged = {99, 13, 7, 123, 5, 4, 3, 2, 1} \n<\/pre><\/div>\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Binary Trees<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">start here: <a href=\"https:\/\/www.youtube.com\/watch?v=udHUW8QjY28\">Introduction to Binary Trees<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">then here: <a href=\"https:\/\/www.youtube.com\/watch?v=DqrpPT1hPl4\">Introduction to Binary Search Trees<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">then here: <a href=\"https:\/\/www.youtube.com\/watch?v=VpkYaWAY5e0\" data-type=\"URL\" data-id=\"https:\/\/www.youtube.com\/watch?v=VpkYaWAY5e0\">application of binary search tree<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #1:<\/strong> Write a function that can search a binary tree for all occurrences of a value.<\/p>\n\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Hash Tables<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">start here: <a href=\"https:\/\/www.youtube.com\/watch?v=Yo0zAqxUJm0\" data-type=\"URL\" data-id=\"https:\/\/www.youtube.com\/watch?v=Yo0zAqxUJm0\">Introduction to Hash Tables<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One thing I forgot to mention in my video is that a downside of hash tables is order doesn&#8217;t typically matter.  For example, I may create a hash table using the following as keys: &#8220;cat&#8221;, &#8220;dog&#8221;, &#8220;horse&#8221;, and &#8220;rabbit&#8221;, in that order.  But if I look at the order of the keys in the hash table it could be something like &#8220;horse&#8221;, &#8220;dog&#8221;, &#8220;rabbit&#8221;, and &#8220;cat&#8221;.  This is a problem if you care about retrieving things from the hash table in the order in which they were added to the hash table.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #1:<\/strong> The partial program below creates a hash table (you should fill this in based on the example given in the video) that resolves collisions using chaining. &nbsp;Write the function <code>countValues()<\/code>, which has the declaration given below and returns a count of how many key\/values pairs have a specific value.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#define SIZE 10\n\nstruct node {\n    char key&#x5B;50];\n    char value&#x5B;50];\n    struct node *next;\n};\n\nint hash(char key&#x5B;]);\nint countValues(struct node* table&#x5B;], char value&#x5B;]);\n\nint main(void) {\n    char key&#x5B;50]; \n    char value&#x5B;50]; \n    struct node *table&#x5B;SIZE] = {NULL};  \/* initialize all values to NULL *\/\n    int count;\n\n    \/*   assume the logic for constructing the hash table is here.  *\/\n\n    printf(&quot;Enter value:  &quot;);\n    fgets(value, sizeof(value), stdin);\n    count = countValues(table, value);  \/* write the function definition for countValues() *\/\n    printf(&quot;%d pairs have a value of %s\\n&quot;, count, value);\n}\n<\/pre><\/div>\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Graphs<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">start here: <a href=\"https:\/\/www.youtube.com\/watch?v=cMns9qT6WGk\">Introduction to Graphs<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #1:<\/strong> The partial program below calls <code>maxSrc()<\/code>, which receives a 2D array of type <code>int<\/code> representing an adjacency matrix. &nbsp;The rows represent the source nodes and the columns represent the destination nodes of a directed graph.  Write <code>maxSrc()<\/code> such that it returns the character representing the node with the most edges leaving it. &nbsp;Assume that the first row represents node A, the second row represents node B, and so forth.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nchar maxSrc(int m&#x5B;]&#x5B;4]);\n\nint main(void) {\n    int matrix&#x5B;]&#x5B;4] = {{0, 0, 1, 0},\n                       {1, 1, 1, 0},\n                       {0, 0, 0, 1},\n                       {0, 1, 1, 0}};\n\n    printf(&quot;node %c has the most edges leaving it\\n&quot;,\n            maxSrc(matrix) );\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Practice #2:<\/strong>  It&#8217;s easy to understand what it means to sort a list of things, but what about a graph.  Directed graphs show relationships.  Assuming we don&#8217;t have any cycles (i.e., there is a path back to where we started) if A points to B and B points to C, then we can think of this in a &#8216;sorted&#8217; fashion as A comes before B, which comes before C.  Applying this reasoning to a directed graph is called a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Topological_sorting\">topological sort<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Store the following list of classes in an adjacency matrix.  Then implement a topological sort using the pseudocode below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n course      is a prerequisite to\n----------------------------------------\nCSE 1310     CSE 1320, CSE 2315 \nCSE 1320     CSE 1325, CSE 2312, CSE 2320 \nCSE 1325 \nCSE 2312 \nCSE 2315     CSE 2320 \nCSE 2320 \nMATH 1426    CSE 2315\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n\/*  pseudocode of algorithm from \n      http:\/\/en.wikipedia.org\/wiki\/Topological_sorting\n\n    L &lt;- Empty list that will contain the sorted elements\n    S &lt;- Set of all nodes with no incoming edges\n    while S is non-empty do\n        remove a node n from S\n        insert n into L\n        for each node m with an edge e from n to m do\n            remove edge e from the graph\n            if m has no other incoming edges then\n                insert m into S\n    if graph has edges then\n        return error (graph has at least one cycle)\n    else \n        return L (a topologically sorted order)\n*\/\n<\/pre><\/div>\n\n\n<br>\n\n\n\n<h2 class=\"wp-block-heading\">Solutions to Practice Problems<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Statements, Practice #<\/strong>2<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint main(void) {\n    int a = 4;     \n    int b = 13; \n    int c = 6;\n    double d;\n\n    d = (double)(a + b + c)\/5;\n\n    printf(&quot;d = %.2f\\n&quot;, d);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Conditionals, Practice #<\/strong>1<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In version 1, each else clause is only evaluated if all previous if statements fail.  In version 2, each if statement is independent of the others so each is evaluated.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- <strong>Conditionals<\/strong>, Practice #<\/strong>2<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n     \nint main(void) {\n    int age = 40;\n     \n    if(age &gt; 50) {\n        if(age &lt; 75)\n            printf(&quot;first printf\\n&quot;);\n    }\n        else\n            printf(&quot;second printf\\n&quot;);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- <strong>Conditionals<\/strong>, Practice #<\/strong>3<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n     \nint main(void) {\n    int x;\n    \n    printf(&quot;enter an integer: &quot;);\n    scanf(&quot;%d&quot;, &amp;x);\n    \n    if(x%2 == 0 &amp;&amp; x%3 != 0)\n        printf(&quot;only 2 is a divisor\\n&quot;);\n    else if(x%2 != 0 &amp;&amp; x%3 == 0)\n        printf(&quot;only 3 is a divisor\\n&quot;);\n    else if(x%2 == 0 &amp;&amp; x%3 == 0)\n        printf(&quot;both 2 and 3 are divisors\\n&quot;);\n    else\n        printf(&quot;neither 2 nor 3 is a divisor\\n&quot;);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- <strong>Loops<\/strong>, Practice #<\/strong>1<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\ninclude &lt;stdio.h&gt;\n\nint main(void) {\n    int i;\n    \n    i = 8;\n    \n    while(i &gt; 5) {\n        printf(&quot;%d\\n&quot;, i);\n        \n        i--;\n    }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- <strong>Loops<\/strong>, Practice #<\/strong>2<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint main(void) {\n    int start, stop, i;\n    \n    printf(&quot;enter a starting integer: &quot;);\n    scanf(&quot;%d&quot;, &amp;start);\n    printf(&quot;enter a stopping integer: &quot;);\n    scanf(&quot;%d&quot;, &amp;stop);\n    \n    for(i = start; i &lt;= stop; i++)\n        if(i%2 == 0)\n            printf(&quot;x is Even\\n&quot;);\n        else\n            printf(&quot;%d\\n&quot;, i);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- <strong>Loops<\/strong>, Practice #<\/strong>3, version 1<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint main(void) {\n    int i; \n    \n    for(i = 1; i &lt; 101; i++) {\n        if(i%10 == 1)\n            printf(&quot;X&quot;);\n        \n        printf(&quot;%3d &quot;, i);\n        \n        if(i%10 == 0) \n            printf(&quot;\\n&quot;);\n    }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- <strong>Loops<\/strong>, Practice #<\/strong>3, version 2<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint main(void) {\n    int i, counter = 1;\n    \n    for(i = 1; i &lt; 101; i++) {\n        if(counter == 1)\n            printf(&quot;X&quot;);\n\n        printf(&quot;%3d &quot;, i);\n\n        if(counter == 10) {\n            printf(&quot;\\n&quot;);\n            counter = 0;  \/* reset counter *\/\n        }\n\n        counter++;\n    }\n}  \n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- <strong>Functions<\/strong>, Practice #<\/strong>1<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nfloat calcAge(int variableName);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- <strong>Functions<\/strong>, Practice #<\/strong>2<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\ndouble multNums(int hours, float rate); \/* declaration *\/\n\n\/* definition *\/\ndouble multNums(int hours, float rate) {\n    double prod = hours * rate;\n\n    return prod;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- <strong>Functions<\/strong>, Practice #<\/strong>3<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nvoid f(int a, int b) {\n    int i, div3 = 0, div5 = 0, other = 0;\n\n    for(i = a; i &lt;= b; i++) {\n        if(i%3 == 0) {\n            div3++;\n            printf(&quot;%d is divisible by 3\\n&quot;, i);\n        }\n\n        if(i%5 == 0) {\n            div5++;\n            printf(&quot;%d is divisible by 5\\n&quot;, i);\n        }\n\n        if(i%3 != 0 &amp;&amp; i%5 != 0) {\n            other++;\n            printf(&quot;%d is not divisible by 3 nor 5\\n&quot;, i);\n        }\n    }\n\n    printf(&quot;\\n&quot;);\n    printf(&quot;divisible by 3 = %d\\n&quot;, div3);\n    printf(&quot;divisible by 5 = %d\\n&quot;, div5);\n    printf(&quot;divisible by neither = %d\\n&quot;, other);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- <strong>Functions<\/strong>, Practice #<\/strong>4, version 1<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nint div2nd(int x) {           \n    int i, count = 0;\n    \n    for(i = 2; i &lt;= x; i++)\n        if(x%i == 0)\n            if(count == 0)\n                count++;\n            else\n                return i; \/* return on 2nd divisor *\/\n\n    return 0;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- <strong>Functions<\/strong>, Practice #<\/strong>4, version 2<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nint div2nd(int x) {\n    int i, second = 0, flag = 0;\n\n    for(i = 2; i &lt;= x; i++) {\n        if(x%i == 0)\n            flag++;\n\n        if(x%i == 0 &amp;&amp; flag == 2)\n            second = i;\n    }\n\n    return second;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- <strong>Functions<\/strong>, Practice #<\/strong>5<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint isPrime(int n) {\n    int i;\n\n    for(i = 2; i &lt; n; i++)\n        if(n%i == 0)\n            return 0;\n\n    return 1;\n}\n  \nint main( void ) { \n    int i;\n\n    printf(&quot;  ..&quot;);  \/* 1 is special case *\/\n    for(i = 2; i &lt; 101; i++) {\n        if( isPrime(i) )\n            printf(&quot; %3d&quot;, i);\n        else\n            printf(&quot;  ..&quot;);\n\n        if(i%10 == 0)\n            printf(&quot;\\n&quot;);\n    }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- <strong>Functions<\/strong>, Practice #<\/strong>6<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n12\n18\n24\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- <strong>Arrays<\/strong>, Practice #<\/strong>1<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n  \nint main(void) {\n    int d&#x5B;] = {1, 2, 7, 18, 23, 40};\n    int sum = 0, i;\n\n    for(i = 0; i &lt; 6; i++)\n        if(d&#x5B;i]%2 == 0)\n            sum += d&#x5B;i];\n\n    printf(&quot;sum = %d\\n&quot;, sum);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- <strong>Arrays<\/strong>, Practice #<\/strong>2<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n    \nvoid printArray(int &#x5B;], int);\n\nint main(void) {\n    int array1&#x5B;5] = {17, 23, 9, 4, 12};\n    int array2&#x5B;5];\n    int length = 5;\n    int i;\n\n    for(i = 0; i &lt; length; i++)\n        array2&#x5B;length - 1 - i] = array1&#x5B;i];\n\n    printf(&quot;array1: &quot;);\n    printArray(array1, length);\n    printf(&quot;array2: &quot;);\n    printArray(array2, length);\n}\n\nvoid printArray(int data&#x5B;], int length) {\n    int i;\n\n    for(i = 0; i &lt; length; i++)\n        printf(&quot;%2d &quot;, data&#x5B;i]);\n\n    printf(&quot;\\n&quot;);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- <strong>Arrays<\/strong>, Practice #<\/strong>3<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n    \nvoid printArray(int &#x5B;], int);\n\nint main(void) {\n    int array1&#x5B;5] = {1, 2, 3, 4, 5};\n    int array2&#x5B;5] = {99, 88, 77, 66, 55};\n    int temp;\n    int length = 5;\n    int i;\n\n    for(i = 0; i &lt; length; i++) {\n        temp = array1&#x5B;i];\n        array1&#x5B;i]= array2&#x5B;i];\n        array2&#x5B;i]= temp;\n    }\n\n    printf(&quot;array1: &quot;);\n    printArray(array1, length);\n    printf(&quot;array2: &quot;);\n    printArray(array2, length);\n}\n\nvoid printArray(int data&#x5B;], int length) {\n    int i;\n\n    for(i = 0; i &lt; length; i++)\n        printf(&quot;%2d &quot;, data&#x5B;i]);\n\n    printf(&quot;\\n&quot;);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- <strong>Arrays<\/strong>, Practice #<\/strong>4<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n    \nint main(void){\n    int data&#x5B;2]&#x5B;3] = { {15, 72,  9},\n                       { 4,  8, 11} };\n    int i, k;\n    \n    \/* I did not correct the formatting to make it\n       more clear what changes I made.  *\/\n        for(k = 0; k &lt; 3; k++) {\n    for(i = 0; i &lt; 2; i++) {\n            printf(&quot;%2d &quot;, data&#x5B;i]&#x5B;k]);\n        }\n        printf(&quot;\\n&quot;);\n    }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- <strong>Arrays<\/strong>, Practice #<\/strong>5<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nvoid addColumns(int d&#x5B;]&#x5B;3], int rows) {\n    int i;\n\n    for(i = 0; i &lt; rows; i++)\n        d&#x5B;i]&#x5B;1] = d&#x5B;i]&#x5B;0] + d&#x5B;i]&#x5B;2];\n}\n\nvoid printArray(int d&#x5B;]&#x5B;3], int rows) {\n    int i, k;\n    \n    for(i = 0; i &lt; rows; i++) {\n        for(k = 0; k &lt; 3; k++)\n            printf(&quot;%2d &quot;, d&#x5B;i]&#x5B;k]);\n\n        printf(&quot;\\n&quot;);\n    }\n}\n    \nint main(void){\n    int data&#x5B;]&#x5B;3] = { { 1, 2, 3},\n                      { 4, 5, 6},\n                      { 7, 8, 9},\n                      {10,11,12} };\n    int i, k, rows = 4;\n        \n    addColumns(data, rows);\n    printArray(data, rows);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- <strong>Arrays<\/strong>, Practice #<\/strong>6<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint main(void) {\n    int d&#x5B;] = {9, 8, 2, 15, 6, 32, 10, 4};\n    int i, length;\n\n    length = sizeof(d)\/sizeof(d&#x5B;0]);\n\n    for(i = 0; i &lt; length - 1; i++)\n        if(d&#x5B;i-1]%2 == 0 &amp;&amp; d&#x5B;i+1]%2 == 0)\n            d&#x5B;i] = 99;\n\n    for(i = 0; i &lt; length; i++)\n        printf(&quot;%d &quot;, d&#x5B;i]);\n\n    printf(&quot;\\n&quot;);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Variable Types, Practice #<\/strong>1<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Answer: The value produced by y = w + x will be incorrect.  Why?  The range of a short is -32,768 to 32,767, so it can&#8217;t store 35,000.  The solution is to use an integer type than can represent a larger range of values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Variable Types, Practice #<\/strong>2<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Answer: After we reach the largest signed value of a short, which is 32,767, adding 1 to it forces the sign bit to flip.  These bit patterns are then interpreted as negative numbers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Variable Types, Practice #<\/strong>3<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Answer: In <code>main()<\/code> we are returning the size of the array in bytes.  We passed the address of the first element of the array to <code>f()<\/code>, not the array itself.  Therefore, we are reporting the size of an address.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Pointers, Practice #<\/strong>1<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nprintf(&quot;%d, %d, %p, %p\\n&quot;, x, *x_ptr, &amp;x, x_ptr);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Pointers, Practice #<\/strong>2<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>x = 5<\/li><li>ptrA = address of x<\/li><li>ptrB = address of ptrB<\/li><li>*ptrA = 5<\/li><li>*ptrB = address of x<\/li><li>**ptrB = 5<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Pointers, Practice #<\/strong>3<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n20, 25, 8\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Pointers, Practice #<\/strong>4<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n    \nint main(void) {\n    int data&#x5B;] = {1, 2, 3, 4, 5, 6};\n    int i;\n    \n    for(i = 0; i &lt; 6; i++)\n        printf(&quot;%2d&quot;, *(data + i));\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Pointers, Practice #<\/strong>5<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n i  *ptr\n--------\n 2,   1\n 1,   3\n 0,   4\n-1,   4\n-2,   3\n-3,   1\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Pointers, Practice #<\/strong>6<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nvoid dbl(int* num) {\n    *num = 2 * *num;\n}\n\nint main(void) {\n    int x = 13;\n\n    dbl(&amp;x);\n\n    printf(&quot;x doubled is %d\\n&quot;, x);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Pointers, Practice #<\/strong>7<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvoid getRange(double *l, double *h, double m) {\n    *l = 0.75*m;\n    *h = 1.25*m;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Pointers, Practice #<\/strong>8<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The statement<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n*(*data + i)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">is equivalent to writing<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n*(*(data+0) + i)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">This means that we have a row offset of 0 to get the row address, then after dereferencing we use a column offset of i.  This never changes.  Remember that when we create a 2D array like in this case, all of the rows are really next to each other in memory so the first number of the second row is after the last number of the first row.  In this case we simply move down the line to each of the 12 numbers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Pointers, Practice #<\/strong>9<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvoid change(int **q) {\n    **q = 99;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Strings, Practice #<\/strong>1<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint  main(void) {\n    int i;\n    \/*                 0123456            *\/\n    char hatetext&#x5B;] = &quot;I hate programming.&quot;;\n    char lovetext&#x5B;] = &quot;love&quot;;\n\n    for(i = 0; i &lt; 4; i++)\n        hatetext&#x5B;i+2] = lovetext&#x5B;i];\n\n    printf(&quot;%s\\n&quot;, hatetext);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Strings, Practice #<\/strong>2<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nD   k\na i i\nr s n\ni   g\nn   .\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Strings, Practice #<\/strong>3<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nint main(void) {\n    char* s&#x5B;] = {&quot;four&quot;, &quot;two&quot;, &quot;three&quot;, &quot;one&quot;};\n    int i;\n\n    char* temp;\n\n    temp = s&#x5B;0];\n    s&#x5B;0] = s&#x5B;3];\n    s&#x5B;3] = temp;\n\n    for(i = 0; i &lt; 4; i++)\n       printf(&quot;%s\\n&quot;, s&#x5B;i]);\n} \n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Strings, Practice #<\/strong>4<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>atoi()<\/code> receives the address of a string of digit characters and returns the corresponding integer.  The bug is that this integer is saved in <code>token2<\/code>, which has type <code>char*<\/code>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nint main(void) {\n    char s&#x5B;] = &quot;Bob,53&quot;;\n    char *token1, *del = &quot;,&quot;;\n    int val;\n\n    token1 = strtok(s, del);\n    val = atoi( strtok(NULL, del) );\n\n    printf(&quot;%s is %d years old.\\n&quot;, token1, val);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Strings, Practice #<\/strong>5<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>strcmp()<\/code> expects to receive two addresses but receives characters (a type of int) instead.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint main(void) {\n    char x1&#x5B;] = &quot;cat&quot;;\n    char x2&#x5B;] = &quot;cadillac&quot;;\n\n    \/* are the first letters the same? *\/\n    if( x1&#x5B;0] == x2&#x5B;0] ) \n        printf(&quot;the same&quot;);\n    else\n        printf(&quot;different&quot;);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Strings, Practice #<\/strong>6<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvoid f(char* t&#x5B;], int size) {\n    int i, min, max;\n    char *shortest, *longest;\n\n    min = strlen( t&#x5B;0] );\n    max = strlen( t&#x5B;0] );\n\n    for(i = 0; i &lt; size; i++) {\n        if( strlen(t&#x5B;i]) &lt; min ) {\n            min = strlen(t&#x5B;i]);\n            shortest = t&#x5B;i];\n        }\n        if( strlen(t&#x5B;i]) &gt; max ) {\n            max = strlen(t&#x5B;i]);\n            longest = t&#x5B;i];\n        }\n    }\n\n    t&#x5B;0] = shortest;\n    t&#x5B;size-1] = longest;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Files, Practice #<\/strong>1<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n\nint main(void) {\n    FILE* fp;\n    char* filename = &quot;payments.txt&quot;;\n    char buffer&#x5B;50];\n    char *name, *amt, *del = &quot; \\n&quot;;\n    int sum = 0;\n\n    if( (fp = fopen(filename, &quot;r&quot;)) == NULL ) {\n        printf(&quot;unable to open %s\\n&quot;, filename);\n        exit(1);\n    }\n\n    while( fgets(buffer, sizeof(buffer), fp) != NULL ) {\n        name = strtok(buffer, del);\n        amt = strtok(NULL, del);\n        printf(&quot;%s make a payment of $%s\\n&quot;, name, amt);\n        sum += atoi(amt);\n    }\n\n    fclose( fp );\n\n    printf(&quot;total payments were %d\\n&quot;, sum);\n}\n\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Files, Practice #<\/strong>2<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n\nint main(void) {\n    FILE* inFile, *outFile;\n    char* inName = &quot;people.txt&quot;;\n    char* outName = &quot;smith.txt&quot;;\n    char buffer&#x5B;100];\n    char *t1, *t2, *t3, *t4, *del = &quot; \\n&quot;;\n    \n    if( (inFile = fopen(inName, &quot;r&quot;)) == NULL ) {\n        printf(&quot;unable to open %s\\n&quot;, inName);\n        exit(1);\n    }\n\n    if( (outFile = fopen(outName, &quot;w&quot;)) == NULL ) {\n        printf(&quot;unable to open %s\\n&quot;, outName);\n        exit(1);\n    }\n    \n    while( fgets(buffer, sizeof(buffer), inFile) != NULL ) {\n        t1 = strtok(buffer, del);\n        t2 = strtok(NULL, del);\n        t3 = strtok(NULL, del);\n        t4 = strtok(NULL, del);\n\n        if( strcmp(t2, &quot;Smith&quot;) == 0 )\n            fprintf(outFile, &quot;%s %s %s %s\\n&quot;, t4, t3, t2, t1);\n    }\n    \n    fclose( inFile );\n    fclose( outFile );\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Files, Practice #<\/strong>3<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n\nint main(void) {\n    FILE* fp;\n    char* filename = &quot;names.txt&quot;;\n    char buffer&#x5B;20]; \n    char *name, *del = &quot; \\n&quot;;\n    \n    if( (fp = fopen(filename, &quot;r&quot;)) == NULL ) {\n        printf(&quot;unable to open %s\\n&quot;, filename);\n        exit(1);\n    }   \n    \n    while( fgets(buffer, sizeof(buffer), fp) != NULL ) {\n        name = strtok(buffer, del);\n        \n        if( &#039;A&#039; &lt;= name&#x5B;0] &amp;&amp; name&#x5B;0] &lt;= &#039;Z&#039; )\n            printf(&quot;%s\\n&quot;, name);\n    }\n\n    fclose( fp );\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Structures, Practice #<\/strong>1<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The answer really depends on how detailed you want to be.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nstruct book {\n    char title&#x5B;100];\n    int edition;\n    char author1&#x5B;100];\n    char author2&#x5B;100];\n    char author3&#x5B;100];\n    int isbn;\n} ;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Structures, Practice #<\/strong>2<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nstruct collegeCourse {\n    char dept&#x5B;5];\n    short course;\n    short section;\n    short enrollment;\n};\n\nint main(void) {\n    struct collegeCourse d&#x5B;] = { {&quot;CSE&quot;, 1310, 1, 45},\n                                 {&quot;CSE&quot;, 1310, 2, 50},\n                                 {&quot;MATH&quot;, 1426, 1, 75},\n                                 {&quot;MATH&quot;, 3330, 2, 60},\n                                 {&quot;IE&quot;, 3301, 1, 40} };\n    int i;\n    \n    for(i = 0; i &lt; 5; i++) \n        printf(&quot;%s %d.%03d has %d students\\n&quot;, \n                d&#x5B;i].dept, d&#x5B;i].course,\n                d&#x5B;i].section, d&#x5B;i].enrollment);\n}       \n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Structures, Practice #<\/strong>3<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n\nstruct collegeCourse {\n    char dept&#x5B;5];\n    short course;\n    short section;\n    short enrollment;\n};\n\nint main(void) {\n    FILE* fp;\n    struct collegeCourse d&#x5B;10];  \/* assume not more than 10 courses *\/\n    char buffer&#x5B;100];\n    char *dept, *del = &quot;,\\n&quot;;\n    int i, counter = 0, course, section, enrollment;\n    char* filename = &quot;courses.csv&quot;;\n\n    if( (fp = fopen(filename, &quot;r&quot;)) == NULL ) {\n        printf(&quot;unable to open %s\\n&quot;, filename);\n        exit(1);\n    }\n\n    while( fgets(buffer, sizeof(buffer), fp) != NULL) {\n        strcpy(d&#x5B;counter].dept, strtok(buffer, del));\n        d&#x5B;counter].course = atoi(strtok(NULL, del));\n        d&#x5B;counter].section = atoi(strtok(NULL, del));\n        d&#x5B;counter].enrollment = atoi(strtok(NULL, del));\n\n        counter++;\n    }\n\n    fclose( fp );\n\n    for(i = 0; i &lt; counter; i++)\n        printf(&quot;%s %d.%03d has %d students\\n&quot;,\n                d&#x5B;i].dept, d&#x5B;i].course, \n                d&#x5B;i].section, d&#x5B;i].enrollment);\n}\n\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Structures, Practice #<\/strong>4<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nstruct person {\n    char name&#x5B;20];\n    int age;\n};\n\nint main(void) {\n    struct person d&#x5B;3];\n    char name&#x5B;20];\n    int i, age;\n\n    for(i = 0; i &lt; 3; i++) {\n        printf(&quot;name %d? &quot;, i+1);\n        scanf(&quot;%s&quot;, name);\n        strcpy(d&#x5B;i].name, name);\n        \n        printf(&quot;age %d? &quot;, i+1);\n        scanf(&quot;%d&quot;, &amp;age);\n        d&#x5B;i].age = age;\n    }\n\n    for(i = 0; i &lt; 3; i++) \n        printf(&quot;%s is %d years old\\n&quot;, d&#x5B;i].name, d&#x5B;i].age);\n} \n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Structures, Practice #<\/strong>5<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvoid addCars(struct car old&#x5B;], int *oCount, struct car new&#x5B;], int nCount) {\n    int i, k, found = 0;\n    \n    \/* for each new car, compare it to list of old cars *\/\n    for(i = 0; i &lt; nCount; i++) {\n        for(k = 0; k &lt; *oCount; k++) \n            if( strcmp(old&#x5B;k].make, new&#x5B;i].make) == 0 &amp;&amp;  \n                strcmp(old&#x5B;k].model, new&#x5B;i].model) == 0 ) \n                found = 1;  \/* found match, so don&#039;t add *\/\n\n        if( !found ) {\n                strcpy(old&#x5B;*oCount].make, new&#x5B;i].make);\n                strcpy(old&#x5B;*oCount].model, new&#x5B;i].model);\n                \n                *oCount += 1;\n        }   \n    }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Structures, Practice #<\/strong>6<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvoid caps(struct animal d&#x5B;], int size) {\n    int i;\n    \n    for(i = 0; i &lt; size; i++) {\n        d&#x5B;i].name&#x5B;0] -= 32;\n        d&#x5B;i].type&#x5B;0] -= 32;\n    }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- DynamicMemory, Practice #<\/strong>1<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n  \nint main(void) {\n     int i = 0, used, number;\n     int* d = malloc(5 * sizeof(int));\n\n     while(i &lt; 5) {\n         printf(&quot;enter integer (negative to stop): &quot;);\n         scanf(&quot;%d&quot;, &amp;number);\n\n         if(number &gt;= 0)\n             d&#x5B;i] = number;\n         else\n             break;\n\n         i++;\n     }\n\n     used = i;\n\n     for(i = 0; i &lt; used; i++)\n         printf(&quot;%d  &quot;, d&#x5B;i]);\n\n     printf(&quot;\\n&quot;);\n\n     free( d );\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- DynamicMemory, Practice #<\/strong>2<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n  \nint main(void) {\n     int i, k, rows = 3, cols = 4;\n     int** d = malloc(rows * sizeof(int*));\n\n     for(i = 0; i &lt; rows; i++)\n         d&#x5B;i] = malloc(cols * sizeof(int));\n     \n     for(i = 0; i &lt; rows; i++)\n         for(k = 0; k &lt; cols; k++)\n             d&#x5B;i]&#x5B;k] = i+k;\n\n     for(i = 0; i &lt; rows; i++) {\n         for(k = 0; k &lt; cols; k++)\n             printf(&quot;%2d &quot;, d&#x5B;i]&#x5B;k]);\n\n         printf(&quot;\\n&quot;);\n    }\n\n     for(i = 0; i &lt; rows; i++) \n         free(d&#x5B;i]);\n\n     free( d );\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- DynamicMemory, Practice #3<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n  \nint main(void) {\n     int i = 0, used, number, maxSize = 5;\n     int* d = malloc(maxSize * sizeof(int));\n\n     \/* create infinite loop *\/\n     while( 1 ) {\n         printf(&quot;enter integer (negative to stop): &quot;);\n         scanf(&quot;%d&quot;, &amp;number);\n\n         if(number &gt;= 0) {\n             if(i == maxSize) { \n                 maxSize = 2*maxSize;\n                 d = realloc(d, maxSize*sizeof(int));\n                 printf(&quot;*** realocated to store %d ints\\n&quot;, maxSize);\n             }\n\n             d&#x5B;i] = number;\n         }\n         else\n             break;\n\n         i++;\n     }\n\n     used = i;\n\n     for(i = 0; i &lt; used; i++)\n         printf(&quot;%d  &quot;, d&#x5B;i]);\n\n     printf(&quot;\\n&quot;);\n\n     free( d );\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- DynamicMemory, Practice #4<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n  \nint main(void) {\n    FILE* fp;\n    char* filename = &quot;people.txt&quot;;\n    char** lines;\n    char buffer&#x5B;100];\n    int i, count = 0;\n\n    if( (fp = fopen(filename, &quot;r&quot;)) == NULL ) {\n        printf(&quot;unable to open %s\\n&quot;, filename);\n        exit(1);\n    }\n\n    lines = malloc(50 * sizeof(char*));\n\n    while( fgets(buffer, sizeof(buffer), fp) != NULL ) {\n        lines&#x5B;count] = malloc( strlen(buffer) + 1 );\n        strcpy(lines&#x5B;count], buffer);\n\n        count++;\n    }\n\n\n    fclose( fp );\n\n    for(i = count-1; i &gt;= 0; i--)\n       printf(&quot;%s&quot;, lines&#x5B;i]);\n\n\n    for(i = 0; i &lt; count; i++)\n        free(lines&#x5B;i]);\n\n    free(lines);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- DynamicMemory, Practice #<\/strong>5<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nvoid changeArray(int **p, int size) {\n    int i;\n\n    free(*p);\n    *p = malloc(size * sizeof(int));\n\n    for(i = 0; i &lt; size; i++)\n        (*p)&#x5B;i] = i+11;\n}\n\nvoid printArray(int d&#x5B;], int size) {\n    int i;\n\n    for(i = 0; i &lt; size; i++)\n        printf(&quot;%d  &quot;, d&#x5B;i]);\n\n    printf(&quot;\\n&quot;);\n}\n  \nint main(void) {\n    int i, size = 5;\n    int* d = malloc(size * sizeof(int));\n\n    for(i = 0; i &lt; size; i++)\n        d&#x5B;i] = i+1;\n\n    printArray(d, size);\n\n    changeArray(&amp;d, size);\n    printArray(d, size);\n\n    free( d );\n}   \n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Recursion, Practice #1<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nint recSum(int n) {\n    if(n == 0)\n        return n;\n\n    return n + recSum(n - 1);\n}\n\nint main(void) {\n    int n = 10;\n\n    printf(&quot;%d\\n&quot;, recSum( n ));\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Recursion, Practice #<\/strong>2<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint binarySearch(int a&#x5B;], int value, int low, int high) {\n      int mid = (low + high) \/ 2;\n\n      if(high &lt; low)\n          return -1;\n\n      if(a&#x5B;mid] &gt; value)\n          return binarySearch(a, value, low, mid-1);\n      else if (a&#x5B;mid] &lt; value)\n          return binarySearch(a, value, mid+1, high);\n      else\n          return mid;\n}\n\nprintResults(int x&#x5B;], int size, int value) {\n    int index;\n\n    index = binarySearch(x, value, 0, size-1);\n\n    if(index &gt;= 0)\n        printf(&quot;%d has index %d\\n&quot;, value, index);\n    else\n        printf(&quot;%d is not in the array\\n&quot;, value);\n}\n\nint main(void) {\n    int d&#x5B;] = {1, 2, 6, 14, 21, 33, 59, 60, };\n    int n = sizeof(d)\/sizeof(d&#x5B;0]);\n    \n    printResults(d, n, 2);\n    printResults(d, n, 123);\n    printResults(d, n, 60);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- LinkedLists, Practice #1:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nstruct node {\n    int value;\n    struct node *next;\n};\n\nstruct node* addNode(struct node *h, int n) {\n    struct node *temp = malloc( sizeof(struct node) );\n\n    temp-&gt;value = n;    \n    temp-&gt;next = h;    \n\n    return temp;\n}\n\nvoid printList(struct node* h) {\n    while(h != NULL) {\n        printf(&quot;%d  &quot;, h-&gt;value);\n        h = h-&gt;next;\n    }\n\n    printf(&quot;\\n&quot;);\n}\n\nint findNum(struct node *h, int x) {\n    int count = 0;\n\n    while(h != NULL) {\n        if(h-&gt;value == x)\n            count++;\n\n        h = h-&gt;next;\n    }\n\n    return count;\n}\n\nint main(void) {\n    struct node *head = NULL;\n    int d&#x5B;] = {5, -8, 99, 7, 13, 99, 0, 123};\n    int i, len = sizeof(d)\/sizeof(d&#x5B;0]);\n    int x, y;\n\n    for(i = 0; i &lt; len; i++)\n        head = addNode(head, d&#x5B;i]);\n\n    printList(head);    \n\n    x = 20;\n    y = findNum(head, x);\n    printf(&quot;%d occurs %d times\\n&quot;, x, y);\n    \n    x = 13;\n    y = findNum(head, x);\n    printf(&quot;%d occurs %d times\\n&quot;, x, y);\n    \n    x = 99;\n    y = findNum(head, x);\n    printf(&quot;%d occurs %d times\\n&quot;, x, y);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- LinkedLists, Practice #2:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n#include &lt;stdlib.h&gt;\n\nstruct node {\n    char name&#x5B;20];\n    struct node *next;\n};\n\nstruct node* addNode(struct node *h, char n&#x5B;]) {\n    struct node *temp = malloc( sizeof(struct node) );\n\n    strcpy(temp-&gt;name, n);    \n    temp-&gt;next = h;    \n\n    return temp;\n}\n\nvoid printList(struct node* h) {\n    while(h != NULL) {\n        printf(&quot;%s  &quot;, h-&gt;name);\n        h = h-&gt;next;\n    }\n\n    printf(&quot;\\n&quot;);\n}\n\nint main(void) {\n    struct node *head = NULL;\n    FILE* fp;\n    char* filename = &quot;people.txt&quot;;\n    char buffer&#x5B;20];\n    char *token, *del = &quot;\\n&quot;;\n\n    fp = fopen(filename, &quot;r&quot;);\n\n    while( fgets(buffer, sizeof(buffer), fp) != NULL ) {\n        token = strtok(buffer, del);  \/* eliminate the newlines *\/\n        head = addNode(head, token);\n    }\n\n    printList(head);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- LinkedLists, Practice #3:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n#include &lt;stdlib.h&gt;\n\nstruct node {\n    char name&#x5B;20];\n    struct node *next;\n};\n\nstruct node* addNode(struct node *h, char n&#x5B;]) {\n    struct node *temp = malloc( sizeof(struct node) );\n\n    strcpy(temp-&gt;name, n);    \n    temp-&gt;next = h;    \n\n    return temp;\n}\n\nvoid printList(struct node* h) {\n    while(h != NULL) {\n        printf(&quot;%s  &quot;, h-&gt;name);\n        h = h-&gt;next;\n    }\n\n    printf(&quot;\\n&quot;);\n}\n\nint inList(struct node* h, char n&#x5B;]) {\n    while(h != NULL) {\n        if( strcmp(h-&gt;name, n) == 0 )\n            return 1;\n\n        h = h-&gt;next;\n    }\n\n    return 0;\n}\n\nint main(void) {\n    struct node *head = NULL;\n    FILE* fp;\n    char* filename = &quot;people.txt&quot;;\n    char buffer&#x5B;20];\n    char *token, *del = &quot;\\n&quot;;\n\n    fp = fopen(filename, &quot;r&quot;);\n\n    while( fgets(buffer, sizeof(buffer), fp) != NULL ) {\n        token = strtok(buffer, del);  \/* eliminate the newlines *\/\n\n        if( !inList(head, token) )\n            head = addNode(head, token);\n    }\n\n    printList(head);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- LinkedLists, Practice #<\/strong>4<strong>:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nstruct node {\n    int value;\n    struct node *next;\n};\n\nstruct node* addNode(struct node *h, int n) {\n    struct node *temp = malloc( sizeof(struct node) );\n\n    temp-&gt;value = n;    \n    temp-&gt;next = h;    \n\n    return temp;\n}\n\nvoid printList(struct node* h) {\n    while(h != NULL) {\n        printf(&quot;%d  &quot;, h-&gt;value);\n        h = h-&gt;next;\n    }\n\n    printf(&quot;\\n&quot;);\n}\n\nvoid mergeLists(struct node **f, struct node *s) {\n    struct node *temp;\n    int found;\n\n    \/* for each node value in s, search *f *\/\n    while(s != NULL) {\n        temp = *f;\n        found = 0;\n\n        while(temp != NULL) {\n            if(s-&gt;value == temp-&gt;value)\n                found = 1;\n\n            temp = temp-&gt;next;\n        }\n\n        if( !found ) {\n            temp = malloc( sizeof(struct node) );\n\n            temp-&gt;value = s-&gt;value;\n            temp-&gt;next = *f;  \/* add at head of *f *\/\n\n            (*f) = temp;\n        }\n\n        s = s-&gt;next;\n    }\n}\n\nint main(void) {\n    struct node *first = NULL, *second = NULL;\n    int f&#x5B;] = {1, 2, 3, 4, 5};\n    int s&#x5B;] = {99, 2, 13, 4, 7, 123};\n    int i, fLen = 5, sLen = 6;\n\n    for(i = 0; i &lt; fLen; i++)\n        first = addNode(first, f&#x5B;i]);\n    printList(first);\n\n    for(i = 0; i &lt; sLen; i++)\n        second = addNode(second, s&#x5B;i]);\n    printList(second);\n\n    mergeLists(&amp;first, second);\n    printList(first);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- BinaryTrees, Practice #<\/strong>1<strong>:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvoid search(struct node* root, int x, int *p) { \n   \/*\n       root: address of current node\n          x: number to search for\n          p: address of counter\n   *\/\n   if(root == NULL)\n       return;\n\n   if(root-&gt;n == x)\n       *p += 1;\n\n   search(root-&gt;left, x, p);\n   search(root-&gt;right, x, p);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- HashTables, Practice #<\/strong>1<strong>:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nint countValues(struct node* table&#x5B;], char value&#x5B;]) {\n    int i, count = 0;\n    struct node *temp;\n\n    for(i = 0; i &lt; SIZE; i++)\n        if(table&#x5B;i] != NULL)\n            for(temp = table&#x5B;i]; temp != NULL; temp = temp-&gt;next) {\n                if( strcmp(temp-&gt;value, value) == 0 )\n                    count++;\n            }\n    \n    return count;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Graphs, Practice #<\/strong>1<strong>:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nchar maxSrc(int m&#x5B;]&#x5B;4]);\n\nint main(void) {\n    int matrix&#x5B;]&#x5B;4] = {{0, 0, 1, 0},\n                       {1, 1, 1, 0},\n                       {0, 0, 0, 1},\n                       {0, 1, 1, 0}};\n\n    printf(&quot;node %c has the most edges leaving it\\n&quot;,\n            maxSrc(matrix) );\n}\n\nchar maxSrc(int m&#x5B;]&#x5B;4]) {\n    int i, k, max = 0, maxCount = 0, rowMax;\n\n    for(i = 0; i &lt; 4; i++)\n    {\n        rowMax = 0;\n        for(k = 0; k &lt; 4; k++)\n            rowMax += m&#x5B;i]&#x5B;k];\n\n        if(rowMax &gt; maxCount) {\n            maxCount = rowMax;\n            max = i;\n        }\n    }\n\n    return (char) (max + &#039;A&#039;);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2014\u2014\u2014- Graphs, Practice #<\/strong>2<strong>:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#define SIZE 8\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nstruct node {\n    int index;\n    struct node *next;\n} ;\n\nstruct node * topSort(int d&#x5B;]&#x5B;SIZE]);\nint hasIncoming(int d&#x5B;]&#x5B;SIZE], int col);\nvoid enqueue(struct node **head, int index, int *queueLength);\nstruct node * dequeue(struct node * head, int *queueLength);\n\nvoid printGraph(int d&#x5B;]&#x5B;SIZE], char *label&#x5B;]);\nvoid printList(struct node *head, char *label&#x5B;]);\nvoid freeMemory(struct node *head);\n\n\nint main(void) {\n    char *label&#x5B;] = {&quot;CSE 1310&quot;, &quot;CSE 1320&quot;, &quot;CSE 1325&quot;, &quot;CSE 2312&quot;, &quot;CSE 2315&quot;, &quot;CSE 2320&quot;, &quot;CSE 3310&quot;, &quot;MATH 1426&quot;};\n    \/*  directed graph 10 20 25 12 15 20 10 26  *\/\n    int dir&#x5B;]&#x5B;SIZE] = {{0, 1, 0, 0, 1, 0, 0, 0},    \/* CSE 1310  *\/\n                       {0, 0, 1, 1, 0, 1, 0, 0},    \/* CSE 1320  *\/\n                       {0, 0, 0, 0, 0, 0, 1, 0},    \/* CSE 1325  *\/\n                       {0, 0, 0, 0, 0, 0, 0, 0},    \/* CSE 2312  *\/\n                       {0, 0, 0, 0, 0, 1, 1, 0},    \/* CSE 2315  *\/\n                       {0, 0, 0, 0, 0, 0, 0, 0},    \/* CSE 2320  *\/\n                       {0, 0, 0, 0, 0, 0, 0, 0},    \/* CSE 3310  *\/\n                       {0, 0, 0, 0, 1, 0, 0, 0}};   \/* MATH 1426 *\/\n    int i;\n    struct node *L;\n    \n    printf(&quot;directed\\n&quot;);\n    printGraph(dir, label);\n\n    printf(&quot;\\ntopological sort\\n&quot;);\n    L = topSort(dir);\n    printList( L, label );\n    freeMemory( L );\n}\n\nvoid printList(struct node *head, char *label&#x5B;]) {\n    for( ; head != NULL; head = head-&gt;next) {\n        printf(&quot;%s, &quot;, label&#x5B; head-&gt;index ]);\n    }\n    printf(&quot;\\n&quot;);\n}\n\nstruct node * topSort(int d&#x5B;]&#x5B;SIZE]) {\n    int i, m, n, queueLengthL = 0, queueLengthS = 0;\n    struct node *L = NULL, *S = NULL;\n\n    \/* find nodes without incoming edges *\/\n    for(i = 0; i &lt; SIZE; i++)\n        if( !hasIncoming(d, i) )\n            enqueue( &amp;S, i, &amp;queueLengthS );\n\n    \/* while S is non-empty *\/\n    while( queueLengthS &gt; 0 ) {\n        n = S-&gt;index;\n        S = dequeue( S, &amp;queueLengthS );\n        enqueue( &amp;L, n, &amp;queueLengthL );\n\n        for (m = 0; m &lt; SIZE; m++) {\n            if(d&#x5B;n]&#x5B;m] != 0) {\n                d&#x5B;n]&#x5B;m] = 0;\n\n                \/* if m has no other incoming edges *\/\n                if( !hasIncoming(d, m) )\n                    enqueue( &amp;S, m, &amp;queueLengthS );\n            }\n        }\n    }\n\n    return L;\n}\n\nint hasIncoming(int d&#x5B;]&#x5B;SIZE], int c) {\n    int r, incoming_edges = 0;\n    for(r = 0; r &lt; SIZE; r++)\n        if( d&#x5B;r]&#x5B;c] != 0 )\n            incoming_edges = 1;\n\n    return incoming_edges;\n}\n\nvoid printGraph(int d&#x5B;]&#x5B;SIZE], char *label&#x5B;]) {\n    int i, k;\n\n    for(i = 0; i &lt; SIZE; i++) {\n        printf(&quot;%9s: &quot;, label&#x5B;i]);\n        for(k = 0; k &lt; SIZE; k++)\n            if(d&#x5B;i]&#x5B;k] == 1)\n                printf(&quot;%9s &quot;, label&#x5B;k]);\n\n        printf(&quot;\\n&quot;);\n    }\n}\n\nvoid enqueue(struct node **head, int index, int *queueLength) {\n    \/*\n        since queues will be short, don&#039;t keep track of tail\n    *\/\n    if( *queueLength == 0 ) {\n        *head = malloc( sizeof(struct node) );\n        (*head)-&gt;next = NULL;\n        (*head)-&gt;index = index;\n    } else {\n        struct node *tail;\n        struct node *temp = malloc( sizeof(struct node) );\n        temp-&gt;next = NULL;\n        temp-&gt;index = index;\n\n        \/*  find end of queue  *\/\n        for(tail = *head ; \n            tail-&gt;next != NULL; \n            tail = tail-&gt;next )\n            ;\n\n        tail-&gt;next = temp;\n    }\n\n    *queueLength += 1;\n}\n\nstruct node * dequeue(struct node * head, int *queueLength){\n    struct node *temp = head-&gt;next;\n\n    free( head );\n    *queueLength -= 1;\n\n    return temp;\n}\n\nvoid freeMemory(struct node *head) {\n    struct node *temp;\n\n    for( ; head != NULL; head = temp-&gt;next) {\n        temp = head; \n        free( head );\n    }\n}\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>The purpose of this page is to guide someone wishing to learn to program using the C programming language, specifically the ANSI Standard of 1989. Each section consists of a link to a video that teaches an element of the language sample problems for you to try. My solutions to the problems can be found at the bottom of the page. It\u2019s OK if your solution isn\u2019t the same as mine. Some sections also include a link to a video&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.brezeale.com\/?p=427\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[6,9],"tags":[],"class_list":["post-427","post","type-post","status-publish","format-standard","hentry","category-c-programming","category-programming"],"_links":{"self":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/427","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=427"}],"version-history":[{"count":10,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/427\/revisions"}],"predecessor-version":[{"id":693,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/427\/revisions\/693"}],"wp:attachment":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}