<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Classes on John Bowers</title>
    <link>https://w3.cs.jmu.edu/bowersjc/classes/</link>
    <description>Recent content in Classes on John Bowers</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Thu, 20 Aug 2015 10:55:38 -0400</lastBuildDate>
    <atom:link href="https://w3.cs.jmu.edu/bowersjc/classes/index.xml" rel="self" type="application/rss+xml" />
    
    <item>
      <title>Programming Fundamentals (CS 139)</title>
      <link>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs139/</link>
      <pubDate>Thu, 20 Aug 2015 10:55:38 -0400</pubDate>
      
      <guid>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs139/</guid>
      <description></description>
    </item>
    
    <item>
      <title>CS240</title>
      <link>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240/</link>
      <pubDate>Thu, 20 Aug 2015 10:55:33 -0400</pubDate>
      
      <guid>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240/</guid>
      <description>

&lt;p&gt;This is the course website for the Fall 2015 offering of CS 240 (Algorithms and Datastructures) taught by &lt;a href=&#34;../..&#34;&gt;John Bowers&lt;/a&gt; (i.e. Sec. 03).&lt;/p&gt;

&lt;h2 id=&#34;welcome:df85e164d207c3442fbb7a841578c17c&#34;&gt;Welcome!&lt;/h2&gt;

&lt;p&gt;The class meets MWF 11:15-12:05 in ISAT 243 or ISAT 248 starting August 31 and ending December 11. Please visit the &lt;a href=&#34;../cs240calendar&#34;&gt;calendar&lt;/a&gt; for a detailed schedule.&lt;/p&gt;

&lt;p&gt;My office hours are posted on my &lt;a href=&#34;../../../teaching&#34;&gt;teaching&lt;/a&gt; page.&lt;/p&gt;

&lt;p&gt;I am available outside of office hours by appointment. Please send me an email with several candidate times to meet and I will do my best to accomodate.&lt;/p&gt;

&lt;h2 id=&#34;news-and-updates:df85e164d207c3442fbb7a841578c17c&#34;&gt;News and Updates!&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Note that Lecture 2 is in the Mac lab, ISAT 248.&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;../../../files/lec01.pdf&#34;&gt;Lecture 1 slides&lt;/a&gt; are posted!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;course-links:df85e164d207c3442fbb7a841578c17c&#34;&gt;Course Links&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;../cs240syllabus&#34;&gt;Syllabus&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;../cs240calendar&#34;&gt;Calendar&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;../cs240assignments&#34;&gt;Assignments&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;../cs240resources&#34;&gt;Resources&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;piazza:df85e164d207c3442fbb7a841578c17c&#34;&gt;Piazza&lt;/h2&gt;

&lt;p&gt;This semester we are using Piazza for class discussions and announcements. If you have questions during this course, I encourage you to post
them to Piazza rather than emailing me directly. If you email me directly a question that I think may be of general interest, you should
expect a &amp;ldquo;please post this question to Piazza and I&amp;rsquo;ll answer it&amp;rdquo; response. I believe that using Piazza as the primary means of electronic
communication is a value-add for everyone in the course, since you can sometimes get responses back faster from other students (especially the
night before things are due) and everyone benefits from my responses.&lt;/p&gt;

&lt;h2 id=&#34;class-grades-quizzes:df85e164d207c3442fbb7a841578c17c&#34;&gt;Class Grades &amp;amp; Quizzes&lt;/h2&gt;

&lt;p&gt;Class grades and quizzes are accessible in &lt;a href=&#34;https://canvas.jmu.edu&#34;&gt;Canvas&lt;/a&gt;.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Syllabus</title>
      <link>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240syllabus/</link>
      <pubDate>Thu, 20 Aug 2015 11:13:50 -0400</pubDate>
      
      <guid>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240syllabus/</guid>
      <description>

&lt;h2 id=&#34;objectives-and-expectations:78d0232414fd482d847cc5d8c36d4578&#34;&gt;Objectives and Expectations&lt;/h2&gt;

&lt;p&gt;The goal of this course is to become proficient in using and analyzing basic
data structures and algorithms. By the end of the course, you should be able to do the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Develop programs in a non-Java language (in this case, C) utilizing common data structures, modular/reusable patterns, memory management strategies, and error handling.&lt;/li&gt;
&lt;li&gt;Describe, explain, and use an abstract container framework that includes stacks, queues, lists, sets, and maps.&lt;/li&gt;
&lt;li&gt;Implement the containers in the container framework using contiguous or linked data structures. Contiguous data structures include arrays and hash tables, and linked data structures include linked lists and binary trees.&lt;/li&gt;
&lt;li&gt;Implement searching algorithms, including linear search and binary search.&lt;/li&gt;
&lt;li&gt;Implement a variety of sorting algorithms, including insertion sort, selection sort, merge sort, quick sort, and heap sort.&lt;/li&gt;
&lt;li&gt;Read and write recursive algorithms, analyze recursion, and remove recursion when appropriate.&lt;/li&gt;
&lt;li&gt;Implement tree traversal algorithms as well as binary search tree insertion, deletion, and search algorithms.&lt;/li&gt;
&lt;li&gt;Implement hash tables using chaining or open addressing for collision resolution.&lt;/li&gt;
&lt;li&gt;State the asymptotic running times of the algorithms and data structure operations studied in this course, and explain the practical behavior of algorithms with various asymptotic running times.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;course-textbook:78d0232414fd482d847cc5d8c36d4578&#34;&gt;Course Textbook&lt;/h2&gt;

&lt;p&gt;&lt;a href=&#34;http://opendatastructures.org/ods-python.pdf&#34;&gt;Open Data Structures&lt;/a&gt; by Pat Morin&lt;/p&gt;

&lt;p&gt;We will be using an open-source textbook this semester. The &lt;a href=&#34;http://opendatastructures.org/ods-python.pdf&#34;&gt;compiled text&lt;/a&gt; itself as
well as all of its source materials (in LaTeX and various other languages) are
available on the &lt;a href=&#34;http://opendatastructures.org/&#34;&gt;textbook website&lt;/a&gt;. We will be using the pseudocode version. The textbook
is required for the course, so I highly recommend obtaining a printed copy.
You have several options for doing so:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We have arranged with the campus bookstore to sell a &amp;ldquo;coursepack&amp;rdquo; consisting of a coil-bound, landscape printout of the pdf (four PDF pages per 8.5&amp;rdquo; x 11&amp;rdquo; sheet). The cost should be $17.39. This is the recommended option for conveniency.&lt;/li&gt;
&lt;li&gt;We have uploaded the PDF to a web-based printing service, and you may order a 6&amp;rdquo; x 9&amp;rdquo; paperbound copy from the &lt;a href=&#34;http://www.lulu.com/shop/pat-morin/open-data-structures/paperback/product-22302549.html&#34;&gt;product page&lt;/a&gt; if you prefer a more traditionally-bound book. The cost is $5.61 plus shipping. I recommend ordering early because it takes 2-3 weeks to print and ship your copy.&lt;/li&gt;
&lt;li&gt;You may print it yourself using your own equipment or a standard print center such as FedEx. This could end up being more expensive that the other two options, but if you already have an established account at such a business, you may find it more convenient.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will also be using excerpts from other resources as needed, such as the &lt;a href=&#34;https://www.seas.harvard.edu/courses/cs20/MIT6_042Notes.pdf&#34;&gt;Mathematics for Computer Science&lt;/a&gt; online textbook by Eric Lehman, F. Thomson Leighton, and Albert R. Meyer.&lt;/p&gt;

&lt;h2 id=&#34;grading-criteria:78d0232414fd482d847cc5d8c36d4578&#34;&gt;Grading Criteria&lt;/h2&gt;

&lt;p&gt;You are responsible for all material discussed in lecture and discussion section and posted on the class web page, including announcements, deadlines, policies, etc.&lt;/p&gt;

&lt;p&gt;Your final course grade will be determined according to the following percentages:&lt;/p&gt;

&lt;table&gt;
    &lt;tr&gt;
        &lt;td&gt;Quizzes and Homeworks&lt;/td&gt; &lt;td&gt;30%&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt;Programming Projects&lt;/td&gt; &lt;td&gt;25%&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt;Midterm Exams&lt;/td&gt; &lt;td&gt;30%&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt;Final Exam&lt;/td&gt; &lt;td&gt;15%&lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;Final course letter grades may be curved if necessary at the end of the semester, based on each student&amp;rsquo;s
overall performance for all coursework.&lt;/p&gt;

&lt;p&gt;If you believe I have made an error while grading your work or calculating your final score, please bring
it to my attention after class or during office hours. If I determine that there has been a simple mistake,
I will fix it immediately and no formal request is necessary.&lt;/p&gt;

&lt;p&gt;If you believe an exam question or assignment has been graded unfairly, you must submit a verbal or written
formal request for a regrade. Such requests must be submitted within one week of when the assignment in question
is returned to you. &lt;strong&gt;Any coursework submitted for reconsideration may be regraded in its entirety, which could
result in a lower score if warranted.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Completing the programming assignments is an essential part of the course. Therefore, I reserve the right to
&lt;strong&gt;fail any student who does not make a good-faith attempt on all course projects, regardless of the student&amp;rsquo;s
performance or scores on the other coursework&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&#34;instructor-contact-info:78d0232414fd482d847cc5d8c36d4578&#34;&gt;Instructor Contact Info&lt;/h2&gt;

&lt;p&gt;Please post generic questions to Piazza, where other students may answer and/or benefit from my answers.
My email is bowersjc at the standard domain. My office is in ISAT 217, and my office hours are posted
on my &lt;a href=&#34;../../../teaching&#34;&gt;teaching page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I am also sometimes available outside office hours by appointment; if you wish to make an appointment,
please email me several candidate times that work for you and I will do my best to accommodate.&lt;/p&gt;

&lt;h2 id=&#34;course-policies:78d0232414fd482d847cc5d8c36d4578&#34;&gt;Course Policies&lt;/h2&gt;

&lt;p&gt;Important announcements will be made in class and/or on Piazza. Please make it a habit to check Piazza daily.&lt;/p&gt;

&lt;p&gt;Although every effort has been made to be complete and accurate, unforeseen circumstances arising during the
semester could require the adjustment of any material given here. Consequently, given due notice to students,
I reserve the right to change any information on this syllabus or in other course materials.&lt;/p&gt;

&lt;p&gt;You are permitted to use course materials for your own personal use only. Course materials may not be distributed
publicly or provided to others (excepting other students in the course), in any way or format unless explicitly allowed.&lt;/p&gt;

&lt;h2 id=&#34;attendance-and-participation:78d0232414fd482d847cc5d8c36d4578&#34;&gt;Attendance and Participation&lt;/h2&gt;

&lt;p&gt;I view your time as valuable, and my goal as a teacher is to make class attendance and participation well worth your time investment.
Though I will not take attendance in this course, I strongly encourage you to attend every class session and participate fully in
order to derive the maximum benefit of this course. If you believe that there is something I could change about the way I am handling
the course in order to improve its effectiveness for you, please let me know via email or office hours.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Please Note: I do occasionally give graded quizzes or graded group work assignments during class periods without advance notice.&lt;/strong&gt;*&lt;/p&gt;

&lt;p&gt;Please silence your cell phone while class is in session. If you have a laptop or tablet, you are encouraged to bring it to class and use it to work along with programming examples and exercises. Mute the volume to avoid unintended interruptions, and do not use any electronic devices for activities that may distract other students. Repeated violations of this policy may result in disciplinary action or a grade penalty in the course.&lt;/p&gt;

&lt;p&gt;I strongly encourage you to check the main website and the Piazza web forum regularly for important announcements (usually regarding programming projects). You may also use the Piazza forum to ask general questions of interest to the class as a whole (e.g., administrative issues or project clarification questions) as well as to offer each other general advice on class assignments. However, do not post any information that would violate the university academic integrity policy. If you are unsure about this, please email me for approval before you post.&lt;/p&gt;

&lt;h2 id=&#34;programming-projects:78d0232414fd482d847cc5d8c36d4578&#34;&gt;Programming Projects&lt;/h2&gt;

&lt;p&gt;Projects must be submitted electronically following the instructions given in class and on the website.
Projects may not be submitted by any other means (e.g., do not email your projects to me unless I request that).
It is your responsibility to test your program and verify that it works properly before submitting it.&lt;/p&gt;

&lt;p&gt;All projects are due at 23:59 (11:59pm) on the day indicated on the project assignment unless noted otherwise.&lt;/p&gt;

&lt;p&gt;Projects may be submitted up to 72 hours late for a 10% penalty per 24-hour period. For example, a submission
that would have earned 90 points in an on-time submission will earn 90 x 0.90 = 81 points if submitted up to
24 hours late, or 90 x 0.80 = 72 points if submitted up to 48 hours late. If you make multiple submissions,
I will typically grade the latest submission. If you wish me to grade a different submission, you must indicate
this before the 72-hour late period is over.&lt;/p&gt;

&lt;p&gt;Regardless of the above policy, I reserve the right to refuse to grade any projects submitted after the beginning
of the second class period following the project deadline, because I may discuss the solution in class.&lt;/p&gt;

&lt;p&gt;Project extensions will not necessarily be granted due to server congestion, system problems, network problems,
power outages, etc., so do not wait to submit a project until the night it is due. No consideration in grading
will be made for errors made in transferring files or submitting the wrong version of your project. Having a working,
non-submitted version will not count; only submitted code will be be counted.&lt;/p&gt;

&lt;p&gt;You are responsible for developing your own techniques for testing your projects before submitting it.
&lt;strong&gt;I will grade your projects based on test cases not provided to you in advance.&lt;/strong&gt;
Because grading may be done automatically, you must follow the project specification exactly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your code will be graded on a combination of correctness, completeness, documentation, and code style.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Any &amp;ldquo;hard coding&amp;rdquo; in a project assignment will result in a score of zero for that project, and is considered a bad-faith effort.&lt;/strong&gt;
Hard coding refers to attempting to make a program appear as if it works correctly, when in fact it does not.
One example of hard coding would be printing the desired output instead of computing it.
If you have any questions as to what constitutes hard coding for a particular assignment, be sure to ask ahead of time.&lt;/p&gt;

&lt;h2 id=&#34;adding-and-dropping-the-course:78d0232414fd482d847cc5d8c36d4578&#34;&gt;Adding and Dropping the Course&lt;/h2&gt;

&lt;p&gt;Students are responsible for adding and dropping courses. Please consult the appropriate academic calendar for the exact deadlines.
I will not give &amp;ldquo;WP&amp;rdquo; or &amp;ldquo;WF&amp;rdquo; grades to students requesting a drop after the deadline except in extraordinary circumstances.&lt;/p&gt;

&lt;h2 id=&#34;academic-honesty:78d0232414fd482d847cc5d8c36d4578&#34;&gt;Academic Honesty&lt;/h2&gt;

&lt;p&gt;You are expected to comply with the JMU Honor Code as stated in the Student Handbook and available from the
Honor Council website on all assignments, projects, and exams.&lt;/p&gt;

&lt;p&gt;Consulting with other students about problems and solutions is not necessarily a violation of the honor code,
depending on the particular assignment. All final work turned in for an assignment must be your own unless it
is a group project. In particular, you may not share source or binary code on programming assignments unless
the project specification explicitly allows it. If you are in doubt about whether something is an honor code
violation, please contact me immediately.&lt;/p&gt;

&lt;p&gt;If I find evidence of a violation of the honor code, I will bring the matter to the attention of the involved
individuals via email and request a face-to-face meeting. As per section IV of the honor code, first time student
offenders may agree that a violation has occurred and accept an appropriate penalty by submitting an
&amp;ldquo;Informal Resolution Agreement Form&amp;rdquo; to the honor council. If the student is not a first-time offender or if there
is disagreement about the violation or penalty, the matter will be refered to the honor council under section
V of the honor code.&lt;/p&gt;

&lt;h2 id=&#34;disability-accommodations:78d0232414fd482d847cc5d8c36d4578&#34;&gt;Disability Accommodations&lt;/h2&gt;

&lt;p&gt;If you need an accommodation based on the impact of a disability, you must contact the Office of Disability
Services if you have not previously done so. Disability Services will provide you with an Access Plan letter
that will verify your need for services and make recommendations for accommodations to be used in the classroom.
Once you have shown me this letter, we will sit down and review the course requirements, your disability
characteristics, and your requested accommodations to develop an individualized plan appropriate for this course.
I will not make any accommodations without the appropriate documentation, as I am not qualified to diagnose disabilities.&lt;/p&gt;

&lt;h2 id=&#34;excused-absences:78d0232414fd482d847cc5d8c36d4578&#34;&gt;Excused Absences&lt;/h2&gt;

&lt;p&gt;Besides the policies in this syllabus, the University&amp;rsquo;s policies apply during the semester. Various policies that
may be relevant appear in the Undergraduate Catalog.&lt;/p&gt;

&lt;p&gt;Excused absences will be granted at my discretion and only with appropriate documentation. Please contact me as soon
as possible if you wish to request an excused absence.&lt;/p&gt;

&lt;p&gt;Missing an exam for reasons such as illness, religious observance, participation in required university activities,
or family or personal emergency (such as a serious automobile accident or the funeral of a close relative) all are
circumstances that may qualify as an excused absence. However, you must provide documentation that the absence qualifies
as excused. I will arrange a makeup exam or substitute assignment at my discretion.&lt;/p&gt;

&lt;p&gt;The policies for excused absences do not apply to in-class activities (which cannot be made up) or project assignments.
Projects will be assigned with sufficient time to be completed by students who have a reasonable understanding of the
necessary material and begin promptly. In cases of extremely serious documented illness of lengthy duration or other
protracted, severe emergency situations, I may consider extensions on project assignments depending upon the specific
circumstances. Please contact me as early as possible if you believe you will need such an extension.&lt;/p&gt;

&lt;h2 id=&#34;inclement-weather:78d0232414fd482d847cc5d8c36d4578&#34;&gt;Inclement Weather&lt;/h2&gt;

&lt;p&gt;This class will operate in accord with JMU&amp;rsquo;s official cancellation policy.&lt;/p&gt;

&lt;h2 id=&#34;catalog-description:78d0232414fd482d847cc5d8c36d4578&#34;&gt;Catalog Description&lt;/h2&gt;

&lt;p&gt;Students learn to implement and analyze elementary data structures and the basic complexity classes of algorithms that use strategies such as greedy algorithms, divide-and-conquer algorithms and backtracking algorithms. This analysis is especially applied to problems in searching, sorting and parsing.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Prerequisites: Grades of &amp;ldquo;C-&amp;rdquo; or better in CS/MATH 227, MATH 231 or equivalent and either CS 159 or CS 239.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Section: 0003&lt;br&gt;
Instructor: &lt;a href=&#34;../../../&#34;&gt;Dr. John Bowers&lt;/a&gt;&lt;br&gt;
Credits: 3&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Calendar</title>
      <link>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240calendar/</link>
      <pubDate>Thu, 20 Aug 2015 11:13:15 -0400</pubDate>
      
      <guid>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240calendar/</guid>
      <description>

&lt;p&gt;&lt;em&gt;Note: Calendar is always subject to change. Please check regularly for updates.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Unless explicitly marked, all class periods will be held in ISAT 243 and labs will be held
in ISAT 248. Unless noted otherwise, all readings are from the class textbook. Unless noted
otherwise, all quizzes will be given on &lt;a href=&#34;https://canvas.jmu.edu&#34;&gt;Canvas&lt;/a&gt;, and all homeworks
(HWs) and programming assignments (PAs) will be posted on the &lt;a href=&#34;../cs240assignments&#34;&gt;assignments page&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&#34;calendar:b5c21f64cb6998adf78c0b553866b6b0&#34;&gt;Calendar&lt;/h2&gt;

&lt;table id=&#34;calendar&#34;&gt;

&lt;tbody&gt;&lt;tr style=&#34;height:50px&#34;&gt; &lt;th colspan=&#34;7&#34;&gt;August&lt;/th&gt; &lt;/tr&gt;
&lt;tr&gt; &lt;th&gt;Su&lt;/th&gt; &lt;th&gt;M&lt;/th&gt; &lt;th&gt;Tu&lt;/th&gt; &lt;th&gt;W&lt;/th&gt;
     &lt;th&gt;Th&lt;/th&gt; &lt;th&gt;F&lt;/th&gt; &lt;th&gt;Sa&lt;/th&gt; &lt;/tr&gt;

&lt;!-- WEEK 1  --&gt;
&lt;tr&gt;
  &lt;td&gt;30
  &lt;/td&gt;
  &lt;td&gt;31
    &lt;br&gt;Intro
    &lt;br&gt;&lt;span class=&#34;quiz&#34;&gt;Reading: Ch. 1 (through 1.2)&lt;/span&gt;
    &lt;br&gt; [&lt;a href=&#34;../../../files/lec01.pdf&#34;&gt;slides&lt;/a&gt;]
  &lt;/td&gt;
  &lt;th&gt;
  &lt;/th&gt;&lt;th&gt;
  &lt;/th&gt;&lt;th&gt;
  &lt;/th&gt;&lt;th&gt;
  &lt;/th&gt;&lt;th&gt;
&lt;/th&gt;&lt;/tr&gt;

&lt;tr style=&#34;height:50px&#34;&gt; &lt;th colspan=&#34;7&#34;&gt;September&lt;/th&gt; &lt;/tr&gt;
&lt;tr&gt; &lt;th&gt;Su&lt;/th&gt; &lt;th&gt;M&lt;/th&gt; &lt;th&gt;Tu&lt;/th&gt; &lt;th&gt;W&lt;/th&gt;
     &lt;th&gt;Th&lt;/th&gt; &lt;th&gt;F&lt;/th&gt; &lt;th&gt;Sa&lt;/th&gt; &lt;/tr&gt;

&lt;!-- WEEK 1 (continued) --&gt;
&lt;tr&gt;
  &lt;th&gt;
  &lt;/th&gt;&lt;th&gt;
  &lt;/th&gt;&lt;td&gt;1
  &lt;/td&gt;
  &lt;td&gt;2
    &lt;br&gt;&lt;a href=&#34;../cs240/lab01&#34;&gt;Lab: GCC and Make&lt;/a&gt;
    &lt;br&gt;
    &lt;br&gt;&lt;span class=&#34;ext&#34;&gt;UUG Linux InstallFest (7pm in ISAT/CS 259)&lt;/span&gt;
  &lt;/td&gt;
  &lt;td&gt;3
  &lt;/td&gt;
  &lt;td&gt;4
    &lt;br&gt;Lab: Basic C
    &lt;br&gt;
    &lt;br&gt;&lt;span class=&#34;ext&#34;&gt;ACM Competitive Programming Club (2:30pm every Friday in ISAT/CS 143)&lt;/span&gt;
  &lt;/td&gt;
  &lt;td&gt;5
  &lt;/td&gt;
&lt;/tr&gt;

&lt;!-- WEEK 2  --&gt;
&lt;tr&gt;
  &lt;td&gt;6
  &lt;/td&gt;
  &lt;td&gt;7
    &lt;br&gt;Lab: Arrays &amp;amp; Strings
  &lt;/td&gt;
  &lt;td&gt;8
      &lt;br&gt;&lt;span class=&#34;ext&#34;&gt;Deadline to drop class&lt;/span&gt;
      &lt;br&gt;
      &lt;br&gt;&lt;span class=&#34;ext&#34;&gt;UUG Linux InstallFest (7pm in ISAT/CS 259)&lt;/span&gt;
  &lt;/td&gt;
  &lt;td&gt;9
    &lt;br&gt;Lab: Structs
  &lt;/td&gt;
  &lt;td&gt;10
  &lt;/td&gt;
  &lt;td&gt;11
    &lt;br&gt;Lab: Testing
    &lt;br&gt;&lt;span class=&#34;pa&#34;&gt;PA 0 due&lt;/span&gt;
  &lt;/td&gt;
  &lt;td&gt;12
  &lt;/td&gt;
&lt;/tr&gt;

&lt;!-- WEEK 3  --&gt;
&lt;tr&gt;
  &lt;td&gt;13
  &lt;/td&gt;
  &lt;td&gt;14
    &lt;br&gt;Lab: Pointers
  &lt;/td&gt;
  &lt;td&gt;15
  &lt;/td&gt;
  &lt;td&gt;16
    &lt;br&gt;Lab: Debugging
  &lt;/td&gt;
  &lt;td&gt;17
  &lt;/td&gt;
  &lt;td&gt;18
    &lt;br&gt;Algorithm Analysis
    &lt;br&gt;&lt;span class=&#34;quiz&#34;&gt;Reading: 1.3-1.5&lt;/span&gt;
    &lt;br&gt;&lt;span class=&#34;hw&#34;&gt;HW 1 due&lt;/span&gt;
  &lt;/td&gt;
  &lt;td&gt;19
  &lt;/td&gt;
&lt;/tr&gt;

&lt;!-- WEEK 4  --&gt;
&lt;tr&gt;
  &lt;td&gt;20
  &lt;/td&gt;
  &lt;td&gt;21
    &lt;br&gt;Algorithm Analysis
  &lt;/td&gt;
  &lt;td&gt;22
  &lt;/td&gt;
  &lt;td&gt;23
    &lt;br&gt;Dynamic Arrays
  &lt;/td&gt;
  &lt;td&gt;24
  &lt;/td&gt;
  &lt;td&gt;25
    &lt;br&gt;Lab: Dynamic Arrays
    &lt;br&gt;&lt;span class=&#34;pa&#34;&gt;PA 1 due&lt;/span&gt;
  &lt;/td&gt;
  &lt;td&gt;26
  &lt;/td&gt;
&lt;/tr&gt;

&lt;!-- WEEK 5  --&gt;
&lt;tr&gt;
  &lt;td&gt;27
  &lt;/td&gt;
  &lt;td&gt;28
    &lt;br&gt;Stacks &amp;amp; Queues
  &lt;/td&gt;
  &lt;td&gt;29
  &lt;/td&gt;
  &lt;td&gt;30
    &lt;br&gt;Lab: Stacks &amp;amp; Queues
  &lt;/td&gt;
  &lt;th&gt;
  &lt;/th&gt;&lt;th&gt;
  &lt;/th&gt;&lt;th&gt;
&lt;/th&gt;&lt;/tr&gt;

&lt;tr style=&#34;height:50px&#34;&gt; &lt;th colspan=&#34;7&#34;&gt;October&lt;/th&gt; &lt;/tr&gt;
&lt;tr&gt; &lt;th&gt;Su&lt;/th&gt; &lt;th&gt;M&lt;/th&gt; &lt;th&gt;Tu&lt;/th&gt; &lt;th&gt;W&lt;/th&gt;
     &lt;th&gt;Th&lt;/th&gt; &lt;th&gt;F&lt;/th&gt; &lt;th&gt;Sa&lt;/th&gt; &lt;/tr&gt;

&lt;!-- WEEK 5 (continued) --&gt;
&lt;tr&gt;
  &lt;th&gt;
  &lt;/th&gt;&lt;th&gt;
  &lt;/th&gt;&lt;th&gt;
  &lt;/th&gt;&lt;th&gt;
  &lt;/th&gt;&lt;td&gt;1
  &lt;/td&gt;
  &lt;td&gt;2
    &lt;br&gt;Linked Lists
    &lt;br&gt;&lt;span class=&#34;hw&#34;&gt;HW 2 due&lt;/span&gt;
  &lt;/td&gt;
  &lt;td&gt;3
  &lt;/td&gt;
&lt;/tr&gt;

&lt;!-- WEEK 6  --&gt;
&lt;tr&gt;
  &lt;td&gt;4
  &lt;/td&gt;
  &lt;td&gt;5
    &lt;br&gt;Lab: Linked Lists
  &lt;/td&gt;
  &lt;td&gt;6
  &lt;/td&gt;
  &lt;td&gt;7
    &lt;br&gt;Adv. Linked Lists
  &lt;/td&gt;
  &lt;td&gt;8
  &lt;/td&gt;
  &lt;td&gt;9
    &lt;br&gt;&lt;span class=&#34;pa&#34;&gt;MIDTERM 1&lt;/span&gt;
  &lt;/td&gt;
  &lt;td&gt;10
  &lt;/td&gt;
&lt;/tr&gt;

&lt;!-- WEEK 7  --&gt;
&lt;tr&gt;
  &lt;td&gt;11
  &lt;/td&gt;
  &lt;td&gt;12
    &lt;br&gt;Lab: Linked Sorted Set
  &lt;/td&gt;
  &lt;td&gt;13
  &lt;/td&gt;
  &lt;td&gt;14
    &lt;br&gt;Skip Lists
    &lt;br&gt;&lt;span class=&#34;quiz&#34;&gt;Reading Ch. 4&lt;/span&gt;
    &lt;br&gt;&lt;br&gt;&lt;span class=&#34;ext&#34;&gt;UUG Vim Tutorial (7:30pm in HHS 2207)&lt;/span&gt;
  &lt;/td&gt;
  &lt;td&gt;15
  &lt;/td&gt;
  &lt;td&gt;16
    &lt;br&gt;Binary Trees
    &lt;br&gt;&lt;span class=&#34;pa&#34;&gt;PA 2 due&lt;/span&gt;
  &lt;/td&gt;
  &lt;td&gt;17
  &lt;/td&gt;
&lt;/tr&gt;

&lt;!-- WEEK 8  --&gt;
&lt;tr&gt;
  &lt;td&gt;18
  &lt;/td&gt;
  &lt;td&gt;19
    &lt;br&gt;Lab: Binary Tree Traversal
  &lt;/td&gt;
  &lt;td&gt;20
  &lt;/td&gt;
  &lt;td&gt;21
    &lt;br&gt;AVL Trees
    &lt;br&gt;&lt;span class=&#34;quiz&#34;&gt;Reading: avl_trees.pdf on Canvas&lt;/span&gt;
  &lt;/td&gt;
  &lt;td&gt;22
  &lt;/td&gt;
  &lt;td&gt;23
    &lt;br&gt;Recursion
    &lt;br&gt;&lt;span class=&#34;quiz&#34;&gt;Reading: recursion.pdf on Canvas&lt;/span&gt;
    &lt;br&gt;&lt;span class=&#34;hw&#34;&gt;HW 3 due&lt;/span&gt;
  &lt;/td&gt;
  &lt;td&gt;24
  &lt;/td&gt;
&lt;/tr&gt;

&lt;!-- WEEK 9  --&gt;
&lt;tr&gt;
  &lt;td&gt;25
  &lt;/td&gt;
  &lt;td&gt;26
    &lt;br&gt;Recurrences
  &lt;/td&gt;
  &lt;td&gt;27
  &lt;/td&gt;
  &lt;td&gt;28
    &lt;br&gt;Recurrences
  &lt;/td&gt;
  &lt;td&gt;29
      &lt;br&gt;&lt;span class=&#34;ext&#34;&gt;Deadline to withdraw with &#34;W&#34;&lt;/span&gt;
  &lt;/td&gt;
  &lt;td&gt;30
    &lt;br&gt;MORE RECURRENCES? SRLSY. Oh, and Tail Recursion.
    &lt;br&gt;&lt;span class=&#34;pa&#34;&gt;PA 3 due&lt;/span&gt;
  &lt;/td&gt;
  &lt;td&gt;31
  &lt;/td&gt;
&lt;/tr&gt;

&lt;tr style=&#34;height:50px&#34;&gt; &lt;th colspan=&#34;7&#34;&gt;November&lt;/th&gt; &lt;/tr&gt;
&lt;tr&gt; &lt;th&gt;Su&lt;/th&gt; &lt;th&gt;M&lt;/th&gt; &lt;th&gt;Tu&lt;/th&gt; &lt;th&gt;W&lt;/th&gt;
     &lt;th&gt;Th&lt;/th&gt; &lt;th&gt;F&lt;/th&gt; &lt;th&gt;Sa&lt;/th&gt; &lt;/tr&gt;

&lt;!-- WEEK 10  --&gt;
&lt;tr&gt;
  &lt;td&gt;1
  &lt;/td&gt;
  &lt;td&gt;2
    &lt;br&gt;Basic Sorts and Merge Sort
  &lt;/td&gt;
  &lt;td&gt;3
  &lt;/td&gt;
  &lt;td&gt;4
    &lt;br&gt;Quicksort
  &lt;/td&gt;
  &lt;td&gt;5
  &lt;/td&gt;
  &lt;td&gt;6
    &lt;br&gt;Lab - Sorting
  &lt;/td&gt;
  &lt;td&gt;7
  &lt;/td&gt;
&lt;/tr&gt;

&lt;!-- WEEK 11  --&gt;
&lt;tr&gt;
  &lt;td&gt;8
  &lt;/td&gt;
  &lt;td&gt;9
    &lt;br&gt;Lab - Sorting (contd.)
  &lt;/td&gt;
  &lt;td&gt;10
  &lt;/td&gt;
  &lt;td&gt;11
    &lt;br&gt;Priority Queues and Heaps
  &lt;/td&gt;
  &lt;td&gt;12
  &lt;/td&gt;
  &lt;td&gt;13
    &lt;br&gt;Heap sort
  &lt;/td&gt;
  &lt;td&gt;14
  &lt;/td&gt;
&lt;/tr&gt;

&lt;!-- WEEK 12  --&gt;
&lt;tr&gt;
  &lt;td&gt;15
  &lt;/td&gt;
  &lt;td&gt;16
    &lt;br&gt;Lab - Heap sort
  &lt;/td&gt;
  &lt;td&gt;17
  &lt;/td&gt;
  &lt;td&gt;18
    &lt;br&gt;Midterm Review
  &lt;/td&gt;
  &lt;td&gt;19
  &lt;/td&gt;
  &lt;td&gt;20
    &lt;br&gt;&lt;span class=&#34;pa&#34;&gt;MIDTERM 2&lt;/span&gt;
  &lt;/td&gt;
  &lt;td&gt;21
  &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
  &lt;td&gt;22
  &lt;/td&gt;
  &lt;td colspan=&#34;5&#34;&gt;
    23-27
    &lt;br&gt;&lt;strong&gt;&lt;span class=&#34;ext&#34;&gt;Thanksgiving Break&lt;/span&gt;&lt;/strong&gt;
  &lt;/td&gt;
  &lt;td&gt;28
  &lt;/td&gt;
&lt;/tr&gt;

&lt;!-- WEEK 13 --&gt;
&lt;tr&gt;
  &lt;td&gt;29
  &lt;/td&gt;
  &lt;td&gt;30
    &lt;br&gt;Maps and Hashing
  &lt;/td&gt;
  &lt;th&gt;
  &lt;/th&gt;&lt;th&gt;
  &lt;/th&gt;&lt;th&gt;
  &lt;/th&gt;&lt;th&gt;
  &lt;/th&gt;&lt;th&gt;
&lt;/th&gt;&lt;/tr&gt;

&lt;tr style=&#34;height:50px&#34;&gt; &lt;th colspan=&#34;7&#34;&gt;December&lt;/th&gt; &lt;/tr&gt;
&lt;tr&gt; &lt;th&gt;Su&lt;/th&gt; &lt;th&gt;M&lt;/th&gt; &lt;th&gt;Tu&lt;/th&gt; &lt;th&gt;W&lt;/th&gt;
     &lt;th&gt;Th&lt;/th&gt; &lt;th&gt;F&lt;/th&gt; &lt;th&gt;Sa&lt;/th&gt; &lt;/tr&gt;

&lt;!-- WEEK 13 (continued) --&gt;
&lt;tr&gt;
  &lt;th&gt;
  &lt;/th&gt;&lt;th&gt;
  &lt;/th&gt;&lt;td&gt;1
  &lt;/td&gt;
  &lt;td&gt;2
    &lt;br&gt;Hasing Activity
  &lt;td&gt;3
  &lt;/td&gt;
  &lt;td&gt;4
    &lt;br&gt;Lab - Hashing
  &lt;/td&gt;
  &lt;td&gt;5
  &lt;/td&gt;
&lt;/tr&gt;

&lt;!-- WEEK 14  --&gt;
&lt;tr&gt;
  &lt;td&gt;6
  &lt;/td&gt;
  &lt;td&gt;7
    &lt;br&gt;Applications
  &lt;/td&gt;
  &lt;td&gt;8
  &lt;/td&gt;
  &lt;td&gt;9
    &lt;br&gt;Applications
  &lt;/td&gt;
  &lt;td&gt;10
  &lt;/td&gt;
  &lt;td&gt;11
    &lt;br&gt;Final Exam Review
  &lt;/td&gt;
  &lt;td&gt;12
  &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
  &lt;td&gt;13
  &lt;/td&gt;
  &lt;td colspan=&#34;5&#34;&gt;
    14-18
    &lt;br&gt;&lt;strong&gt;&lt;span class=&#34;pa&#34;&gt;Final Exams&lt;/span&gt;&lt;/strong&gt;
    &lt;br&gt;Section 1: Mon 8:00-10:00 (8:00am-10:00pm)
    &lt;br&gt;Section 2: Wed 8:00-10:00 (8:00am-10:00pm)
  &lt;/td&gt;
&lt;/tr&gt;

&lt;/tbody&gt;&lt;/table&gt;
</description>
    </item>
    
    <item>
      <title>Assignments</title>
      <link>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240assignments/</link>
      <pubDate>Thu, 20 Aug 2015 11:13:54 -0400</pubDate>
      
      <guid>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240assignments/</guid>
      <description>

&lt;h2 id=&#34;homeworks:ad4ea8fda2c7f23037fe1824c6178b62&#34;&gt;Homeworks&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;HW1: Basic C (details TBD)&lt;/li&gt;
&lt;li&gt;HW2: Big-O Notation (details TBD)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;programming-assignments:ad4ea8fda2c7f23037fe1824c6178b62&#34;&gt;Programming Assignments&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;../cs240/pa0&#34;&gt;PA0: Adding Numbers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PA1: Turtle Geometry (details TBD)&lt;/li&gt;
&lt;/ul&gt;
</description>
    </item>
    
    <item>
      <title>Resources</title>
      <link>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240resources/</link>
      <pubDate>Thu, 20 Aug 2015 11:13:58 -0400</pubDate>
      
      <guid>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240resources/</guid>
      <description>

&lt;p&gt;This page contains links to third-party resources that you may find
helpful. If you have suggestions for other resources that your fellow students
may find helpful, please let me know.&lt;/p&gt;

&lt;h2 id=&#34;installing-a-c-compiler:69b2560281225fb76b0be36a6313d11c&#34;&gt;Installing a C Compiler&lt;/h2&gt;

&lt;p&gt;&lt;a href=&#34;http://www.gnu.org/gnu/why-gnu-linux.en.html&#34;&gt;GNU/Linux&lt;/a&gt; is the
preferred operating system for this class. The &lt;a href=&#34;https://gcc.gnu.org/&#34;&gt;GNU C compiler&lt;/a&gt; is an integral part of a
GNU/Linux operating system and is easy to install and use in such an
environment. Our lab runs a specific GNU/Linux distribution called &lt;a href=&#34;http://www.linuxmint.com/&#34;&gt;Mint&lt;/a&gt;. If you do not already have access to
a C compiler on your personal computer, you may wish to set one up using one of
the following options depending on your operating system:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ubuntu or Mint Linux (or any other Debian-based distribution): Run &lt;code&gt;sudo apt-get install build-essential&lt;/code&gt; in a terminal as an admin user.&lt;/li&gt;
&lt;li&gt;Fedora or other RPM-based distribution: Run &lt;code&gt;yum groupinstall &amp;quot;Development tools&amp;quot;&lt;/code&gt; in a terminal as an admin user.&lt;/li&gt;
&lt;li&gt;Mac OS X: You will need to install the Xcode Command Line Tools package. &lt;a href=&#34;http://osxdaily.com/2014/02/12/install-command-line-tools-mac-os-x/&#34;&gt;Here&lt;/a&gt; is one guide.&lt;/li&gt;
&lt;li&gt;Native Windows: You have two main options, &lt;a href=&#34;https://www.cygwin.com/&#34;&gt;Cygwin&lt;/a&gt; or &lt;a href=&#34;http://www.mingw.org/&#34;&gt;MinGW&lt;/a&gt;. The former is a larger system that includes a variety of tools to create a development environment similar to Linux on Windows. The later is a smaller system that focuses more closely on providing compilers.&lt;/li&gt;
&lt;li&gt;Any platform: You can use &lt;a href=&#34;https://www.virtualbox.org/wiki/Downloads&#34;&gt;VirtualBox&lt;/a&gt; to create a &lt;em&gt;virtual machine&lt;/em&gt; on your system that runs GNU/Linux. &lt;a href=&#34;http://www.everydaylinuxuser.com/2014/05/how-to-install-linux-mint-as-virtual.html&#34;&gt;Here&lt;/a&gt; is a tutorial; you should also consider attending the Unix Users&amp;rsquo; Group tutorial at 7:00pm on Wednesday, September 2, in ISAT/CS 259.&lt;/li&gt;
&lt;li&gt;SSH server: You can develop C programs on &lt;code&gt;stu.cs.jmu.edu&lt;/code&gt; (a departmental server) or &lt;code&gt;educ.jmu.edu&lt;/code&gt; (a university server). You will need an SSH client (the &lt;code&gt;ssh&lt;/code&gt; command-line utility is installed by default in GNU/Linux or Mac OS X; try &lt;a href=&#34;http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html&#34;&gt;PuTTY&lt;/a&gt; on Windows). To access Educ, you&amp;rsquo;ll first need to &lt;a href=&#34;https://remedy.jmu.edu/educ.asp&#34;&gt;request an account&lt;/a&gt;. If you are a CS student, you should already have an account on stu.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;learning-c:69b2560281225fb76b0be36a6313d11c&#34;&gt;Learning C&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;http://www.physics.drexel.edu/courses/Comp_Phys/General/C_basics/c_tutorial.html&#34;&gt;C Language Tutorial&lt;/a&gt; - Basic tutorial from Drexel University&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://www.cprogramming.com/tutorial/c-tutorial.html&#34;&gt;C Tutorial&lt;/a&gt; - Another basic tutorial&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://c.learncodethehardway.org/book/&#34;&gt;Learn C The Hard Way&lt;/a&gt; - Yet another (rather humorous) tutorial&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://www.cs.cf.ac.uk/Dave/C/CE.html&#34;&gt;Programming in C&lt;/a&gt; - Reference document (also covers threads and other systems topics)&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://www.cplusplus.com/reference/cstdio/printf/&#34;&gt;printf reference&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://ideone.com&#34;&gt;ideone.com&lt;/a&gt; - Web-based online compiler and IDE (make sure you choose &amp;ldquo;C&amp;rdquo; as your language!)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;LaTeX&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;http://www.latex-project.org&#34;&gt;LaTeX-Project.org&lt;/a&gt; - LaTeX home page&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://www.sharelatex.com&#34;&gt;ShareLaTeX&lt;/a&gt; - Web-based LaTeX collaborative editor&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://www.xm1math.net/texmaker/&#34;&gt;Texmaker&lt;/a&gt; - Cross-platform LaTeX editor&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://www.lyx.org&#34;&gt;Lyx&lt;/a&gt; - Graphical semi-WYSIWYG LaTeX editor&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Miscellaneous&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;http://openclipart.org&#34;&gt;OpenClipArt&lt;/a&gt; - Public domain images (clipart on this website comes from here)&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://www.wolframalpha.com&#34;&gt;Wolfram Alpha&lt;/a&gt; - General purpose mathematical engine&lt;/li&gt;
&lt;/ul&gt;
</description>
    </item>
    
    <item>
      <title>Lab 01</title>
      <link>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240/lab01/</link>
      <pubDate>Tue, 01 Sep 2015 10:55:33 -0400</pubDate>
      
      <guid>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240/lab01/</guid>
      <description>

&lt;h1 id=&#34;using-c-and-make:72892675bcb09366bedb5a4fc80ce987&#34;&gt;Using C and Make&lt;/h1&gt;

&lt;p&gt;The goal of this lab is to introduce you to the main tools you will use throughout
this course to produce executables from C source files.&lt;/p&gt;

&lt;h2 id=&#34;learning-objectives:72892675bcb09366bedb5a4fc80ce987&#34;&gt;Learning Objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lab, you should be able to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Interact with the command line using a terminal.&lt;/li&gt;
&lt;li&gt;Write a simple &amp;ldquo;Hello, World&amp;rdquo; application in C.&lt;/li&gt;
&lt;li&gt;Compile a C source file using gcc.&lt;/li&gt;
&lt;li&gt;Understand the difference between:

&lt;ul&gt;
&lt;li&gt;header files and source files,&lt;/li&gt;
&lt;li&gt;object files and executables,&lt;/li&gt;
&lt;li&gt;compiling and linking,&lt;/li&gt;
&lt;li&gt;declarations and definitions.&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;Write a simple makefile to help automate compilation.&lt;/li&gt;
&lt;li&gt;Understand how the course&amp;rsquo;s project makefiles work and how to edit them.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;launching-and-using-the-command-line:72892675bcb09366bedb5a4fc80ce987&#34;&gt;Launching and Using the Command Line&lt;/h2&gt;

&lt;p&gt;All steps of this lab will be done using the command line in a terminal window.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS IN THE LINUX LAB (ISAT 250):&lt;/strong&gt; Press ALT-F2, type in
&amp;ldquo;&lt;code&gt;gnome-terminal&lt;/code&gt;&amp;rdquo; and press ENTER. Alternatively, just click this button at the
bottom of the screen:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;../../../../images/gnome_terminal.png&#34; alt=&#34;Gnome Terminal&#34; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS IN THE MAC LAB (ISAT 248):&lt;/strong&gt; Press COMMAND-SPACE, type in &amp;ldquo;&lt;code&gt;terminal&lt;/code&gt;&amp;ldquo;
and press ENTER.&lt;/p&gt;

&lt;p&gt;The terminal functions as an intermediary between you and the computer system,
allowing you to have a &amp;ldquo;conversation&amp;rdquo; of sorts with the programs on the system.
Using the terminal, you will execute commands by typing them and pressing ENTER,
and the system will respond with any output in the same window. Try this now:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Execute the following line in your terminal window:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# date
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt; What is the output? If you run the command again, does the output change?&lt;/p&gt;

&lt;p&gt;Here is a quick reference to common terminal commands on Unix-based systems
(e.g., Ubuntu/Mint Linux or Mac OS X):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;pwd                     &amp;quot;print working directory;&amp;quot; essentially &amp;quot;where am I?&amp;quot;
ls                      list the files in the current working directory
cd &amp;lt;dir&amp;gt;                &amp;quot;change directory&amp;quot; to the given directory name
cd ..                   change to the parent of the current directory
cd                      change to your home directory
cat &amp;lt;file&amp;gt;              &amp;quot;concatenate&amp;quot; a file; essentially &amp;quot;print this file to the screen&amp;quot;
cp &amp;lt;src&amp;gt; &amp;lt;dest&amp;gt;         copy a file from &amp;quot;src&amp;quot; to &amp;quot;dest&amp;quot;
mv &amp;lt;src&amp;gt; &amp;lt;dest&amp;gt;         move a file from &amp;quot;src&amp;quot; to &amp;quot;dest&amp;quot;; essentially &amp;quot;rename a file&amp;quot;
rm &amp;lt;file&amp;gt;               remove a file
mkdir &amp;lt;dir&amp;gt;             create a new directory
rmdir &amp;lt;dir&amp;gt;             remove a directory (it must be empty first!)
man &amp;lt;command&amp;gt;           read the &amp;quot;manual&amp;quot; page for the given command
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you are interested in becoming more skilled with command-line interaction,
you could work through &lt;a href=&#34;http://linuxcommand.org/lc3_learning_the_shell.php&#34;&gt;this
tutorial&lt;/a&gt; or &lt;a href=&#34;https://www.codecademy.com/en/courses/learn-the-command-line/&#34;&gt;this online
class&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&#34;you-probably-knew-it-was-coming-hello-world:72892675bcb09366bedb5a4fc80ce987&#34;&gt;You-Probably-Knew-It-Was-Coming: Hello World&lt;/h2&gt;

&lt;p&gt;Open a text editor to create a new C file called &lt;code&gt;hello.c&lt;/code&gt; in nano by executing
the following line in a terminal window:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# nano hello.c
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Type the following program, save your code (CTRL+O then ENTER) and exit nano (CTRL+X).&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;#include &amp;lt;stdio.h&amp;gt;

int main()
{
    puts(&amp;quot;Hello world!&amp;quot;);
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Congratulations! You have typed your first C program. For reference, this is
essentially the same as the following Java program:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-java&#34;&gt;public class Hello
{
    public static void main(String[] args)
    {
        System.out.println(&amp;quot;Hello world!&amp;quot;);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Take a moment to note some of the similaries and differences betweeen the Java
version and the C version.&lt;/p&gt;

&lt;p&gt;Now let&amp;rsquo;s compile your &lt;code&gt;hello.c&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; From the command line:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# gcc -o hello hello.c
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you typed the code exactly as it appears above then it should compile. This
replaces the command you would have used in Java: (&amp;rdquo;&lt;code&gt;javac Hello.java&lt;/code&gt;&amp;rdquo;). The
command above has several parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The program we are invoking (&amp;rdquo;&lt;code&gt;gcc&lt;/code&gt;&amp;rdquo;, the GNU C Compiler)&lt;/li&gt;
&lt;li&gt;The program options (&amp;rdquo;&lt;code&gt;-o hello&lt;/code&gt;&amp;rdquo;, which tells the compiler to produce a file
called &lt;code&gt;hello&lt;/code&gt; as output)&lt;/li&gt;
&lt;li&gt;The file to compile (&amp;rdquo;&lt;code&gt;hello.c&lt;/code&gt;&amp;rdquo;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let&amp;rsquo;s see what this produced.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; List the contents of the current folder:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# ls
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If all has gone according to plan you should now have a file called &lt;code&gt;hello&lt;/code&gt;.
Furthermore, this file is executable, and can be run via:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# ./hello
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;At this point the immortal words &lt;code&gt;Hello world!&lt;/code&gt; should appear on your terminal. Success!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt; What happens if you don&amp;rsquo;t compile with the &lt;code&gt;-o hello&lt;/code&gt; flag? (try it and see!)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt; Can you get &lt;code&gt;gcc&lt;/code&gt; to compile hello.c as a binary called &lt;code&gt;hw&lt;/code&gt; instead of &lt;code&gt;hello&lt;/code&gt;?&lt;/p&gt;

&lt;h2 id=&#34;makefiles:72892675bcb09366bedb5a4fc80ce987&#34;&gt;Makefiles&lt;/h2&gt;

&lt;p&gt;When you are compiling one file like we did above (&lt;code&gt;gcc -o hello hello.c&lt;/code&gt;), it
may not feel that tedious, but most programming projects require that you
compile more than one file. We&amp;rsquo;ll see later exactly how this works, but for now
let&amp;rsquo;s assume that each source file (called a &lt;em&gt;module&lt;/em&gt;) is compiled independently
into an intermediate file called an &lt;em&gt;object file&lt;/em&gt; and then all the object files
are combined into a single &lt;em&gt;executable file&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This is somewhat similar to the Java process of compiling a bunch of &lt;code&gt;.java&lt;/code&gt;
files into &lt;code&gt;.class&lt;/code&gt; files, and then combining them all into a single &lt;code&gt;.jar&lt;/code&gt;
file. In earlier classes, you probably used an &lt;em&gt;integrated development
environment&lt;/em&gt; (IDE) such as JGrasp or Eclipse to manage the compilation process.
While IDEs exist for the C language, the C build process is simple enough that
we do not need one in this course.&lt;/p&gt;

&lt;p&gt;To more easily handle compiling large projects we will use a tool called &lt;code&gt;make&lt;/code&gt;.
The purpose of &lt;code&gt;make&lt;/code&gt; is to automate the process of compiling and recompiling
multiple files.  To use &lt;code&gt;make&lt;/code&gt; we need to create a special file, called a
&lt;em&gt;makefile&lt;/em&gt;, which tells &lt;code&gt;make&lt;/code&gt; how to make our program. Do:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# nano Makefile
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note the capitalization of the filename. It is necessary to capitalize the
filename, but it is customary not to capitalize it in other contexts.&lt;/p&gt;

&lt;p&gt;Type in the following simple makefile:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;helloworld: hello.c
    gcc -o helloworld hello.c
clean:
    rm -f helloworld
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Pitfall Alert:&lt;/strong&gt; In makefiles, whitespace is very important! The first and third
lines of the above example should have no space before &lt;code&gt;helloworld&lt;/code&gt; and &lt;code&gt;clean&lt;/code&gt;, while
the second and fourth line should have exactly one tab (not four or eight spaces!) at the
beginning of the line.&lt;/p&gt;

&lt;p&gt;Now, let&amp;rsquo;s use the makefile by running &lt;code&gt;make&lt;/code&gt;. Go ahead and close nano.
(Remember: CTRL-O and ENTER to save, CTRL-X to exit.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# make
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You should see that make builds the &lt;code&gt;helloworld&lt;/code&gt; executable using &lt;code&gt;gcc -o helloworld hello.c&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Why don&amp;rsquo;t you try running it again? Type:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# make
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This time you should get a message back, &lt;code&gt;make: &#39;helloworld&#39; is up to date.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;OK. So what is going on? The basic idea is that a makefile is a series of &lt;em&gt;build
targets&lt;/em&gt; followed by the commands to build the target. A particular build target
may &lt;em&gt;depend&lt;/em&gt; on other build targets or files (listed after the colon). If there
is a change in a dependency, make will run the target. So let&amp;rsquo;s dissect the
first build target &lt;code&gt;helloworld&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;helloworld: hello.c
    gcc -o helloworld hello.c
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The first part, &lt;code&gt;helloworld:&lt;/code&gt; is telling &lt;code&gt;make&lt;/code&gt; that the build target for the
coming list of commands is to build a file called &lt;code&gt;helloworld&lt;/code&gt;. The string after
the colon tells &lt;code&gt;make&lt;/code&gt; exactly which other files this depends on. Here building
&lt;code&gt;helloworld&lt;/code&gt; only depends on the source file &lt;code&gt;hello.c&lt;/code&gt;. Then the next line tells
&lt;code&gt;make&lt;/code&gt; how to build the &lt;code&gt;helloworld&lt;/code&gt; file, namely by running the compiler.&lt;/p&gt;

&lt;p&gt;The first time you called &lt;code&gt;make&lt;/code&gt;, it looked at the first build target in your
make file, which is &lt;code&gt;helloworld&lt;/code&gt;. It then checked the file system to see if a
&lt;code&gt;helloworld&lt;/code&gt; file already existed. Next it checked the dependency list, and
found that indeed a &lt;code&gt;hello.c&lt;/code&gt; file &lt;em&gt;does exist&lt;/em&gt;. Since the build target did not
exist, but all of its dependencies did exist, &lt;code&gt;make&lt;/code&gt; ran the commands we placed
there (i.e. &lt;code&gt;gcc -o helloworld hello.c&lt;/code&gt;) which produced the &lt;code&gt;helloworld&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;The second time you ran &lt;code&gt;make&lt;/code&gt; however, when &lt;code&gt;make&lt;/code&gt; checked the file system to
see whether a &lt;code&gt;helloworld&lt;/code&gt; file existed it found one! Since one already existed,
there was no reason to run the compiler again, and so make told you everything
was up to date.&lt;/p&gt;

&lt;p&gt;&amp;ldquo;But wait!&amp;rdquo; you say, &amp;ldquo;what if I had modified the &lt;code&gt;hello.c&lt;/code&gt; file after calling
&lt;code&gt;make&lt;/code&gt;?&amp;rdquo; This is where the dependency list comes in. &lt;code&gt;make&lt;/code&gt; is not only checking
that &lt;code&gt;helloworld&lt;/code&gt; exists, but it is also checking to make sure that &lt;code&gt;helloworld&lt;/code&gt;
&lt;em&gt;is newer than&lt;/em&gt; its dependency &lt;code&gt;hello.c&lt;/code&gt;. To see this, let&amp;rsquo;s make a change to
&lt;code&gt;hello.c&lt;/code&gt; and run &lt;code&gt;make&lt;/code&gt; again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Add a new line to your &lt;code&gt;hello.c&lt;/code&gt; file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;...
puts(&amp;quot;Hello world!&amp;quot;);
puts(&amp;quot;Hello make!&amp;quot;);
...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; run &lt;code&gt;make&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# make
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You&amp;rsquo;ll see that this time &lt;code&gt;make&lt;/code&gt; does run the compiler. This is because your
file &lt;code&gt;hello.c&lt;/code&gt; is now newer than the &lt;code&gt;helloworld&lt;/code&gt; file, and &lt;code&gt;make&lt;/code&gt; realizes that
it needs to rebuild &lt;code&gt;helloworld&lt;/code&gt; since something has changed.&lt;/p&gt;

&lt;p&gt;So far, so good. However, there is a second build target: &lt;code&gt;clean&lt;/code&gt;. By default
&lt;code&gt;make&lt;/code&gt; will use the first build target it comes across; however, you can also
specify a build target by passing it as a command line argument to &lt;code&gt;make&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# make clean
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This tells &lt;code&gt;make&lt;/code&gt; to use the &lt;code&gt;clean&lt;/code&gt; target. Note that this doesn&amp;rsquo;t actually
&amp;ldquo;build&amp;rdquo; anything, instead it just deletes the &lt;code&gt;helloworld&lt;/code&gt; file. This is an
important point: you can put whatever commands you want in a build target.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; For instance, add the following after the &lt;code&gt;clean&lt;/code&gt; target:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gettime:
    date
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then call &lt;code&gt;make&lt;/code&gt; again using the &lt;code&gt;gettime&lt;/code&gt; target. What happened?&lt;/p&gt;

&lt;h2 id=&#34;larger-projects:72892675bcb09366bedb5a4fc80ce987&#34;&gt;Larger Projects&lt;/h2&gt;

&lt;p&gt;As your program grows much beyond the basic &amp;ldquo;Hello world.&amp;rdquo; example, you are
going to need to break it up into separate files for manageability.&lt;/p&gt;

&lt;p&gt;The C language lets you &amp;ldquo;embed&amp;rdquo; one file into another using the &lt;code&gt;#include&lt;/code&gt;
directive. If you look back at &lt;code&gt;hello.c&lt;/code&gt;, you&amp;rsquo;ll see that the first line reads:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This tells the C compiler to &lt;em&gt;include&lt;/em&gt; the &lt;code&gt;stdio.h&lt;/code&gt; file at this point in the
program. You can think of this as a copy/paste&amp;ndash;the compiler basically reads the
contents of the &lt;code&gt;stdio.h&lt;/code&gt; file and copies them into your &lt;code&gt;hello.c&lt;/code&gt; program
exactly where you put the &lt;code&gt;#include&lt;/code&gt; directive. This is similar to Java&amp;rsquo;s
&lt;code&gt;import&lt;/code&gt; statement, but is closer to a literal copy-and-paste.&lt;/p&gt;

&lt;p&gt;There are two ways to include a file, using either angle brackets (&amp;rsquo;&amp;lt;&amp;rsquo; and &amp;lsquo;&amp;gt;&amp;rsquo;)
or quote marks:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;#include &amp;lt;stdio.h&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;#include &amp;quot;helloprinter.h&amp;quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The difference is that angle brackets are used for library files that are part
of the standard (system-wide) C library, while quotes are used for local files.
&lt;strong&gt;In other words, you will use &lt;code&gt;#include &amp;quot;somefile.h&amp;quot;&lt;/code&gt; to include &lt;em&gt;your&lt;/em&gt;
files, and &lt;code&gt;#include &amp;lt;somefile.h&amp;gt;&lt;/code&gt; to include &lt;em&gt;library&lt;/em&gt; files.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;C comes with many standard library files.
The &lt;code&gt;stdio.h&lt;/code&gt; file is a particularly handy one that you will use a lot. We
included it above because that is where the &lt;code&gt;puts&lt;/code&gt; function is defined.
That function prints a simple string to the screen with a newline at the end;
it is roughly equivalent to &lt;code&gt;System.out.println()&lt;/code&gt; in Java.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Create a file called &lt;code&gt;helloprinter.h&lt;/code&gt; and put the following in it:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;#include &amp;lt;stdio.h&amp;gt;

void helloprinter()
{
    puts(&amp;quot;Hello from another file.&amp;quot;);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Edit your &lt;code&gt;hello.c&lt;/code&gt; by adding the appropriate &lt;code&gt;#include&lt;/code&gt; statement
to include your &lt;code&gt;helloprinter.h&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Edit the &lt;code&gt;main()&lt;/code&gt; function in &lt;code&gt;hello.c&lt;/code&gt; so that it calls
&lt;code&gt;helloprinter()&lt;/code&gt; instead of calling &lt;code&gt;puts&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Recompile everything using &lt;code&gt;make&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt; Should we edit our makefile at this point? If so, how?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt; What happens if you &lt;code&gt;#include&lt;/code&gt; your &lt;code&gt;helloprinter.h&lt;/code&gt; file twice in
&lt;code&gt;hello.c&lt;/code&gt;?&lt;/p&gt;

&lt;h3 id=&#34;separate-your-api-from-your-code:72892675bcb09366bedb5a4fc80ce987&#34;&gt;Separate Your API from Your Code&lt;/h3&gt;

&lt;!-- TODO: define API ? --&gt;

&lt;!-- TODO: Why would you want to? --&gt;

&lt;p&gt;One common practice in C that is a little different from Java is
that we often separate the &lt;em&gt;declaration&lt;/em&gt; of a function (its interface) from the
&lt;em&gt;definition&lt;/em&gt; of that function (its implementation; i.e., its code). In C, the
declaration of a function is simply its return type, name, and function
parameters followed by a semicolon. Thus, a declaration for the &lt;code&gt;helloprinter()&lt;/code&gt;
function would look like this:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;void helloprinter();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Somewhere else we would put the definition:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;void helloprinter()
{
    // code goes here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can think of the declaration as a promise to or a contract with the
compiler: &lt;code&gt;void helloprinter();&lt;/code&gt; says to the compiler, &amp;ldquo;Hey, I&amp;rsquo;m going to
define a function with signature &lt;code&gt;void helloprinter()&lt;/code&gt; somewhere down the line,
I promise.&amp;rdquo;&lt;/p&gt;

&lt;p&gt;This can be useful, for instance, if a function calls another function that is
defined below it. For instance, the following will not compile:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;void call_helloprinter()
{
    helloprinter(); // this will cause an error, since helloprinter has not yet been defined
}

void helloprinter()
{
    ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;but the following will compile:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;void helloprinter(); // we promise to define a helloprinter() function

void call_helloprinter()
{
    helloprinter(); // no error this time, since the compiler is taking us at our word, for now
}

void helloprinter()
{
    // code goes here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;This highlights an important difference between C compilers and the Java
compiler:&lt;/strong&gt; a C compiler looks through source code in order, so you can only
use a function if it has already been declared. The Java compiler does multiple
passes over the code; it uses one pass to read off all of the functions and find
where they are defined, then it uses a second pass to actually compile things.&lt;/p&gt;

&lt;!-- NOTE: I don&#39;t think this is actually accurate (I think Java just parses
everything in one pass and uses static analysis to find undefined functions),
but I suppose it&#39;s close enough to the truth for this course. - mike --&gt;

&lt;h3 id=&#34;header-files-vs-source-files:72892675bcb09366bedb5a4fc80ce987&#34;&gt;Header Files vs. Source Files&lt;/h3&gt;

&lt;p&gt;The other reason to use forward declarations is to separate your code into
header files and source files. You can think of header files as the &lt;em&gt;public&lt;/em&gt;
interface to your code, or rather the part of your code that some other
programmer might need to see, while the source files are the &lt;em&gt;private&lt;/em&gt; part of
your code, which contains implementations. Above we just defined the
&lt;code&gt;helloprinter()&lt;/code&gt; function in the header file, but now we are going to break it
into two pieces, a header file containing only the declaration of the
&lt;code&gt;helloprinter()&lt;/code&gt; function, and a source file containing the actual definition.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Create the following two files if you haven&amp;rsquo;t already:&lt;/p&gt;

&lt;p&gt;File 1 - &lt;code&gt;helloprinter.h&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;void helloprinter();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;File 2 - &lt;code&gt;helloprinter.c&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;void helloprinter()
{
    puts(&amp;quot;Hello world.&amp;quot;);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This way someone using the code only needs to look through the &lt;code&gt;helloprinter.h&lt;/code&gt;
file to see what functions are available and how to use it. The header file is
sort of like an index to a book, it gives you a guide for what is there, but
doesn&amp;rsquo;t give you the actual content.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt; What happens when you call &lt;code&gt;make&lt;/code&gt; now?&lt;/p&gt;

&lt;h3 id=&#34;creating-object-files:72892675bcb09366bedb5a4fc80ce987&#34;&gt;Creating Object Files&lt;/h3&gt;

&lt;p&gt;Now that we&amp;rsquo;ve split our little hello printer library into a header file and a
source file, we need to start compiling a little differently. The reason &lt;code&gt;make&lt;/code&gt;
now fails is that &lt;code&gt;hello.c&lt;/code&gt; only &lt;code&gt;#include&lt;/code&gt;s the &lt;code&gt;helloprinter.h&lt;/code&gt; file, but it
does not include the &lt;code&gt;helloprinter.c&lt;/code&gt; file anywhere. So when the compiler tried
to compile, it saw in &lt;code&gt;helloprinter.h&lt;/code&gt; that we promised we would define the
&lt;code&gt;helloprinter()&lt;/code&gt; function somewhere, but we never made good on that promise (at
least not according to the compiler). When the compiler is building the final
executable, it has to check that we made good on all of our promises, otherwise
the executable would not know what to do when the &lt;code&gt;helloprinter()&lt;/code&gt; function was
called!&lt;/p&gt;

&lt;p&gt;To handle this situation, we will begin using a kind of intermediate file called
an &lt;em&gt;object file&lt;/em&gt;. An object file contains compiled code from a single source
code file (as you will recall, source code files are also sometimes called
&lt;em&gt;modules&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;Thus, we will separate compilation into two processes. The first step is to
&lt;em&gt;compile&lt;/em&gt; all modules into object files. At this point, the compiler does not
yet check that we&amp;rsquo;ve made good on all of our promises. Then, in a second step
called &lt;em&gt;linking&lt;/em&gt;, the compiler will take all of our object files and &lt;em&gt;link&lt;/em&gt;
(combine) them into one executable. Only then will it &amp;ldquo;connect the dots&amp;rdquo; and
check that we&amp;rsquo;ve actually made good on our promises.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;../../../../images/compile_link.png&#34; alt=&#34;Compilation and Linking&#34; /&gt;&lt;/p&gt;

&lt;p&gt;So now we have two source files, &lt;code&gt;helloprinter.c&lt;/code&gt; and &lt;code&gt;hello.c&lt;/code&gt;. For a second,
let&amp;rsquo;s ignore the makefile and compile them by hand again. The &lt;code&gt;-c&lt;/code&gt; flag tells
the compiler to compile to an object file (i.e., only perform the first step of
compilation). When we compile a file called &lt;code&gt;xyz.c&lt;/code&gt;, the compiler will
automatically produce an object file called &lt;code&gt;xyz.o&lt;/code&gt;. Thus, there is no need to
specify output using the &lt;code&gt;-o&lt;/code&gt; flag like we did before.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# gcc -c helloprinter.c
# gcc -c hello.c
# ls
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You should now see that the compiler has produced &lt;code&gt;helloprinter.o&lt;/code&gt; and &lt;code&gt;hello.o&lt;/code&gt;
object files. However, attempting to run either of these will not work:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# ./hello.o
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It&amp;rsquo;s not executable yet, since it doesn&amp;rsquo;t know where to find things like
&lt;code&gt;helloprinter()&lt;/code&gt;. These &amp;ldquo;.o&amp;rdquo; files are the object files. They are the output of
the first step of compilation (actual &amp;ldquo;compiling&amp;rdquo;) and the input to the second
step of compilation (&amp;ldquo;linking&amp;rdquo;).&lt;/p&gt;

&lt;h3 id=&#34;linking-object-files:72892675bcb09366bedb5a4fc80ce987&#34;&gt;Linking Object Files&lt;/h3&gt;

&lt;p&gt;The final step, then, is to &lt;em&gt;link&lt;/em&gt; all of our object files together into an
executable. This time the compiler will check that we have actually defined all
of the function that we declared and used. The command looks very similar to the
command we were using before, except that we call it on the object files instead
of the source files:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# gcc -o helloworld_linked hello.o helloprinter.o
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This time, all should be well and you should have a brand new &lt;code&gt;helloworld_linked&lt;/code&gt; binary to run.&lt;/p&gt;

&lt;h2 id=&#34;makefiles-revisited:72892675bcb09366bedb5a4fc80ce987&#34;&gt;Makefiles Revisited&lt;/h2&gt;

&lt;p&gt;Now the compilation is getting more complicated&amp;ndash;we&amp;rsquo;re both compiling files to
object code and linking them. Let&amp;rsquo;s see how to handle this in the makefile.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt; How many build targets do we need now? What are they?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Modify the makefile in order to correctly build the &lt;code&gt;helloworld_linked&lt;/code&gt; binary.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Hint&lt;/em&gt;: You are going to need build targets for each of the files you want to
build: &lt;code&gt;hello.o&lt;/code&gt;, &lt;code&gt;helloprinter.o&lt;/code&gt;, and &lt;code&gt;helloworld_linked&lt;/code&gt;. Think about what
other files each one depends on when you are designing your makefile.
You will probably also want to add &amp;ldquo;&lt;code&gt;*.o&lt;/code&gt;&amp;rdquo; to the &lt;code&gt;clean&lt;/code&gt; target to remove any
compiled object files.&lt;/p&gt;

&lt;p&gt;In future labs, we will be providing default makefiles. For the programming
assignments, you will need to download and modify the generic makefiles that we
have provided on Piazza.&lt;/p&gt;

&lt;p&gt;Review of file types:&lt;/p&gt;

&lt;p&gt;Extension       Description&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;.c              C source code file
.h              C header file
.o              object file (created by compiling a .c file)
(none)          executable file (created by linking .o files)&lt;/p&gt;

&lt;h3 id=&#34;compiler-flags:72892675bcb09366bedb5a4fc80ce987&#34;&gt;Compiler Flags&lt;/h3&gt;

&lt;p&gt;One quick side note: throughout this exercise, we&amp;rsquo;ve been compiling and linking
without any special flags sent to &lt;code&gt;gcc&lt;/code&gt;. However, the &lt;code&gt;gcc&lt;/code&gt; compiler has a lot
of useful options, some of which we will use in this course. Typically,
we will use the following flags:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;-g -O0 -Wall --std=c99 -pedantic
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;-g&lt;/code&gt; Tells the compiler to compile with additional debug information so that
when you use a debugger it can tell you things like what line number the
program crashed on (similar to the stack traces from Java). This is very useful.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;-O0&lt;/code&gt; (Note that is an Oh-Zero, not two Os or two 0s) Tells the compiler not
to do any code optimization. GCC can do lots of optimizations to make your
code run faster, which is great, but it often makes debugging a lot more
difficult. Thus, when you are in development, its best to use &lt;code&gt;-O0&lt;/code&gt; to ensure no
optimization is done.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;--std=c99&lt;/code&gt; Tells the compiler we want to use the C99 version of the C
language standard. This allows us to incorporate some useful features that
were added after the original ANSI C standard (C90). There is an even newer
version of the language standard (C11), but we will not use any of its features
in this class.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;-Wall&lt;/code&gt; As with Java, in addition to errors the compiler can also warn you
about various potential problems with the code. Warnings come at different
severity levels and &lt;code&gt;-Wall&lt;/code&gt; tells the compiler to show you any warning no matter
how slight. This may frustrate you at times, but it will save you time in the
long run by catching mistakes earlier.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;-pedantic&lt;/code&gt; This one is like &lt;code&gt;-Wall&lt;/code&gt; but essentially tells the compiler to be
even pickier and complain about things that it normally would not complain
about. This will also help you a lot with debugging.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thus, a compilation step will usually look like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# gcc -c -g -O0 -Wall --std=c99 -pedantic some_file.c
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Similarly, when linking we will pass the &lt;code&gt;-g&lt;/code&gt; and &lt;code&gt;-O0&lt;/code&gt; flags:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# gcc -g -O0 -o my_shiny_new_binary all.o of.o my.o object.o files.o to.o link.o
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Hopefully now you can see how useful makefiles are. Those are really long
commands; you don&amp;rsquo;t want to have to type all of that out every time you
want to compile your code!&lt;/p&gt;

&lt;h3 id=&#34;optional-other-useful-makefile-things-variables:72892675bcb09366bedb5a4fc80ce987&#34;&gt;(Optional) Other Useful Makefile Things: Variables&lt;/h3&gt;

&lt;p&gt;Two other useful things to know for building makefiles are variables and
patterns. This is useful, for instance, in specifying what flags you use in
compiling.&lt;/p&gt;

&lt;p&gt;For instance, you might have:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;file1.o: file1.c
    gcc -g -O0 -Wall --std=c99 -pedantic file1.c
file2.o: file2.c
    gcc -g -O0 -Wall --std=c99 -pedantic file2.c
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now assume you decide to stop using the &lt;code&gt;-O0&lt;/code&gt; flag. You&amp;rsquo;d have to edit the file
in two places. Instead, you could use a variable:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;CFLAGS=-g -O0 -Wall --std=c99 -pedantic

file1.o: file1.c
    gcc $(CFLAGS) file1.c
file2.o: file2.c
    gcc $(CFLAGS) file2.c
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The variable (note it is in all caps) is called &lt;code&gt;CFLAGS&lt;/code&gt; and you refer to it
using &lt;code&gt;$(CFLAGS)&lt;/code&gt;. Now we can update the &lt;code&gt;CFLAGS&lt;/code&gt; variable (in one place)
and it will change the flags we use in all of the compilation steps.&lt;/p&gt;

&lt;p&gt;Maybe you even want to play around with which compiler to use (and not
necessarily use gcc). That can be done using a variable as well:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;CC=gcc
CFLAGS=-g -O0 -Wall --std=c99 -pedantic

file1.o: file1.c
    $(CC) $(CFLAGS) file1.c
file2.o: file2.c
    $(CC) $(CFLAGS) file2.c
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There are also special variables. For instance, the &lt;code&gt;$&amp;lt;&lt;/code&gt; variable refers to the
first dependency in the dependency list. We can modify our makefile to use this
as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;CC=gcc
CFLAGS=-g -O0 -Wall --std=c99 -pedantic

file1.o: file1.c
    $(CC) $(CFLAGS) $&amp;lt;
file2.o: file2.c
    $(CC) $(CFLAGS) $&amp;lt;
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&#34;optional-other-useful-makefile-things-patterns:72892675bcb09366bedb5a4fc80ce987&#34;&gt;(Optional) Other Useful Makefile Things: Patterns&lt;/h3&gt;

&lt;p&gt;As you see above, using variables can help us make our makefile more
maintainable. Another nice feature of makefiles is the ability to do pattern
matching. Notice that the following two rules are essentially the same:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;file1.o: file1.c
    $(CC) $(CFLAGS) $&amp;lt;
file2.o: file2.c
    $(CC) $(CFLAGS) $&amp;lt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We can replace this with a single rule:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;%.o: %.c
    $(CC) $(CFLAGS) $&amp;lt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This rule essentially uses &lt;code&gt;%&lt;/code&gt; as a wild card. Thus, it will match both
&lt;code&gt;file1.o&lt;/code&gt; and &lt;code&gt;file2.o&lt;/code&gt;. In the first case it will use &lt;code&gt;file1.c&lt;/code&gt; as a dependency
(and compile it thanks to the &lt;code&gt;$&amp;lt;&lt;/code&gt; variable), and in the second will compile
&lt;code&gt;file2.c&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The makefile system is very powerful, and we have only scratched the surface in
this tutorial. Makefiles allow many other time-saving tricks. There is also a
platform-independent system for generating makefiles called &lt;a href=&#34;http://www.gnu.org/software/automake/manual/html_node/Autotools-Introduction.html&#34;&gt;autotools&lt;/a&gt;,
as well as more modern implementations such as
&lt;a href=&#34;http://www.cmake.org/&#34;&gt;CMake&lt;/a&gt;.
Such systems are outside the scope of this course, but if you are interested we
encourage you to join your local Unix Users Group.&lt;/p&gt;

&lt;h3 id=&#34;challenge-problem-add-another-code-module:72892675bcb09366bedb5a4fc80ce987&#34;&gt;(Challenge Problem) Add another code module&lt;/h3&gt;

&lt;p&gt;Create two new files, &lt;code&gt;name_printer.h&lt;/code&gt; and &lt;code&gt;name_printer.c&lt;/code&gt;. Add a function
called &lt;code&gt;print_name()&lt;/code&gt; to your new &lt;code&gt;name_printer&lt;/code&gt; module that prints out your
name. You should declare &lt;code&gt;print_name()&lt;/code&gt; in the header file (&lt;code&gt;.h&lt;/code&gt;) and define
&lt;code&gt;print_name()&lt;/code&gt; in the source file (&lt;code&gt;.c&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Then, add a call to &lt;code&gt;print_name()&lt;/code&gt; in the &lt;code&gt;main()&lt;/code&gt;
function in &lt;code&gt;main.c&lt;/code&gt;. Make sure that your code compiles and runs. This
will require making changes to the makefile in order to compile the
&lt;code&gt;name_printer&lt;/code&gt; module.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>PA 0</title>
      <link>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240/pa0/</link>
      <pubDate>Tue, 01 Sep 2015 10:55:33 -0400</pubDate>
      
      <guid>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240/pa0/</guid>
      <description>

&lt;h1 id=&#34;pa-0-arithmetic:5cc97e04113e953891ed30eab2c02e43&#34;&gt;PA 0: Arithmetic&lt;/h1&gt;

&lt;h2 id=&#34;objective:5cc97e04113e953891ed30eab2c02e43&#34;&gt;Objective&lt;/h2&gt;

&lt;p&gt;This is a very simple &amp;ldquo;programming assignment&amp;rdquo; designed to make sure that your
development environment is set up correctly and to familiarize you with the PA
completion process in CS 240.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PLEASE READ THIS DOCUMENT CAREFULLY!&lt;/strong&gt; Although this project spec is rather
lengthy, the amount of code you will need to write is very small (fewer than ten
lines). Use this opportunity to become more comfortable with the tools that you
be using throughout the semester (the compiler, the make system, the test suite,
the debugger, etc.) without the stress of a &amp;ldquo;real&amp;rdquo; programming assignment.&lt;/p&gt;

&lt;p&gt;If you have questions or run into issues, please post a question on Piazza or
come to office hours.&lt;/p&gt;

&lt;h2 id=&#34;introduction:5cc97e04113e953891ed30eab2c02e43&#34;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;Here are the starter files for this project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://w3.cs.jmu.edu/lam2mo/cs240_2015_08/files/00_arith.tar.gz&#34;&gt;00_arith.tar.gz&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://w3.cs.jmu.edu/lam2mo/cs240_2015_08/files/00_arith.zip&#34;&gt;00_arith.zip&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The two versions contain identical files, and differ only in the container
format.  If you are using GNU/Linux or Mac OS X, you probably want to use the
tarball (&lt;code&gt;.tar.gz&lt;/code&gt;) and if you&amp;rsquo;re using Windows you probably want to use the zip
file.&lt;/p&gt;

&lt;p&gt;The rest of this section contains step-by-step instructions for setting up your
working environment. Pay close attention to this process; future project
descriptions will not contain this level of detail.&lt;/p&gt;

&lt;p&gt;First you will need to create a folder in which to work. We recommend using
version control software (e.g., &lt;a href=&#34;https://git-scm.com/&#34;&gt;Git&lt;/a&gt; or
&lt;a href=&#34;https://mercurial.selenic.com/&#34;&gt;Mercural&lt;/a&gt;) to track your changes, or keeping
your project files in a location that is automatically backed up (e.g., using
&lt;a href=&#34;https://www.dropbox.com/&#34;&gt;Dropbox&lt;/a&gt;). Download one of the starter files linked
above and put it in the working folder.&lt;/p&gt;

&lt;p&gt;Next, unzip the starter files into the project folder. If you&amp;rsquo;re using the
tarball, use a command like the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;tar -zxvf 00_arith.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;On some operating systems, you can create a folder, download the starter files,
and unzip them using a graphical interface. That&amp;rsquo;s fine. For the rest of the
steps, however, you will want to open a terminal and &lt;code&gt;cd&lt;/code&gt; to the folder where
the unzipped project files are.&lt;/p&gt;

&lt;p&gt;First, do an &lt;code&gt;ls&lt;/code&gt; to see what files you have to work with. For this project, the
results should look like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ls
arith.c     arith.h     main.c      testsuite.c
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Notice that there is currently no makefile. Because the Makefile needs to be
customized for various operating systems, we will NOT be distributing makefiles
with the other starter files. However, we have posted generic makefiles for each
supported operating system on Piazza in the &amp;ldquo;Generic C Makefiles&amp;rdquo; thread. Open
this thread and download the appropriate file into the project folder.&lt;/p&gt;

&lt;p&gt;For example, suppose you were on an ISAT 250 lab machine running Mint Linux.
Consulting the Piazza thread, you will find that the correct makefile for that
environment is &lt;code&gt;Makefile.ubuntu&lt;/code&gt;. After downloading that file to the working
folder, you should see it in &lt;code&gt;ls&lt;/code&gt; results:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ls
Makefile.ubuntu arith.c         arith.h         main.c          testsuite.c
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Rename the file to remove the &lt;code&gt;.ubuntu&lt;/code&gt; extension because it is no longer
necessary:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ mv Makefile.ubuntu Makefile

$ ls
Makefile    arith.c     arith.h     main.c      testsuite.c
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Take a moment to look over the Makefile. It includes very generic support for
compiling a &lt;code&gt;main.c&lt;/code&gt; module and a &lt;code&gt;testsuite.c&lt;/code&gt; module (both of which should be
included in the starter files). Before you compile, you will need to update the
Makefile to include any extra modules or libraries needed by the project. This
may require modifying the following section:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# application-specific settings and run target

EXE=main
TEST=testsuite
MODS=
LIBS=
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For this project, you can see from the provided files that there is one
additional module: &lt;code&gt;arith.c&lt;/code&gt;. Thus, you will need to modify the Makefile, adding
the extra module to the &lt;code&gt;MODS&lt;/code&gt; variable. The new line should look like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;MODS=arith.o
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that the extension is &lt;code&gt;.o&lt;/code&gt; instead of &lt;code&gt;.c&lt;/code&gt;; that is because the &lt;code&gt;MODS&lt;/code&gt;
variable is used to denote object dependencies later in the makefile, ensuring
that the build system re-compiles the object files when necessary.&lt;/p&gt;

&lt;p&gt;In addition, if the project requires any library linking options (e.g., &lt;code&gt;-lm&lt;/code&gt;
for the math library), you would need to add them to the &lt;code&gt;LIBS&lt;/code&gt; definition.
However, this particular project does not need any external libraries.&lt;/p&gt;

&lt;p&gt;At this point, you should be able to build and run the project:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ make
gcc -c -g -O0 -Wall --std=c99 -pedantic main.c
gcc -c -g -O0 -Wall --std=c99 -pedantic arith.c
gcc -g -O0 -o main main.o arith.o

$ ./main
|2| + |3| = 0
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can see that the result is incorrect; this is because you have not yet
completed the project.&lt;/p&gt;

&lt;h2 id=&#34;project-description:5cc97e04113e953891ed30eab2c02e43&#34;&gt;Project Description&lt;/h2&gt;

&lt;p&gt;This project is very simple. You must implement a single function in &lt;code&gt;arith.c&lt;/code&gt;
as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;int add_abs(int a, int b)&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Takes two integer parameters and returns the sum of their absolute values.&lt;/p&gt;
&lt;/blockquote&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;project-testing:5cc97e04113e953891ed30eab2c02e43&#34;&gt;Project Testing&lt;/h2&gt;

&lt;p&gt;&lt;p/&gt;&lt;/p&gt;

&lt;h3 id=&#34;public-tests:5cc97e04113e953891ed30eab2c02e43&#34;&gt;Public Tests&lt;/h3&gt;

&lt;p&gt;If you have the &lt;a href=&#34;http://check.sourceforge.net/&#34;&gt;Check&lt;/a&gt; unit testing framework
installed (highly recommended!), you can run the test suite with the &lt;code&gt;make
test&lt;/code&gt; target. If you run it before working on the project, you will see output
that looks like the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ make test
gcc -c -g -O0 -Wall --std=c99 -pedantic -Wno-gnu-zero-variadic-macro-arguments testsuite.c
gcc -L/opt/local/lib -o testsuite testsuite.o arith.o  -lcheck -lm -lpthread
./testsuite
Running suite(s): Default
0%: Checks: 1, Failures: 1, Errors: 0
testsuite.c:11:F:Core:add_pos:0: Assertion &#39;add_abs(2, 3)==5&#39; failed: add_abs(2, 3)==0, 5==5
FAILURE: At least one test failed or crashed.
make: *** [test] Error 1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can see that the test suite ran a single check that failed. To see the
actual test, look at the &lt;code&gt;testsuite.c&lt;/code&gt; file for the test name (the label after
&amp;ldquo;Core&amp;rdquo; in the printout: in this case, &amp;ldquo;&lt;code&gt;add_pos&lt;/code&gt;&amp;rdquo;):&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;START_TEST (add_pos)
{ ck_assert_int_eq(add_abs(2, 3), 5); }
END_TEST
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you can see, this test calls &lt;code&gt;add_abs&lt;/code&gt; with parameters &lt;code&gt;2&lt;/code&gt; and &lt;code&gt;3&lt;/code&gt;, expecting
the result to be &lt;code&gt;5&lt;/code&gt;. The test output indicates that the result is in fact &lt;code&gt;0&lt;/code&gt;.
If you haven&amp;rsquo;t started work on the project, this makes sense considering that
the starter file contains a non-functional implementation of the &lt;code&gt;add_abs&lt;/code&gt;
function:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;int add_abs(int a, int b)
{
    return 0;   // TODO: finish this function!
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Alternatively, if you HAD already done some work on the project and had thus
been expecting the test to pass, the tests results would indicate that the
&lt;code&gt;add_abs&lt;/code&gt; function is a good place to start debugging.&lt;/p&gt;

&lt;p&gt;After you have implemented the function using a native solution (&amp;rdquo;&lt;code&gt;return a +
b;&lt;/code&gt;&amp;rdquo;), the test should pass:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ make test
gcc -c -g -O0 -Wall --std=c99 -pedantic arith.c
gcc -L/opt/local/lib -o testsuite testsuite.o arith.o  -lcheck -lm -lpthread
./testsuite
Running suite(s): Default
100%: Checks: 1, Failures: 0, Errors: 0
SUCCESS: All current tests passed!
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;At this point, it is tempting to consider your work done. After all, you have
passed the test! However, this is only a single test, and may not be exhaustive
(in fact, it is by design NOT exhaustive for this assignment). Thus, your code
might not in fact be fully compliant with the project specification and would
therefore not be eligible for full credit.&lt;/p&gt;

&lt;p&gt;In fact, there is a key type of input that the provided public testsuite does
not test for. Can you figure out what it is?&lt;/p&gt;

&lt;h3 id=&#34;private-tests:5cc97e04113e953891ed30eab2c02e43&#34;&gt;Private Tests&lt;/h3&gt;

&lt;p&gt;For this assignment (and this assignment ONLY) we have posted the full &amp;ldquo;private&amp;rdquo;
testsuite in the Files section of Canvas. It is strongly recommended that you
download this testsuite, replace the original (public) version of &lt;code&gt;testsuite.c&lt;/code&gt;,
and then re-run the tests to make sure that your code passes.&lt;/p&gt;

&lt;p&gt;Look carefully at the private testsuite; you should now be able to see the class
of inputs that the public testsuite does not check for. You should think about
how you could have anticipated that class of inputs from the project description
without seeing the private test suite.&lt;/p&gt;

&lt;p&gt;This is why it is very important to write your own tests while working on PAs in
this class; as stated in the course syllabus, we will be testing your code at
least partially based on test cases that you will not be aware of when you
submit. This reflects a typical software development situation in industry,
where clients often have &amp;ldquo;hidden&amp;rdquo; requirements that only surface after you have
delivered the project. In many cases, even the client is unaware of these
requirements (e.g., compatibility issues with legacy systems).  Thus, the
ability to write good tests is an important skill that you will find very
valuable in your career.&lt;/p&gt;

&lt;p&gt;One important difference between this class and dealing with clients in industry
is that in this class we will be providing exceptionally clear project
specifications.  This should enable you to anticipate the full range of tests
that we might apply. To write your own tests, you can use the Check testing
framework and add more tests to &lt;code&gt;testsuite.c&lt;/code&gt;, or you can write your own ad-hoc
testing methods in &lt;code&gt;main.c&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Feel free to share and discuss test cases on
Piazza; however, be aware that test cases provided by other students may not be
entirely true to the project spec.  &lt;strong&gt;Always read the spec carefully and test your
code thoroughly!&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&#34;submission:5cc97e04113e953891ed30eab2c02e43&#34;&gt;Submission&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;DUE DATE:&lt;/strong&gt; Friday, September 11, 2015 at 23:59 EDT (11:59pm)&lt;/p&gt;

&lt;p&gt;Submit ONLY your completed &lt;code&gt;arith.c&lt;/code&gt; file on Canvas using the appropriate
assignment submission. Make sure the file contains a comment field at the top
with your name and a statement of compliance with the &lt;a href=&#34;http://www.jmu.edu/honor/code.shtml&#34;&gt;JMU honor
code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Please check your code carefully and make sure that it complies with the project
specification. In addition, make sure that your code adheres to general good
coding style guidelines. Fix any spelling mistakes, incorrect code formatting,
and inconsistent whitespace before submitting. Make sure you include any
appropriate documentation.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Lab 02</title>
      <link>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240/lab02/</link>
      <pubDate>Tue, 01 Sep 2015 10:55:33 -0400</pubDate>
      
      <guid>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240/lab02/</guid>
      <description>

&lt;h1 id=&#34;basic-c-programming:714bed912a45b0ad0844699f7b4a4a71&#34;&gt;Basic C programming&lt;/h1&gt;

&lt;p&gt;The goal of this tutorial is to learn basic C programming constructs. We will do this
by examining some of the familiar constructs from Java and learning their C equivalents.&lt;/p&gt;

&lt;p&gt;In order to make things more interesting than simply printing out &amp;ldquo;Hello world.&amp;rdquo; like we did
in the last class, we will be using a basic bitmap drawing package base on
&lt;a href=&#34;https://en.wikipedia.org/wiki/Turtle_graphics&#34;&gt;turtle graphics&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;../../../../images/shapes-polygons.png&#34; alt=&#34;Row of Shapes&#34; /&gt;&lt;/p&gt;

&lt;h2 id=&#34;learning-objectives:714bed912a45b0ad0844699f7b4a4a71&#34;&gt;Learning Objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lab, you should be able to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write basic C programs using standard constructs.&lt;/li&gt;
&lt;li&gt;Perform basic formatted output.&lt;/li&gt;
&lt;li&gt;Write basic C functions.&lt;/li&gt;
&lt;li&gt;Generate images using turtle graphics.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;starter-code:714bed912a45b0ad0844699f7b4a4a71&#34;&gt;Starter Code&lt;/h2&gt;

&lt;p&gt;First, download the starter files:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IN THE LINUX LAB (ISAT 250):&lt;/strong&gt; Download &lt;a href=&#34;../../../../files/lab02.tar.gz&#34;&gt;lab02.tar.gz&lt;/a&gt; and
untar it from the command line with the following command from the folder that
you saved the file in:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;tar -zxvf lab02.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;IN THE MAC LAB (ISAT 248):&lt;/strong&gt; Download &lt;a href=&#34;../../../../files/lab02.zip&#34;&gt;lab02.zip&lt;/a&gt; and unzip it by
either double-clicking it or using the following command line command in the
Terminal from the folder that you saved the file in:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;unzip lab02.zip
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The starter code has four files: a &lt;code&gt;main.c&lt;/code&gt;, which you will be modifying; &lt;code&gt;turtle.h&lt;/code&gt; and &lt;code&gt;turtle.c&lt;/code&gt;,
which contain the turtle graphics library; and an OS-specific &lt;code&gt;Makefile&lt;/code&gt; (the
latter is why it is important to download the correct file for your lab).&lt;/p&gt;

&lt;p&gt;Open up the &lt;code&gt;main.c&lt;/code&gt; file, and add the following lines to &lt;code&gt;int main()&lt;/code&gt; where it
says &lt;code&gt;// TODO: add your code here&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;turtle_forward(100);
turtle_turn_left(90);
turtle_forward(100);
turtle_draw_turtle();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now compile and run the &lt;code&gt;main&lt;/code&gt; program. You&amp;rsquo;ll see that it produced an output
file, &lt;code&gt;output.bmp&lt;/code&gt;. Open this in an image viewer:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IN THE LINUX LAB (ISAT 250):&lt;/strong&gt; Use the following command from the project
folder:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;eog output.bmp
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;IN THE MAC LAB (ISAT 248):&lt;/strong&gt; Double-click the bitmap file in Finder or use the following
command in the Terminal from the project folder:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;open output.bmp
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;turtle.h&lt;/code&gt; contains complete documentation for the functions available to you in the turtle library. The following functions should be enough for today&amp;rsquo;s lab:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;turtle_forward(int distance)&lt;/code&gt; / &lt;code&gt;turtle_backward(int distance)&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Move the turtle forwards/backwards by the number of pixels indicated.&lt;/p&gt;
&lt;/blockquote&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;turtle_turn_left(double angle)&lt;/code&gt; / &lt;code&gt;turtle_turn_right(double angle)&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Rotate the turtle left/right by &lt;code&gt;angle&lt;/code&gt; degrees.&lt;/p&gt;
&lt;/blockquote&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;turtle_pen_up()&lt;/code&gt; / &lt;code&gt;turtle_pen_down()&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;After &lt;code&gt;turtle_penup()&lt;/code&gt; is called the turtle will not draw until &lt;code&gt;turtle_pendown()&lt;/code&gt; is called.&lt;/p&gt;
&lt;/blockquote&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;turtle_goto(int x, int y)&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Go to the &lt;code&gt;(x, y)&lt;/code&gt; position indicated. Note that &lt;code&gt;(0,0)&lt;/code&gt; is the center of
the screen and this command preserves the orientation (direction) of the
turtle.&lt;/p&gt;
&lt;/blockquote&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;turtle_set_heading(double angle)&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Set the turtle&amp;rsquo;s orientation to the indicated angle. Zero degrees points
towards the right, and ninety degrees
points up.&lt;/p&gt;
&lt;/blockquote&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;turtle_init(int w, int h)&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Initializes the image size of the turtle&amp;rsquo;s space to width x height = &lt;code&gt;(w,
h)&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;turtle_save_bmp(char* filename)&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Saves the computed image to a file specified by the filename.&lt;/p&gt;
&lt;/blockquote&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To learn more about the turtle library, see the &lt;a href=&#34;https://w3.cs.jmu.edu/lam2mo/cs240_2015_08/turtle.html&#34;&gt;reference page&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&#34;getting-started:714bed912a45b0ad0844699f7b4a4a71&#34;&gt;Getting Started&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Spend a few minutes experimenting with compiling the code. Make changes to
&lt;code&gt;main.c&lt;/code&gt; to draw different things using the commands listed above. Can you make
the turtle draw a square? What about a rectangle or a triangle?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;java-to-c-a-crash-course:714bed912a45b0ad0844699f7b4a4a71&#34;&gt;Java to C: A Crash Course&lt;/h2&gt;

&lt;p&gt;&lt;p/&gt;&lt;/p&gt;

&lt;h3 id=&#34;data-types:714bed912a45b0ad0844699f7b4a4a71&#34;&gt;Data types&lt;/h3&gt;

&lt;p&gt;C has multiple integer data types; the most common is &lt;code&gt;int&lt;/code&gt;, which is
sufficient for our current needs. It is a signed integer at least 16 bits wide.
On most modern architectures, it is at least 32 bits.&lt;/p&gt;

&lt;p&gt;You can read about the other integer data types at the &lt;a href=&#34;https://en.wikipedia.org/wiki/C_data_types&#34;&gt;wikipedia
page&lt;/a&gt;. In future labs, we will use
the &lt;code&gt;size_t&lt;/code&gt; type, which is used by convention when we want to store
non-negative size values.&lt;/p&gt;

&lt;p&gt;Like Java, C has two floating-point formats: the 32-bit &lt;code&gt;float&lt;/code&gt; and the 64-bit
&lt;code&gt;double&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Like Java, C also has a single character data type called &lt;code&gt;char&lt;/code&gt;. However,
unlike Java, C does &lt;strong&gt;not&lt;/strong&gt; have a built-in string type. Rather, strings are
conventionally stored as an array of &lt;code&gt;char&lt;/code&gt; values. We will discuss strings in
C next week.&lt;/p&gt;

&lt;p&gt;Finally, if you &lt;code&gt;#include &amp;lt;stdbool.h&amp;gt;&lt;/code&gt;, you may use the &lt;code&gt;bool&lt;/code&gt; data type, which
is the same as the &lt;code&gt;boolean&lt;/code&gt; data type from Java. The &lt;code&gt;bool&lt;/code&gt; data type is not
part of the original C specification; it was a convention to use integers
instead (0 for false and 1 for true).&lt;/p&gt;

&lt;h3 id=&#34;comments:714bed912a45b0ad0844699f7b4a4a71&#34;&gt;Comments&lt;/h3&gt;

&lt;p&gt;Like Java, C has two kinds of comments: a single line format and a multi-line
format. Here are examples of both:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;c = a + b;          // this is a single line comment

/* this is a
 * multi-line comment */
d = a * b;
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&#34;console-output-using-printf:714bed912a45b0ad0844699f7b4a4a71&#34;&gt;Console output: Using &lt;code&gt;printf&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;The C standard library contains a very powerful string output function called
&lt;code&gt;printf&lt;/code&gt;; it can handle many different kinds of output formats. For the purposes
of this class, we will only need a few features.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;printf&lt;/code&gt; function is rather unusual in that it can take a variable number
of parameters. The first parameter is always a string, often called the &amp;ldquo;format
string.&amp;rdquo; If all you want to do is print a simple string, that&amp;rsquo;s all you have to
pass:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;printf(&amp;quot;Hello!\nThis is some simple text.&amp;quot;);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The resulting output is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Hello!
This is some simple text.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There are several important escape sequences:&lt;/p&gt;

&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Code&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\n&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;newline&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\r&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;tab&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\&amp;quot;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;quote mark&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;To print the values of variables, you can embed &lt;em&gt;format specifiers&lt;/em&gt; into the
format string. For example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;int a = 42;
float pi = 3.141592;
printf(&amp;quot;The answer is %d and %s is approximately %f.\n&amp;quot;, a, &amp;quot;PI&amp;quot;, pi);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The resulting output is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;The answer is 42 and PI is approximately 3.141592.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here is a list of important format specifiers:&lt;/p&gt;

&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Code&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;%d&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;signed integer (&lt;code&gt;int&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;&lt;code&gt;%u&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;unsigned integer (&lt;code&gt;size_t&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;&lt;code&gt;%f&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;floating-point number (&lt;code&gt;float&lt;/code&gt; or &lt;code&gt;double&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;&lt;code&gt;%e&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;scientific notation (&lt;code&gt;float&lt;/code&gt; or &lt;code&gt;double&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;&lt;code&gt;%c&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;character (&lt;code&gt;char&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;&lt;code&gt;%s&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;character string (&lt;code&gt;char[]&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;There is no format specifier for the &lt;code&gt;bool&lt;/code&gt; data type. If you wish to output the
value of a boolean, you will need to use a conditional to print different
strings (perhaps &lt;code&gt;&amp;quot;true&amp;quot;&lt;/code&gt; and &lt;code&gt;&amp;quot;false&amp;quot;&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Here is a &lt;a href=&#34;http://www.cplusplus.com/reference/cstdio/printf/&#34;&gt;full reference&lt;/a&gt; to
all the capabilities of the &lt;code&gt;printf&lt;/code&gt; function.&lt;/p&gt;

&lt;h3 id=&#34;conditionals:714bed912a45b0ad0844699f7b4a4a71&#34;&gt;Conditionals&lt;/h3&gt;

&lt;p&gt;Conditional statements in C look exactly like their Java counterparts.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;if (some-test) {
    ...
} else if (a-second-test) {
    ...
} else {
    ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&#34;functions-parameters-calling-and-recursion:714bed912a45b0ad0844699f7b4a4a71&#34;&gt;Functions, parameters, calling, and recursion&lt;/h3&gt;

&lt;p&gt;Function definitions in C look very similar to their Java counterparts. For example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;int add(int a, int b) {
    return a + b;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In this example, the function &lt;code&gt;add&lt;/code&gt; takes two integer parameters, &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; and returns an their sum as an integer.&lt;/p&gt;

&lt;p&gt;One difference between Java and C is that in Java every &amp;ldquo;function&amp;rdquo; is a method of some class. So the &lt;code&gt;add&lt;/code&gt; function defined above would be part of some class and to call it, you would need an object of that class. Something like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;int result = obj.add(3, 4); // add 3 and 4 using the add method of obj
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;C, however, is not object oriented. Functions are not part of some parent object, so you call them directly:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;int result = add(3, 4); // add 3 and 4 using the add function
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&#34;exercise-drawing-rectangles:714bed912a45b0ad0844699f7b4a4a71&#34;&gt;Exercise: Drawing Rectangles&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Add a new function with the following signature:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;void rectangle(int x, int y, int width, int height);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This function should draw a rectangle with the indicated width and height. The
lower-left corner of the rectangle should be at position &lt;code&gt;(x, y)&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Call your function from inside &lt;code&gt;main()&lt;/code&gt;. Test it by recompiling and running the application from the command line.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt; Can you define the function after &lt;code&gt;main()&lt;/code&gt;?&lt;/p&gt;

&lt;h3 id=&#34;looping-for-and-while:714bed912a45b0ad0844699f7b4a4a71&#34;&gt;Looping: &lt;code&gt;for&lt;/code&gt; and &lt;code&gt;while&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;As with Java, the main looping constructs in C are the &lt;code&gt;for&lt;/code&gt; and &lt;code&gt;while&lt;/code&gt; loops. Unlike Java, there is only one syntax for the &lt;code&gt;for&lt;/code&gt; loop, namely:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;for (int i = 0; i &amp;lt; n; i++) {
    ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or more generally,&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;for (initialization; condition; update) {
    ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;while&lt;/code&gt; loop behaves exactly the same as in Java:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;while (something_is_true) {
    do_this();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&#34;exercise-rows:714bed912a45b0ad0844699f7b4a4a71&#34;&gt;Exercise: Rows&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Add a new function with the following signature:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;void row(int x, int y, int count, int size);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This function should draw a horizontal row of squares with the lower-left corner
of the row at position &lt;code&gt;(x, y)&lt;/code&gt;. The &lt;code&gt;count&lt;/code&gt; and &lt;code&gt;size&lt;/code&gt; parameters indicate the
number of squares in the row and the size of each square respectively. Your
function should leave a small (5-10 pixel) fixed space between each square.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do not&lt;/strong&gt; copy/paste code from the existing &lt;code&gt;rectangle&lt;/code&gt; function! Your &lt;code&gt;row&lt;/code&gt;
function should invoke &lt;code&gt;rectangle&lt;/code&gt; with appropriate parameter values.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Update the &lt;code&gt;main&lt;/code&gt; function so that it includes several calls to your newly defined &lt;code&gt;row&lt;/code&gt; function.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;optional-exercises:714bed912a45b0ad0844699f7b4a4a71&#34;&gt;Optional Exercises&lt;/h2&gt;

&lt;p&gt;&lt;p/&gt;&lt;/p&gt;

&lt;h3 id=&#34;polygons:714bed912a45b0ad0844699f7b4a4a71&#34;&gt;Polygons&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Add a new function with the following signature:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;void polygon(int x, int y, int num_sides, int size);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This function should draw a regular polygon with the indicated number of sides
starting at position &lt;code&gt;(x, y)&lt;/code&gt;. The length of each side is determined by the
&lt;code&gt;size&lt;/code&gt; parameter. This function should work for any value of &lt;code&gt;sides&lt;/code&gt; that is
greater than 2. Do not hard-code the function for any particular number of
sides.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hint:
    * This can be accomplished using either a &lt;code&gt;for&lt;/code&gt; or a &lt;code&gt;while&lt;/code&gt; loop.
    * Note that the turning angles required to draw a square are all &lt;code&gt;90&lt;/code&gt; degrees and that &lt;code&gt;90 = 360 / 4&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Update the &lt;code&gt;main&lt;/code&gt; function so that it draws several different polygons in different places with different numbers of sides and side lengths.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Challenge Goal:&lt;/strong&gt; Try writing code to draw a series of polygons with the same general size but with an increasing number of sides (as in the graphic at the top of this page). You need to change the side length of the polygons as you increase the number of sides. Here are some questions to think about:

&lt;ul&gt;
&lt;li&gt;Do you need to increase or decrease the side length as you increase the number of sides.&lt;/li&gt;
&lt;li&gt;After how many sides is the polygon indistinguishable from a circle?&lt;/li&gt;
&lt;li&gt;Does this number vary if you change the initial size?&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&#34;grids:714bed912a45b0ad0844699f7b4a4a71&#34;&gt;Grids&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Write a new function with the following signature:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;void grid(int x, int y, int columns, int size)&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This function should create a &lt;code&gt;rows x columns&lt;/code&gt; grid of squares each of size &lt;code&gt;size&lt;/code&gt; with the lower-left corner at &lt;code&gt;(x, y)&lt;/code&gt;. The &lt;code&gt;size&lt;/code&gt; parameter indicates teh width of the individual squares.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For further experimentation, make alternating squares filled in a checkerboard pattern. You will need to look through the documentation for the &lt;code&gt;turtle_begin_fill()&lt;/code&gt; and &lt;code&gt;turtle_end_fill()&lt;/code&gt; functions. Here is an example of what it could look like:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&#34;../../../../images/shapes-checkerboard.png&#34; alt=&#34;A grid of alternating color blocks&#34; /&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Update the &lt;code&gt;main&lt;/code&gt; function so that it includes several calls to your newly defined &lt;code&gt;grid&lt;/code&gt; function.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&#34;random-walks:714bed912a45b0ad0844699f7b4a4a71&#34;&gt;Random Walks&lt;/h3&gt;

&lt;p&gt;Here is some code that simulates rolling a single die, generating a single
random number in the range [0,5]:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;#include &amp;lt;time.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;

/*
 * returns a random integer from 0 to 5 (inclusive)
 */
int roll_dice()
{
    static int initialized = 0;
    if (!initialized) {
        srand(time(NULL));
        initialized = true;
    }
    return rand() % 6;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Don&amp;rsquo;t worry about the details of how that function works right now; just
copy-and-paste it into your &lt;code&gt;main.c&lt;/code&gt; above the &lt;code&gt;main()&lt;/code&gt; function declaration.
Now write a function called &lt;code&gt;turtle_random_walk&lt;/code&gt; that does a &lt;a href=&#34;https://en.wikipedia.org/wiki/Random_walk&#34;&gt;random
walk&lt;/a&gt;. It should take an integer
parameter telling the turtle how many steps to take.&lt;/p&gt;

&lt;p&gt;For each step, the function should call the &lt;code&gt;roll_dice&lt;/code&gt; function to generate a
random number. If the number is 0 or 1, the turtle should move forward 10
spaces. If the number is 2 or 3, the turtle should turn left 90 degrees. If the
number is 4 or 5, the turtle should turn right 90 degrees.&lt;/p&gt;

&lt;p&gt;Run your program several times and observe the results.&lt;/p&gt;

&lt;h3 id=&#34;challenge-problem-sierpinski:714bed912a45b0ad0844699f7b4a4a71&#34;&gt;Challenge Problem: Sierpinski&lt;/h3&gt;

&lt;p&gt;If you want some practice with a more complex algorithm, try writing a function
that draws a &lt;a href=&#34;https://en.wikipedia.org/wiki/Sierpinski_triangle&#34;&gt;Sierpinski
triangle&lt;/a&gt;. You should be able
to reuse the polygon function you wrote earlier. Here is an example of what the
end result should look like:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;../../../../images/shapes-sierpinski.png&#34; alt=&#34;A Sierpinski triangle&#34; /&gt;&lt;/p&gt;

&lt;p&gt;Hint: Use recursion =D.&lt;/p&gt;

&lt;!--
TODO:
* switch statements
* file I/O
--&gt;
</description>
    </item>
    
    <item>
      <title>Lab 03</title>
      <link>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240/lab03/</link>
      <pubDate>Mon, 07 Sep 2015 10:55:33 -0400</pubDate>
      
      <guid>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240/lab03/</guid>
      <description>

&lt;h1 id=&#34;arrays-in-c:62db7dc4e96a9a6664437b0b49d20c48&#34;&gt;Arrays in C&lt;/h1&gt;

&lt;p&gt;The goal of this tutorial is to learn how to use basic static arrays in C.&lt;/p&gt;

&lt;p&gt;We will also continue to make use of the turtle graphics library used in the
last class.&lt;/p&gt;

&lt;h2 id=&#34;learning-objectives:62db7dc4e96a9a6664437b0b49d20c48&#34;&gt;Learning Objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lab, you should be able to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write basic C programs using arrays and strings&lt;/li&gt;
&lt;li&gt;Calculate the length of arrays&lt;/li&gt;
&lt;li&gt;Traverse and search an array in multiple directions and intervals&lt;/li&gt;
&lt;li&gt;Use arrays in combination with turtle graphics to draw lines and triangles&lt;/li&gt;
&lt;li&gt;Explain C strings and their storage format&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;starter-code:62db7dc4e96a9a6664437b0b49d20c48&#34;&gt;Starter Code&lt;/h2&gt;

&lt;p&gt;Begin by downloading the starter files:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IN THE LINUX LAB (ISAT 250):&lt;/strong&gt; Download &lt;a href=&#34;../../../../files/lab03.tar.gz&#34;&gt;lab03.tar.gz&lt;/a&gt;
and untar it from the command line with the following command from the folder
that you saved the file in:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;tar -zxvf lab03.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;IN THE MAC LAB (ISAT 248):&lt;/strong&gt; Download &lt;a href=&#34;../../../../files/lab03.zip&#34;&gt;lab03.zip&lt;/a&gt; and unzip
it by either double-clicking it or using the following command line command in
the Terminal from the folder that you saved the file in:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;unzip lab03.zip
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The starter code has four files, a &lt;code&gt;main.c&lt;/code&gt;, which you will be modifying;
&lt;code&gt;turtle.h&lt;/code&gt; and &lt;code&gt;turtle.c&lt;/code&gt;, which contain the turtle graphics library; and an
&lt;code&gt;Makefile&lt;/code&gt; to help with building the lab.&lt;/p&gt;

&lt;h2 id=&#34;an-array-of-options:62db7dc4e96a9a6664437b0b49d20c48&#34;&gt;An array of options&lt;/h2&gt;

&lt;p&gt;&lt;p/&gt;&lt;/p&gt;

&lt;h3 id=&#34;array-data-types:62db7dc4e96a9a6664437b0b49d20c48&#34;&gt;Array data types&lt;/h3&gt;

&lt;p&gt;An array in C is a bounded sequence of memory locations that store data
elements. These locations are accessed by an indexed offset from the
variable name.  Some examples of arrays are a list of grades for students in a
class, salaries for employees in a company, or number of seats in each row at a
theater.&lt;/p&gt;

&lt;p&gt;An array can be of any C type, but all elements in the array must be
of the same type.&lt;/p&gt;

&lt;p&gt;One important difference betweeen C and Java is that in Java the
size of the array is explicitly tracked by the language runtime, while C
arrays do not explicitly track such information.&lt;/p&gt;

&lt;p&gt;In particular, this means that you no longer have a &lt;code&gt;.length&lt;/code&gt; attribute
attached to each array.  In Java, you might have done something like this:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-java&#34;&gt;	int[] numbers = {1, 2, 5, 9};
	for (int i = 0; i &amp;lt; numbers.length; i++) {
		System.out.println(numbers[i]);
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In C, however, there is no &lt;code&gt;numbers.length&lt;/code&gt; attribute to access, and we would
have to track the length manually. Thus, the loop above would have to be
written something like:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;	int numbers[4] = {1, 2, 5, 9};
	for (int i = 0; i &amp;lt; 4; i++) {
		printf(&amp;quot;%d\n&amp;quot;, numbers[i]);
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;More importantly: if you write a function that takes an array as input, it will
need to also take the length of the array as input. For example, in Java you
could write an arithmetic product function like this:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-java&#34;&gt;	public int product(int[] nums)
	{
		int prod = 1;
		for (int i = 0; i &amp;lt; nums.length; i++) {
			prod *= nums[i];
		}
		return prod;
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In C, because there is no &lt;code&gt;nums.length&lt;/code&gt;, you should pass the length of the
array in as a parameter to the function as well:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;	int product(int nums[], int nums_len)
	{
		int prod = 1;
		for (int i = 0; i &amp;lt; nums_len; i++) {
			prod *= nums[i];
		}
		return prod;
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Question: Can you think of another way to solve this problem? How else could
we indicate the end of an array?&lt;/p&gt;

&lt;h3 id=&#34;declaration-of-an-array:62db7dc4e96a9a6664437b0b49d20c48&#34;&gt;Declaration of an array&lt;/h3&gt;

&lt;p&gt;Static arrays in C are declared using syntax similar to (but not identical to)
Java&amp;rsquo;s:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    int numbers[4];         // this creates an array of four integers
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Recall that in Java, you must both declare and allocate space for an array
before you use it:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-java&#34;&gt;    int[] numbers = new int[4];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is not the case for the kind of C arrays that we will use in this lab.&lt;/p&gt;

&lt;p&gt;Arrays can also be declared and initialized in one statement as such:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    int a[4] = { 1, 2, 3, 4 };
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Question: What would happen if you tried to read an array location before
writing something to it?&lt;/p&gt;

&lt;p&gt;Question: Can you create an array with a negative size?&lt;/p&gt;

&lt;h3 id=&#34;memory-model:62db7dc4e96a9a6664437b0b49d20c48&#34;&gt;Memory model&lt;/h3&gt;

&lt;p&gt;It is important to note at this point that the C arrays that we will use in this
lab are allocated in a very different way than the arrays in Java.&lt;/p&gt;

&lt;p&gt;Specifically, we are only currently using fixed-sized arrays, which means that
the space for the array can be allocated from a fixed-size chuck of memory that
is reserved for the program when it launches (or when the current function is
executed). This means that we don&amp;rsquo;t have to do any &lt;em&gt;memory management&lt;/em&gt;, but it
also means that we must know exactly how large each array should be in advance.&lt;/p&gt;

&lt;p&gt;In Java, however, arrays were allocated from &lt;em&gt;heap&lt;/em&gt; memory, which is a large
pool of
memory available to a program while it is running. You may not have been aware
of this because Java managed the memory for you. In C, this is not the case,
and you will need to think carefully about how much memory to reserve and when
to reserve it.&lt;/p&gt;

&lt;p&gt;Next week, we will learn how to allocate space for arrays and other data
structures on the heap in C, but for now we will stick with fixed-size arrays.&lt;/p&gt;

&lt;p&gt;We are also glossing over the details of how arrays are passed into functions.
For now, just realize that you can pass arrays into functions by putting &amp;ldquo;&lt;code&gt;[]&lt;/code&gt;&amp;ldquo;
brackets after the parameter name in the function signature:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    int my_function(int numbers[], char characters[])
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Also, we will for now &lt;strong&gt;NOT&lt;/strong&gt; use arrays as return values from functions. Next
week we will learn how to do this.&lt;/p&gt;

&lt;h2 id=&#34;exercise-your-first-foray-into-arrays:62db7dc4e96a9a6664437b0b49d20c48&#34;&gt;Exercise: Your first foray into arrays&lt;/h2&gt;

&lt;p&gt;For our first excercise, let&amp;rsquo;s find the sum of elements in an array of numbers
of type int.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Fill in the code of the &lt;code&gt;sum&lt;/code&gt; function:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    int sum(int nums[], int nums_len) {
        // add your code here
        return 0;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The input to this function is an array of integers and the length of that array.
The output of the function is the sum of all numbers in the array.  Add a call
to &lt;code&gt;sum()&lt;/code&gt; in your &lt;code&gt;main()&lt;/code&gt; function to test it.&lt;/p&gt;

&lt;h2 id=&#34;multidimensional-arrays:62db7dc4e96a9a6664437b0b49d20c48&#34;&gt;Multidimensional arrays&lt;/h2&gt;

&lt;p&gt;The C language provides support for multidimensional arrays.
For instance, we could have an array of two
dimensions to store x and y coordinates of points, or we could have a 3
dimensional array to store Minecraft-like block/cube data.&lt;/p&gt;

&lt;p&gt;The syntax for declaring and accessing multidimensional arrays is &lt;code&gt;array[i][j]&lt;/code&gt;
where &lt;code&gt;i&lt;/code&gt; is the row index and &lt;code&gt;j&lt;/code&gt; is the column index.&lt;/p&gt;

&lt;p&gt;A two-dimensional array of (x,y) points could be created in C as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;	int my_points[4][2] = {
		{11,12},
		{13,14},
		{15,16},
		{17,18}
	};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can also think of multidimensional arrays as &amp;ldquo;array of arrays&amp;rdquo;. In the
example above, the first dimension (&lt;code&gt;[4]&lt;/code&gt;), tells us that we are creating an
array with 4 entries, and the second dimension (&lt;code&gt;[2]&lt;/code&gt;) says that each of those
four entries is itself an array with two entries (an x-coordinate and a
y-coordinate).&lt;/p&gt;

&lt;p&gt;Our first point is (x,y) = (11,12), which is stored at &lt;code&gt;array[0]&lt;/code&gt;. To access
the x-coordinate of this first point you would use &lt;code&gt;my_points[0][0]&lt;/code&gt; and to
access the y-coordinate of the first point, you would use: &lt;code&gt;my_points[0][1]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Similarly, the value of &lt;code&gt;my_points[1][0]&lt;/code&gt; is 13 and the value of
&lt;code&gt;my_points[1][1]&lt;/code&gt; is 14.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt; How would you access the y-coordinate of the third point?&lt;/p&gt;

&lt;h2 id=&#34;exercise-drawing-points:62db7dc4e96a9a6664437b0b49d20c48&#34;&gt;Exercise: Drawing points&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Fill in the code of the &lt;code&gt;draw_points&lt;/code&gt; function:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;	void draw_points(int points[][2], int num_points) {
		// add your code here
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;strong&gt;input&lt;/strong&gt; to &lt;code&gt;draw_points&lt;/code&gt; is a 2D array named &lt;code&gt;points&lt;/code&gt;, containing
&lt;code&gt;num_points&lt;/code&gt; number of points stored in the same way as in the example above.&lt;/p&gt;

&lt;p&gt;As &lt;strong&gt;output&lt;/strong&gt;, your function should draw all of the points in &lt;code&gt;points&lt;/code&gt; using
the &lt;a href=&#34;https://w3.cs.jmu.edu/lam2mo/cs240_2015_08/turtle.html&#34;&gt;turtle graphics
library&lt;/a&gt;. You may use
the &lt;code&gt;turtle_draw_pixel&lt;/code&gt; function, or you may manually move the turtle using
&lt;code&gt;turtle_goto&lt;/code&gt; function and draw the point using the &lt;code&gt;turtle_draw_dot&lt;/code&gt; function.
See the link for documentation on these functions.&lt;/p&gt;

&lt;p&gt;The resulting image should have four dots in a diagonal row, as below:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;../../../../images/array_points.png&#34; alt=&#34;Dots&#34; /&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Recall the following instructions for viewing the turtle graphics output:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IN THE LINUX LAB (ISAT 250):&lt;/strong&gt; Use the following command from the project
folder:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;`eog output.bmp`
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;IN THE MAC LAB (ISAT 248):&lt;/strong&gt; Double-click the bitmap file in Finder or use
the following command in the Terminal from the project folder:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;`open output.bmp`
&lt;/code&gt;&lt;/pre&gt;

&lt;hr /&gt;

&lt;h2 id=&#34;exercise-drawing-line-segments:62db7dc4e96a9a6664437b0b49d20c48&#34;&gt;Exercise: Drawing line segments&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Fill in the code of the &lt;code&gt;draw_segments&lt;/code&gt; function:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;	void draw_segments(int points[][2], int num_points) {
		// add your code here
	}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;strong&gt;input&lt;/strong&gt; to &lt;code&gt;draw_segments&lt;/code&gt; is a 2D array named &lt;code&gt;points&lt;/code&gt; that represents a
list of segment endpoints.  Each consecutive pair of points represents the two
endpoints for a single segment.  Note that because each segment has two
endpoints, the length of &lt;code&gt;points&lt;/code&gt; should be twice the value of &lt;code&gt;num_points&lt;/code&gt;.
As &lt;strong&gt;output&lt;/strong&gt;, your function should draw the segments using turtle graphics.&lt;/p&gt;

&lt;p&gt;For example, if your code is called in the following way:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;	int my_segments[4][2] = {
		{10,40},
		{70,100},
		{50,40},
		{10,80}
	};
	draw_segments(my_segments, 2)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then your code should draw two line segments as below:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;../../../../images/array_line_segments.png&#34; alt=&#34;Line segments&#34; /&gt;&lt;/p&gt;

&lt;h2 id=&#34;c-strings:62db7dc4e96a9a6664437b0b49d20c48&#34;&gt;C strings&lt;/h2&gt;

&lt;p&gt;As with arrays, strings are handled a bit differently (and more low-level) than
in Java.&lt;/p&gt;

&lt;p&gt;In Java a string is stored in an actual object, which gives you access to
various helpful attributes and functions, most notably a &lt;code&gt;.length&lt;/code&gt; attribute for
determining the length of the string. In C, however, a string is just an array
of characters. You can declare and initialize a simple static C string using
code like this:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;	char my_message[] = &amp;quot;C ya, Morehead State!!&amp;quot;;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This defines a string simply as a character array.  Since a string is just an
array, you can access a single character the same way you would an element of an
array, i.e. &lt;code&gt;my_message[6]&lt;/code&gt; will give you the 7th character (which is &lt;code&gt;&#39;M&#39;&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;Note that the quotes are really just &lt;em&gt;syntactic sugar&lt;/em&gt; for a long array of
characters. It is not, in principle, different from:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;	char my_message[22] = {&#39;C&#39;, &#39; &#39;, &#39;y&#39;, &#39;a&#39;, &#39;,&#39;, &#39; &#39;,
        &#39;M&#39;, &#39;o&#39;, &#39;r&#39;, &#39;e&#39;, &#39;h&#39;, &#39;e&#39;, &#39;a&#39;, &#39;d&#39;, &#39; &#39;,
        &#39;S&#39;, &#39;t&#39;, &#39;a&#39;, &#39;t&#39;, &#39;e&#39;, &#39;!&#39;, &#39;!&#39;};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There is one important caveat. Notice that in the second case, as we have
above, we gave the length of the array (22), but in the first case we did not.&lt;/p&gt;

&lt;p&gt;Why? This is because C strings are just character arrays, but by convention they
have &lt;em&gt;one extra special character at the end&lt;/em&gt;. This character is called the
&lt;em&gt;null terminator&lt;/em&gt;, has the value zero, and is indicated in C code using an
escape code: &lt;code&gt;&#39;\0&#39;&lt;/code&gt;. In other words, the first definition for &lt;code&gt;my_message&lt;/code&gt; above
is really equivalent to:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;	char my_message[23] = {&#39;C&#39;, &#39; &#39;, &#39;y&#39;, &#39;a&#39;, &#39;,&#39;, &#39; &#39;,
        &#39;M&#39;, &#39;o&#39;, &#39;r&#39;, &#39;e&#39;, &#39;h&#39;, &#39;e&#39;, &#39;a&#39;, &#39;d&#39;, &#39; &#39;,
        &#39;S&#39;, &#39;t&#39;, &#39;a&#39;, &#39;t&#39;, &#39;e&#39;, &#39;!&#39;, &#39;!&#39;, &#39;\0&#39;};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Why is this helpful? The main reason is that when we are dealing with strings,
unlike arrays, we don&amp;rsquo;t necessarily have to keep track of how long the string
is. We can simply rifle through the string until we find the null terminator
&lt;code&gt;&#39;\0&#39;&lt;/code&gt;. In fact, you can now use this to determine the length of any given
string:&lt;/p&gt;

&lt;h2 id=&#34;exercise-length-of-strings:62db7dc4e96a9a6664437b0b49d20c48&#34;&gt;Exercise: Length of strings&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Fill in the code of the &lt;code&gt;string_length&lt;/code&gt; function:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;int string_length(char my_string[]) {
    // add your code here
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This function takes a character string (&lt;code&gt;char[]&lt;/code&gt;) as input and returns the
length of the string as an &lt;code&gt;int&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&#34;exercise-string-splitting:62db7dc4e96a9a6664437b0b49d20c48&#34;&gt;Exercise: String splitting&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Fill in the code of the &lt;code&gt;tokenize&lt;/code&gt; function:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;void tokenize(char my_string[])
{
    // add your code here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This function takes as input a string made up of words separated by spaces,
(e.g. &amp;ldquo;fifty-six to seven&amp;rdquo;) and prints each word on a new line, e.g.:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fifty-six
to
seven
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The function should print a newline character after each token, even the last
one.&lt;/p&gt;

&lt;p&gt;HINT: Use &lt;code&gt;printf(&amp;quot;%c&amp;quot;, a_string[i])&lt;/code&gt; to print the &lt;code&gt;i&lt;/code&gt;th character of the string
named &lt;code&gt;a_string&lt;/code&gt;. Recall that &lt;code&gt;%c&lt;/code&gt; is the &lt;code&gt;printf&lt;/code&gt; format specifier for a &lt;code&gt;char&lt;/code&gt;
value.&lt;/p&gt;

&lt;h2 id=&#34;string-library-reference:62db7dc4e96a9a6664437b0b49d20c48&#34;&gt;String library reference&lt;/h2&gt;

&lt;p&gt;The standard C library provides many useful functions for working with strings.
Most of these functions are declared in the &lt;code&gt;string.h&lt;/code&gt; header file. Here is a
&lt;a href=&#34;http://www.cplusplus.com/reference/cstring/&#34;&gt;full reference&lt;/a&gt; to these
functions.&lt;/p&gt;

&lt;h2 id=&#34;optional-exercises:62db7dc4e96a9a6664437b0b49d20c48&#34;&gt;Optional exercises&lt;/h2&gt;

&lt;p&gt;&lt;p/&gt;&lt;/p&gt;

&lt;h3 id=&#34;finding-the-minimum-value:62db7dc4e96a9a6664437b0b49d20c48&#34;&gt;Finding the minimum value&lt;/h3&gt;

&lt;p&gt;In this exercise we will explore finding minimum values in an array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1) DO THIS:&lt;/strong&gt; Create a function to return the minimum value of an array of
  numbers. It should have the following signature:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;int min(int nums[], int nums_len)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This should take as &lt;strong&gt;input&lt;/strong&gt; an array of integers called &lt;code&gt;nums&lt;/code&gt;. The
  second parameter, &lt;code&gt;nums_len&lt;/code&gt; is the length of the array.&lt;/p&gt;

&lt;p&gt;Next we want to modify the function to return not just the minimum but
  another like the second smallest or 3rd smallest value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2) DO THIS:&lt;/strong&gt; Create a function that returns the k-th minimum value of an
  array. So for example, calling &lt;code&gt;min_k(nums, n, 2)&lt;/code&gt; should return the 2nd
  smallest element in the array. Here is the function signature:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;int min_k(int nums[], int nums_len, int k)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The input to this function is the array of integers, the length of the
  array, and an integer &lt;code&gt;k&lt;/code&gt; specifying which minimum value to find. For instance
  if &lt;code&gt;k&lt;/code&gt;=3 you should find the third smallest element in &lt;code&gt;nums&lt;/code&gt;. If there are
  duplicate elements, you should only count them once.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3) DO THIS:&lt;/strong&gt; Use the &lt;code&gt;min_k&lt;/code&gt; function to print the array in sorted order.
  Use this signature:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;void print_sorted(int nums[], int nums_len)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The function should print the whole array on a single line, with a space
  character after each number. The function should print a single newline after
  the last number (and its associated space).&lt;/p&gt;

&lt;p&gt;Question: How efficient (or inefficient) is your solution? Can you think of a
  faster way to print a sorted version of the array?&lt;/p&gt;

&lt;h3 id=&#34;triangle-man:62db7dc4e96a9a6664437b0b49d20c48&#34;&gt;Triangle man&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Do This:&lt;/strong&gt; Write a function with the following signature:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;void draw_triangles(int points[][2], int num_points)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Your function should take as &lt;em&gt;input&lt;/em&gt; an array of points; num&lt;em&gt;points is the
number of points. Each three consecutive points represents a triangle.  As
_output&lt;/em&gt; your function should draw each three consecutive points as a triangle.&lt;/p&gt;

&lt;h3 id=&#34;palindromes:62db7dc4e96a9a6664437b0b49d20c48&#34;&gt;Palindromes&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Write a routine with the following signature:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;bool is_palindrome(char word[])
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Your function should determine if the given string is a palindrome word (can be
written the same way forwards and backwards - like &amp;ldquo;madam&amp;rdquo;, &amp;ldquo;otto&amp;rdquo;, &amp;ldquo;anna&amp;rdquo;,
&amp;ldquo;redivider&amp;rdquo;, &amp;ldquo;kayak&amp;rdquo;, and &amp;ldquo;racecar&amp;rdquo;. Empty words and one-character words should
be considered palindromes for the purposes of this function.&lt;/p&gt;

&lt;h2 id=&#34;challenge-exercise-same-things-in-strings:62db7dc4e96a9a6664437b0b49d20c48&#34;&gt;Challenge exercise: Same things in strings&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Write a routine with the following signature:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;int same_things(char string1[], char string2[])
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This function should search through the given strings and determine the longest
set of letters that match. It should return the first index of the longest
match, or -1 if there is no match.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Lab 04</title>
      <link>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240/lab04/</link>
      <pubDate>Mon, 07 Sep 2015 10:55:33 -0400</pubDate>
      
      <guid>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240/lab04/</guid>
      <description>

&lt;h1 id=&#34;structures-in-c:2794d5fdc83eb9ce5aac1da0cf9d3e8d&#34;&gt;Structures in C&lt;/h1&gt;

&lt;p&gt;In this lab you will learn how to define and use C structures.&lt;/p&gt;

&lt;h2 id=&#34;learning-objectives:2794d5fdc83eb9ce5aac1da0cf9d3e8d&#34;&gt;Learning Objectives&lt;/h2&gt;

&lt;p&gt;By the end of this lab, you should be able to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define a &lt;code&gt;struct&lt;/code&gt; for encapsulating data.&lt;/li&gt;
&lt;li&gt;Create new types using &lt;code&gt;enum&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;struct&lt;/code&gt;s and &lt;code&gt;enum&lt;/code&gt;s to make your programs more readable.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;starter-code:2794d5fdc83eb9ce5aac1da0cf9d3e8d&#34;&gt;Starter code&lt;/h2&gt;

&lt;p&gt;The starter code for this lab can be found at the following links:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;../../../../files/lab04.tar.gz&#34;&gt;Tarball (.tar.gz)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;../../../../files/lab04.zip&#34;&gt;Zip file (.zip)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Download the starter code and unzip it. Make sure it compiles by running &lt;code&gt;make&lt;/code&gt;.
You will be editing the &lt;code&gt;main.c&lt;/code&gt; file.&lt;/p&gt;

&lt;h2 id=&#34;your-first-structure:2794d5fdc83eb9ce5aac1da0cf9d3e8d&#34;&gt;Your first structure&lt;/h2&gt;

&lt;p&gt;You already know about primitive data types in C such as &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;char&lt;/code&gt;, etc.
You can create more complex data types using the &lt;code&gt;struct&lt;/code&gt; keyword. A &lt;code&gt;struct&lt;/code&gt;
combines several pieces of data into a single aggregate data structure (thus the
name).&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;struct point {
    int x;
    int y;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt; What does this code remind you of from Java?&lt;/p&gt;

&lt;p&gt;A struct allows us to abstract away some of the underlying memory layout
details, collecting related information inside of a single structure. This is a
form of &lt;em&gt;encapsulation&lt;/em&gt;, a term you may remember from your Java courses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt; What is the benefit of encapsulation?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Fill in the code of the &lt;code&gt;print_point&lt;/code&gt; function; it is a function
that takes a parameter of type &lt;code&gt;struct point&lt;/code&gt;, and prints out the x- and
y-coordinates:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    void print_point(struct point p)
    {
        printf(&amp;quot;Point: (%d, %d)\n&amp;quot;, p.x, p.y);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This code can be tested using this following snippet:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    struct point the_point = {.x = 5, .y = 7};
    print_point(the_point);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Try this yourself. Make sure that it prints what you expect and that you
understand what each piece does.&lt;/p&gt;

&lt;h2 id=&#34;key-differences-from-java:2794d5fdc83eb9ce5aac1da0cf9d3e8d&#34;&gt;Key differences from Java&lt;/h2&gt;

&lt;p&gt;One key difference between a Java class and a C struct is that the struct cannot
directly contain member methods. It can only contain member data. This is why C
is not considered to be object-oriented: we cannot call methods on structs. This
makes the language simpler but more limited.&lt;/p&gt;

&lt;p&gt;Another major difference is that there is no notion of a &amp;ldquo;private&amp;rdquo; member
variable. All variables are publicly-visible by default. It is possible to
define a struct in a header file such that its members are not visible to files
that include that header, but we will not be using such functionality in this
lab.&lt;/p&gt;

&lt;h2 id=&#34;initializing-structs:2794d5fdc83eb9ce5aac1da0cf9d3e8d&#34;&gt;Initializing Structs&lt;/h2&gt;

&lt;p&gt;Note that initializing a struct can be done in multiple ways. It can be done
inline, as was done above:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    struct point the_point = {.x = 5, .y = 7};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here we create a new point with the &lt;code&gt;x&lt;/code&gt; member set to 5 and the &lt;code&gt;y&lt;/code&gt; member set
to 7.&lt;/p&gt;

&lt;p&gt;You can also create the struct without initialization and then modify its data
afterwards, like:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    struct point the_point;
    the_point.x = 5;
    the_point.y = 7;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt; What do you think happens if you do this:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    struct point the_point;
    print_point(the_point);
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;accessing-attributes:2794d5fdc83eb9ce5aac1da0cf9d3e8d&#34;&gt;Accessing Attributes&lt;/h2&gt;

&lt;p&gt;Note that here the dot (&lt;code&gt;.&lt;/code&gt;) syntax is used to access the struct&amp;rsquo;s variables.
These can also be used in computations. For instance, we could define a function
that adds two points together:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;     struct point add(struct point p1, struct point p2)
     {
        // Get the sums of the x and y values:
        int sum_x = p1.x + p2.x;
        int sum_y = p1.y + p2.y;

        // Create a new point with the sum values:
        struct point ret_point;
        ret_point.x = sum_x;
        ret_point.y = sum_y;

        return ret_point;
     }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or, much more succinctly:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    struct point add(struct point p1, struct point p2)
    {
        struct point ret_point = {.x = p1.x + p2.x, .y = p1.y + p2.y};
        return ret_point;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;exercise-drawing-points-revisited:2794d5fdc83eb9ce5aac1da0cf9d3e8d&#34;&gt;Exercise: Drawing points revisited&lt;/h2&gt;

&lt;p&gt;Remember that in the last lab we used 2-dimensional arrays to create a list of
points. This was a little cumbersome from a code-readability standpoint because
when you look at a piece of code that reads &amp;ldquo;&lt;code&gt;points[5][0]&lt;/code&gt;&amp;rdquo; it is not really
clear exactly what the indices are referring to.&lt;/p&gt;

&lt;p&gt;Using structs, we can now make this a bit more readable. We can use our
point type to create an array of points:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    struct point my_points[4] = {
        {.x = 13, .y = 14},
        {.x = 15, .y = 16},
        {.x = 17, .y = 18},
        {.x = 19, .y = 20}
    };
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now, rewrite your &lt;code&gt;draw_points&lt;/code&gt; function from the last lab to use your new type
&lt;code&gt;struct point&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Fill in the code of the &lt;code&gt;draw_points&lt;/code&gt; function:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    void draw_points(struct point points[], int num_points) {
        // add your code here
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;strong&gt;input&lt;/strong&gt; to &lt;code&gt;draw_points&lt;/code&gt; is an array (one dimensional) named &lt;code&gt;points&lt;/code&gt;,
containing &lt;code&gt;num_points&lt;/code&gt; number of points.&lt;/p&gt;

&lt;p&gt;As &lt;strong&gt;output&lt;/strong&gt;, your function should draw all of the points in &lt;code&gt;points&lt;/code&gt; using
the &lt;a href=&#34;https://w3.cs.jmu.edu/lam2mo/cs240_2015_08/turtle.html&#34;&gt;turtle graphics
library&lt;/a&gt;. You may use
the &lt;code&gt;turtle_draw_pixel&lt;/code&gt; function, or you may manually move the turtle using
&lt;code&gt;turtle_goto&lt;/code&gt; function and draw the point using the &lt;code&gt;turtle_draw_dot&lt;/code&gt; function.
See the link for documentation on these functions.&lt;/p&gt;

&lt;p&gt;The resulting image should have four dots in a diagonal row, as below:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;../../../../images/array_points.png&#34; alt=&#34;Dots&#34; /&gt;&lt;/p&gt;

&lt;h3 id=&#34;exercise-distances-between-points:2794d5fdc83eb9ce5aac1da0cf9d3e8d&#34;&gt;Exercise: distances between points&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Fill in the code of the &lt;code&gt;dist&lt;/code&gt; function:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    double dist(struct point p1, struct point p2);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This function takes as input two points (&lt;code&gt;p1&lt;/code&gt; and &lt;code&gt;p2&lt;/code&gt;), calculating and
returning the distance between them as a &lt;code&gt;double&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Hint 1:&lt;/em&gt; Remember that the distance between two points $$(x_1, y_1)$$ and $$(x_2,
y_2)$$ is given by $$\sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}$$&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Hint 2:&lt;/em&gt; The &lt;code&gt;math.h&lt;/code&gt; library file contains a function called &lt;code&gt;sqrt&lt;/code&gt; that takes
an integer (or a double) as a parameter and returns the square root of the
number as a result. For instance &lt;code&gt;sqrt(4)&lt;/code&gt; returns &lt;code&gt;2.0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Check to make sure that the distance between the points in the testing code in
&lt;code&gt;main()&lt;/code&gt; is approximately 305.9.&lt;/p&gt;

&lt;h2 id=&#34;typedefs-for-the-win:2794d5fdc83eb9ce5aac1da0cf9d3e8d&#34;&gt;Typedefs for the win&lt;/h2&gt;

&lt;p&gt;Notice that every time we want to take a struct as a parameter to a function or
define a struct we have to use the &lt;code&gt;struct&lt;/code&gt; keyword. This starts to get
cluttered. For instance, the signature for our &lt;code&gt;add&lt;/code&gt; function above is:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    struct point add(struct point p1, struct point p2);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In Java, this would undoubtedly look a lot prettier, something like:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-java&#34;&gt;    Point add(Point p1, Point p2);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In such cases, the &lt;code&gt;struct&lt;/code&gt; keyword is just cluttering things up. Fortunately C
has a useful tool for dealing with this: the &lt;code&gt;typedef&lt;/code&gt;. The &lt;code&gt;typedef&lt;/code&gt; keyword
allows you to define new types for your program.&lt;/p&gt;

&lt;p&gt;For a silly example, you could do:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    typedef int integer;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That defines a new type called &lt;code&gt;integer&lt;/code&gt;, which is basically just an alias for
&lt;code&gt;int&lt;/code&gt;. After adding that &lt;code&gt;typedef&lt;/code&gt;, you could do this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    integer x = 5;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;More helpfully, programmers sometimes use &lt;code&gt;typedef&lt;/code&gt; to create shorthand versions
of types, for instance, rather than &lt;code&gt;unsigned int&lt;/code&gt; you can use &lt;code&gt;typedef unsigned
int uint;&lt;/code&gt; to use &lt;code&gt;uint&lt;/code&gt; as shorthand for &lt;code&gt;unsigned int&lt;/code&gt;. This sort of
shorthanding also works for for structs.&lt;/p&gt;

&lt;h3 id=&#34;exercise-create-a-point-t-type-using-typedef:2794d5fdc83eb9ce5aac1da0cf9d3e8d&#34;&gt;Exercise: Create a &lt;code&gt;point_t&lt;/code&gt; type using &lt;code&gt;typedef&lt;/code&gt;.&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create a new type called &lt;code&gt;point_t&lt;/code&gt; using &lt;code&gt;typedef&lt;/code&gt; as an alias for &lt;code&gt;struct point&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Convert your code so that it no longer uses &lt;code&gt;struct point&lt;/code&gt; but instead uses
&lt;code&gt;point_t&lt;/code&gt; as the type.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&#34;another-way-to-create-types-enum:2794d5fdc83eb9ce5aac1da0cf9d3e8d&#34;&gt;Another way to create types: &lt;code&gt;enum&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;Another useful method for creating types is with the &lt;code&gt;enum&lt;/code&gt; keyword. The &lt;code&gt;enum&lt;/code&gt;
keyword allows you to create an enumerated type, or rather a type whose values
come from a pre-defined finite list. You can also combine typedefs and enums.&lt;/p&gt;

&lt;p&gt;Consider the following code:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    // Define a new type called fruit_t, which
    // is either APPLES, ORANGES, or BANANAS:
    //
    typedef enum {
        APPLES,
        ORANGES,
        BANANAS
    } fruit_t;

    void print_fruit(fruit_t which_fruit)
    {
        if (which_fruit == APPLES) {
            printf(&amp;quot;Apples!\n&amp;quot;);
        } else if (which_fruit == ORANGES) {
            printf(&amp;quot;Oranges!\n&amp;quot;);
        } else if (which_fruit == BANANAS) {
            printf(&amp;quot;Bananas!\n&amp;quot;);
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;another-shorter-way-to-create-typedef-ed-structs:2794d5fdc83eb9ce5aac1da0cf9d3e8d&#34;&gt;Another (shorter) way to create typedef&amp;rsquo;ed structs&lt;/h2&gt;

&lt;p&gt;You may have noticed in the previous code example that we declare the &lt;code&gt;enum&lt;/code&gt; and
the &lt;code&gt;typedef&lt;/code&gt; in one declaration, rather than two separate ones as we did with
structs. The former syntax is cleaner and avoids any intermediate names for the
enum.  In fact, you can use it for structs as well. Here is our &lt;code&gt;point_t&lt;/code&gt; type
defined using this syntax:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    typedef struct {
        int x;
        int y;
    } point_t;
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;additional-exercises:2794d5fdc83eb9ce5aac1da0cf9d3e8d&#34;&gt;Additional Exercises&lt;/h2&gt;

&lt;p&gt;&lt;p/&gt;&lt;/p&gt;

&lt;h3 id=&#34;drawing-triangles:2794d5fdc83eb9ce5aac1da0cf9d3e8d&#34;&gt;Drawing triangles&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create a struct called &lt;code&gt;triangle_t&lt;/code&gt; for storing triangles. Each triangle
should store three points: &lt;code&gt;p1&lt;/code&gt;, &lt;code&gt;p2&lt;/code&gt;, &lt;code&gt;p3&lt;/code&gt;, which are its endpoints.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Write a function called &lt;code&gt;draw_triangle&lt;/code&gt;, which takes as input a &lt;code&gt;triangle_t&lt;/code&gt;
and then draws the triangle using the turtle graphics library.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;In your &lt;code&gt;main()&lt;/code&gt; function, create a triangle with corners at (0,0), (100,75),
(-60,80) using your new &lt;code&gt;triangle_t&lt;/code&gt; type and draw it.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Your output should look like this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;../../../../images/triangle.png&#34; alt=&#34;A triangle&#34; /&gt;&lt;/p&gt;

&lt;h3 id=&#34;icons:2794d5fdc83eb9ce5aac1da0cf9d3e8d&#34;&gt;Icons&lt;/h3&gt;

&lt;p&gt;For this exercise you are going to create an &lt;code&gt;icon_t&lt;/code&gt; type to define icons. An
icon will have a shape (either CIRCLE, SQUARE, or TRIANGLE), a dimension (width
x height). You will then write a &lt;code&gt;draw_icons&lt;/code&gt; function that takes as input an
array of of &lt;code&gt;icon_t&lt;/code&gt;s and draws the icons along a row in the image.&lt;/p&gt;

&lt;p&gt;Your tasks:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Define a new enumerated type called &lt;code&gt;shapetype_t&lt;/code&gt; which can be either CIRCLE,
SQUARE, or TRIANGLE. (Hint: use &lt;code&gt;enum&lt;/code&gt;.)&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Define a new type called &lt;code&gt;icon_t&lt;/code&gt; which stores a &lt;code&gt;size_t&lt;/code&gt; called &lt;code&gt;size&lt;/code&gt; and a
&lt;code&gt;shapetype_t&lt;/code&gt; called &lt;code&gt;icon_type&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Define a function called &lt;code&gt;draw_icons&lt;/code&gt; which takes as input an array of icons
and the number of icons in the array and a starting (x, y) position and
draws them in a row (according to their &lt;code&gt;icon_type&lt;/code&gt;) using the turtle graphics
library. Hint: the function&amp;rsquo;s signature should be:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;void draw_icons(icon_t icons[], int num_icons, int start_x, int start_y)&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;In your &lt;code&gt;main()&lt;/code&gt; function add code to create an array called &lt;code&gt;icons&lt;/code&gt; which
has at least one icon of each type. Use your &lt;code&gt;draw_icons&lt;/code&gt; function to draw
the icons in the array.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Your output should look something like this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;../../../../images/row_of_shapes.png&#34; alt=&#34;A row of seven icons&#34; /&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Lab 05</title>
      <link>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240/lab05/</link>
      <pubDate>Mon, 14 Sep 2015 10:55:33 -0400</pubDate>
      
      <guid>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240/lab05/</guid>
      <description>

&lt;h1 id=&#34;testing-in-c:a87a12c8c675422b4d8d4db87e64becd&#34;&gt;Testing in C&lt;/h1&gt;

&lt;p&gt;In this lab, you will learn how to use a testing framework in C.&lt;/p&gt;

&lt;p&gt;Objectives:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Describe the benefits of automated testing&lt;/li&gt;
&lt;li&gt;Assemble a test suite using the Check software package&lt;/li&gt;
&lt;li&gt;Add and modify tests in a test suite&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;introduction:a87a12c8c675422b4d8d4db87e64becd&#34;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;You may be familiar with the &lt;a href=&#34;http://junit.org/&#34;&gt;JUnit&lt;/a&gt; framework from Java.
JUnit allows you to write automated tests for your code. In this course, we
will be using &lt;a href=&#34;http://check.sourceforge.net/&#34;&gt;Check&lt;/a&gt;, which is a C-based
testing framework similar to JUnit.&lt;/p&gt;

&lt;p&gt;Throughout this lab, we will be testing implementations of the following
function:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    // takes an integer and returns the next prime number
    // that is greater than the parameter
    //
    int next_prime(int x)
    {
        return 0;   // TODO: finish this
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You should take a few minutes to consider how you would implement this
function.&lt;/p&gt;

&lt;h2 id=&#34;lab-setup:a87a12c8c675422b4d8d4db87e64becd&#34;&gt;Lab setup&lt;/h2&gt;

&lt;p&gt;Here are the starter files for this project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;../../../../files/lab05.tar.gz&#34;&gt;Tarball (.tar.gz)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;../../../../files/lab05.zip&#34;&gt;Zip file (.zip)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Download one of the archives and decompress it to a working folder. You will
find three code modules (&lt;code&gt;*.c&lt;/code&gt; files):&lt;/p&gt;

&lt;p&gt;1)  &lt;code&gt;main.c&lt;/code&gt; - the &lt;code&gt;main()&lt;/code&gt; program
2)  &lt;code&gt;primes.c&lt;/code&gt; - the implementation of the &lt;code&gt;next_prime&lt;/code&gt; function
3)  &lt;code&gt;testsuite.c&lt;/code&gt; - the automated tests&lt;/p&gt;

&lt;p&gt;You will also find &lt;code&gt;primes.h&lt;/code&gt;, a header file for the &lt;code&gt;primes.c&lt;/code&gt; module.&lt;/p&gt;

&lt;p&gt;Because we will be using the Check framework, it is time to begin using the
generic makefiles that we have posted on Piazza and/or on your course website.
Download the makefile that is appropriate for the system you are working on and
put it in the same folder as the other lab files, renaming it to simply
&lt;code&gt;Makefile&lt;/code&gt; (note the capitalization).&lt;/p&gt;

&lt;p&gt;You will need to customize the generic makefile for each project. For this
project, we will need to add a module target for the &lt;code&gt;primes&lt;/code&gt; module to the
&lt;code&gt;MODS&lt;/code&gt; variable in the makefile. Find the appropriate line and add &lt;code&gt;primes.o&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    # application-specific settings and run target

    EXE=main
    TEST=testsuite
    MODS=primes.o
    LIBS=

    ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;IMPORTANT:&lt;/strong&gt; Even though you are adding an entry for the &lt;code&gt;primes.c&lt;/code&gt; code file,
you need to put &lt;code&gt;primes.o&lt;/code&gt; (with the &amp;ldquo;&lt;code&gt;.o&lt;/code&gt;&amp;rdquo; extension) in the makefile. Recall
that the &lt;code&gt;make&lt;/code&gt; build process runs on &lt;em&gt;target&lt;/em&gt; information; i.e., a list of
things that need to be built. All you need to do is tell &lt;code&gt;make&lt;/code&gt; what you want to
build, and the generic makefile has rules and patterns to help &lt;code&gt;make&lt;/code&gt; figure out
how to do it.&lt;/p&gt;

&lt;p&gt;Also note that we didn&amp;rsquo;t have to include &lt;code&gt;main.o&lt;/code&gt; and &lt;code&gt;testsuite.o&lt;/code&gt; in that
list. We only have to list any &lt;strong&gt;extra&lt;/strong&gt; code objects beyond the basic &lt;code&gt;main.o&lt;/code&gt;
and &lt;code&gt;testsuite.o&lt;/code&gt; objects, which are already accounted for elsewhere in the
generic makefile.&lt;/p&gt;

&lt;p&gt;Test the build with &lt;code&gt;make&lt;/code&gt; to ensure that everything is set up properly. You
will need to use a similar process to set up all of the programming assignments
in this course.&lt;/p&gt;

&lt;h2 id=&#34;part-1-manual-testing:a87a12c8c675422b4d8d4db87e64becd&#34;&gt;Part 1: Manual testing&lt;/h2&gt;

&lt;p&gt;In this lab, we will alternate between improving our tests and improving our
code. This resembles a formal software development process called &lt;a href=&#34;https://en.wikipedia.org/wiki/Test-driven_development&#34;&gt;&lt;em&gt;test-driven
development&lt;/em&gt;&lt;/a&gt; (TDD).  The
idea behind TDD is that you should write tests for a program &lt;strong&gt;before&lt;/strong&gt; you
write the program itself. This requires you to think critically about the
expected behavior of your program even before you begin to code it.&lt;/p&gt;

&lt;p&gt;After you write some initial tests, you then write the minimal amount of code
necessary to pass the test. After you have fixed any problems and all of the
current tests are passing, you write more tests and then more code. Gradually,
you will build up a large list of tests that help you verify that new changes
don&amp;rsquo;t break older features. This technique is also sometimes called the &amp;ldquo;test a
little, code a little&amp;rdquo; strategy, and is a recommended approach for tackling the
programming assignments in this class.&lt;/p&gt;

&lt;p&gt;For this lab, we will (over the course of several exercises) gradually build up
a &lt;em&gt;suite&lt;/em&gt; of tests for the &lt;code&gt;next_prime&lt;/code&gt; function. This suite will be tightly
integrated with our build process (&lt;code&gt;make&lt;/code&gt;) and thus it will be very easy to
check that we haven&amp;rsquo;t broken anything if we ever have to make changes to the
code in the future.&lt;/p&gt;

&lt;h3 id=&#34;exercise-1-ad-hoc-tests:a87a12c8c675422b4d8d4db87e64becd&#34;&gt;Exercise 1: Ad-hoc tests&lt;/h3&gt;

&lt;p&gt;Let&amp;rsquo;s write a test for &lt;code&gt;next_prime&lt;/code&gt;. We&amp;rsquo;ll start by writing an ad-hoc test in
the &lt;code&gt;main()&lt;/code&gt; function. This is a very rudimentary method of testing, but it is
quick and simple.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Add the following code to your &lt;code&gt;main()&lt;/code&gt; function:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    if (next_prime(2) == 3) {
        printf(&amp;quot;x=2: correct\n&amp;quot;);
    } else {
        printf(&amp;quot;x=2: incorrect\n&amp;quot;);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This test encodes the following specification: &amp;ldquo;when given the number 2, the
&lt;code&gt;next_prime&lt;/code&gt; function should return 3.&amp;rdquo; This is a concrete, testable statement
that follows from the original function specification, which was the somewhat
hard-to-test notion that the function &amp;ldquo;takes an integer and returns the next
prime number that is greater than the parameter.&amp;rdquo; When writing tests, you should
always begin with the project specification and work towards concrete, testable
test cases that are easy to code.&lt;/p&gt;

&lt;p&gt;Compile and run the &lt;code&gt;main&lt;/code&gt; program and see that the test fails.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Fix the code to make the test pass. For now, just change &lt;code&gt;return
0;&lt;/code&gt; in &lt;code&gt;next_prime&lt;/code&gt; to &lt;code&gt;return x+1;&lt;/code&gt;. This is a minimal change necessary to
make the given test pass.&lt;/p&gt;

&lt;p&gt;Hopefully you can see that this does not fully implement the project
specification for &lt;code&gt;next_prime&lt;/code&gt;. Later, we will need a better implementation.
This also exposes the inadequacy of our current test; it now passes, but that
doesn&amp;rsquo;t mean that the code matches the spec.&lt;/p&gt;

&lt;h3 id=&#34;exercise-2-function-based-tests:a87a12c8c675422b4d8d4db87e64becd&#34;&gt;Exercise 2: Function-based tests&lt;/h3&gt;

&lt;p&gt;Ad-hoc tests in a &lt;code&gt;main()&lt;/code&gt; function are quick to implement, but we want to be
able to add lots of tests. If we have to duplicate all the code for a test every
time we add a new one, we will end up with a very hard-to-maintain test suite.&lt;/p&gt;

&lt;p&gt;We can improve the situation by extracting the test behavior into a separate
function. Let&amp;rsquo;s do that by adding a new &lt;code&gt;test_next_prime&lt;/code&gt; function:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Move the testing code from &lt;code&gt;main()&lt;/code&gt; into the &lt;code&gt;test_next_prime&lt;/code&gt;
function, which takes two parameters: a test input value and an expected test
result value.  This function should test &lt;code&gt;next_prime&lt;/code&gt; with the given input value
and check it against the expected result value. Thus, this function should work
for testing any integer input. You will need to &lt;em&gt;parameterize&lt;/em&gt; the code from the
last section by changing the constants (2 and 3) to variable references.&lt;/p&gt;

&lt;p&gt;Then, add the following tests to the &lt;code&gt;main()&lt;/code&gt; function:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    test_next_prime(2, 3);
    test_next_prime(3, 5);
    test_next_prime(5, 7);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Compile and run to verify that the first test passes but the latter two fail.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Fix the code for &lt;code&gt;next_prime&lt;/code&gt; so that all three tests pass. Here is
an easy fix that should cause your implementation to pass all three tests:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    if (x == 2) {
        return x+1;
    } else {
        return x+2;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Again, hopefully you can see the futility of coding to an incomplete test suite.
Even though we have already &amp;ldquo;fixed&amp;rdquo; the function twice, we are not much closer
to a true implementation of the project specification. This is why it is so
important to have a comprehensive test suite.&lt;/p&gt;

&lt;h2 id=&#34;part-2-automated-testing:a87a12c8c675422b4d8d4db87e64becd&#34;&gt;Part 2: Automated testing&lt;/h2&gt;

&lt;p&gt;Thus far, we have confined our tests to ad-hoc and function-based tests in the
main program module. However, it is much more robust to separate a program&amp;rsquo;s
tests into a separate module, enabling them to be maintained, compiled, and
managed separately. This will also allow us to handle test crashes gracefully,
and to use the &lt;code&gt;main()&lt;/code&gt; function for actual program code.&lt;/p&gt;

&lt;p&gt;As mentioned before, we will be using the &lt;a href=&#34;http://check.sourceforge.net/&#34;&gt;Check&lt;/a&gt;
framework for tests in this course. This will allow us to take advantage of
robust testing code that someone else has written, meaning there is less work
for us!&lt;/p&gt;

&lt;p&gt;For this course, we will always put our test suites in a separate source code
file that we will call &lt;code&gt;testsuite.c&lt;/code&gt;. We have included a basic testsuite
template in the starter files for this lab. Load that file now and look through
it.&lt;/p&gt;

&lt;p&gt;Actual tests are enclosed between &lt;code&gt;START_TEST&lt;/code&gt; and &lt;code&gt;END_TEST&lt;/code&gt; markers. These
are actually &lt;em&gt;macros&lt;/em&gt;, a copy/paste mechanism similar to the &lt;code&gt;#include&lt;/code&gt;
directive that we have seen already. The Check framework replaces these macros
with code that runs the test, handles any crashes, and records the result.
Every test is named, and the name is declared as a parameter to the
&lt;code&gt;START_TEST&lt;/code&gt; macro.&lt;/p&gt;

&lt;p&gt;Inside of these tests, Check provides many convenience functions for actually
performing tests. Here are the most important:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;ck_assert(expr)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Evaluate &lt;code&gt;expr&lt;/code&gt;; the check passes if the result is true and fails if it is
false.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;ck_assert_int_eq(x, y)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The check passes if the two integers are equal and fails if they are not.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;ck_assert_str_eq(x, y)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The check passes if the two character strings are equal (according to the
&lt;code&gt;strcmp&lt;/code&gt; function) and fails if they are not.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- Lam: I think we shouldn&#39;t encourage them to use &#34;ck_abort&#34; because it will
make the total number of checks unpredictable

* `ck_abort()`

    Unconditionally fail the check. This is useful if you need to encode a more
    complicated check than is possible using the other convenience functions. You
    can write your check in a standard `if` structure, and then put a call to
    `ck_abort()` in all of the failure branches.
--&gt;

&lt;p&gt;Here is a
&lt;a href=&#34;http://check.sourceforge.net/doc/doxygen/html/check_8h.html&#34;&gt;reference&lt;/a&gt; to all
of the checking functions provided by our testing framework.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IMPORTANT:&lt;/strong&gt; Every test also needs to be registered with the testing
framework using the &lt;code&gt;tcase_add_test&lt;/code&gt; function; in our course framework, this is
done in the &lt;code&gt;test_suite&lt;/code&gt; function.&lt;/p&gt;

&lt;h3 id=&#34;optional-peeking-behind-the-curtain:a87a12c8c675422b4d8d4db87e64becd&#34;&gt;(Optional) Peeking behind the curtain&lt;/h3&gt;

&lt;p&gt;Read this section if you&amp;rsquo;re curious about what the rest of the code in
&lt;code&gt;testsuite.c&lt;/code&gt; is doing; however, it is not strictly necessary to understand the
content in this section in order to use the framework.&lt;/p&gt;

&lt;p&gt;The test suite module (&lt;code&gt;testsuite.c&lt;/code&gt;) contains two major framework functions:
&lt;code&gt;test_suite()&lt;/code&gt; and &lt;code&gt;run_testsuite()&lt;/code&gt;. The former is responsible for building a
&lt;code&gt;Suite&lt;/code&gt; object that contains a list of all the tests in the test suite, and the
latter is responsible for running the &lt;code&gt;Suite&lt;/code&gt; and printing the results. The file
also contains a &lt;code&gt;main()&lt;/code&gt;, but it just calls &lt;code&gt;run_testsuite&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here is the code for the &lt;code&gt;test_suite()&lt;/code&gt; function:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;Suite * test_suite(void)
{
    Suite *s;
    TCase *tc_core;
    s = suite_create(&amp;quot;Default&amp;quot;);
    tc_core = tcase_create(&amp;quot;Core&amp;quot;);

    tcase_add_test(tc_core, TEST_NAME);

    suite_add_tcase(s, tc_core);
    return s;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The first four lines create &lt;code&gt;Suite&lt;/code&gt; and &lt;code&gt;TCase&lt;/code&gt; structures and initialize them.
These structs are defined in the Check framework files (which are not part of
our project).  A &lt;code&gt;Suite&lt;/code&gt; is a collection of &lt;code&gt;TCase&lt;/code&gt; objects, and a &lt;code&gt;TCase&lt;/code&gt; is a
collection of actual test functions, which by convention should begin with the
prefix &amp;ldquo;&lt;code&gt;test_&lt;/code&gt;&amp;rdquo;. The test functions themselves may contain multiple &lt;em&gt;checks&lt;/em&gt;
using the convenience functions (e.g., &lt;code&gt;ck_assert_int_eq&lt;/code&gt;).  Thus, there is a
hierarchical, container-based relationship between all of these structures:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    + Suite struct
      + TCase struct
        + &amp;quot;test_*&amp;quot; function
          + individual &amp;quot;ck_assert_*&amp;quot; checks
        + &amp;quot;test_*&amp;quot; function
          + individual &amp;quot;ck_assert_*&amp;quot; checks
        + &amp;quot;test_*&amp;quot; function
          + individual &amp;quot;ck_assert_*&amp;quot; checks
        ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For simplicity, we will only use a single Suite and TCase for all of the test
suites in this course. We will separate tests into multiple &amp;ldquo;&lt;code&gt;test_*&lt;/code&gt;&amp;ldquo;
functions, each of which should have a small number of individual checks
(usually just one).&lt;/p&gt;

&lt;p&gt;The only modification that you should ever need to make to the &lt;code&gt;test_suite&lt;/code&gt;
function is to add each new test case using a call to the &lt;code&gt;tcase_add_test&lt;/code&gt;
function.&lt;/p&gt;

&lt;p&gt;Here is the code for the &lt;code&gt;run_testsuite()&lt;/code&gt; function:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;int run_testsuite()
{
    int number_failed;
    Suite *s;
    SRunner *sr;

    s = test_suite();
    sr = srunner_create(s);

    srunner_run_all(sr, CK_NORMAL);
    number_failed = srunner_ntests_failed(sr);
    srunner_free(sr);

    if (number_failed == 0) {
        printf(&amp;quot;SUCCESS: All current tests passed!\n&amp;quot;);
    } else {
        printf(&amp;quot;FAILURE: At least one test failed or crashed.\n&amp;quot;);
    }

    return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The first three lines allocate some variables, including a &lt;code&gt;Suite&lt;/code&gt; struct (which
is initialized in the fourth line using the &lt;code&gt;test_suite()&lt;/code&gt; function described
above) and an &lt;code&gt;SRunner&lt;/code&gt; struct (which manages the actual execution of tests.
These variables are initialized, and then the runner is executed using the
&lt;code&gt;srunner_run_all&lt;/code&gt; function. The Check library is then polled to see how many
tests failed, and a summary message is printed. Finally, the function returns an
exit value to indicate whether or not all of the tests passed.&lt;/p&gt;

&lt;p&gt;You should not need to modify the &lt;code&gt;run_testsuite&lt;/code&gt; function in this course.&lt;/p&gt;

&lt;p&gt;Finally, if you look through the generic makefile, you will see a separate
&lt;code&gt;CFLAGS&lt;/code&gt; and &lt;code&gt;LIBS&lt;/code&gt; variables for compiling the test suite:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;TESTCFLAGS=$(CFLAGS) -Wno-gnu-zero-variadic-macro-arguments
TESTLIBS=-lcheck
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;-Wno-gnu-zero-variadic-macro-arguments&lt;/code&gt; flag suppresses a particular
(harmless) warning that is produced when compiling the Check header files, and
the &lt;code&gt;-lcheck&lt;/code&gt; flag is what tells the linker to include the Check library when it
builds the final executable.&lt;/p&gt;

&lt;h3 id=&#34;exercise-3-automated-test:a87a12c8c675422b4d8d4db87e64becd&#34;&gt;Exercise 3: Automated test&lt;/h3&gt;

&lt;p&gt;Let&amp;rsquo;s re-implement our original ad-hoc test using the test framework.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Inside the &lt;code&gt;test_prime_2&lt;/code&gt; test case, add a check that the result of
calling &lt;code&gt;next_prime(2)&lt;/code&gt; is 3:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;START_TEST (test_prime_2)
{
    ck_assert_int_eq(next_prime(2), 3);
}
END_TEST
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is much simpler than our previous code! We could also reformat the test to
be even more compact:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;START_TEST (test_prime_2)
{ ck_assert_int_eq(next_prime(2), 3); }
END_TEST
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Such formatting would not be very readable for normal functions, but for simple
test cases it is occasionally useful to be able to encode a large number of
tests in a compact form.&lt;/p&gt;

&lt;p&gt;In our generic Makefile, we provide a separate build target for automated
tests: &lt;code&gt;test&lt;/code&gt;. To compile and run the tests, just run &lt;code&gt;make test&lt;/code&gt;. Verify that
the test suite builds and runs. The output should look something like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Running suite(s): Default
100%: Checks: 1, Failures: 0, Errors: 0
SUCCESS: All current tests passed!
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This output tells you how many checks were run, how many failures and errors
were encountered, and what percentage of the tests passed. The last line is a
general at-a-glance summary of the results.&lt;/p&gt;

&lt;h3 id=&#34;exercise-4-more-tests:a87a12c8c675422b4d8d4db87e64becd&#34;&gt;Exercise 4: More tests&lt;/h3&gt;

&lt;p&gt;Now that it is easy to add more tests, let&amp;rsquo;s add a &lt;strong&gt;lot&lt;/strong&gt; more.&lt;/p&gt;

&lt;p&gt;In particular, we should try to anticipate &lt;a href=&#34;https://en.wikipedia.org/wiki/Edge_case&#34;&gt;&lt;em&gt;edge
cases&lt;/em&gt;&lt;/a&gt;, which are input values that
lie on the border of acceptable values. To find edge cases, think about what
sorts of input values make sense for any particular function, and choose values
that are unexpected or extreme.  For example, we know that two is the smallest
prime number, so we need to think about the behavior of our function for values
smaller than two. what is the next prime of the value one?  Zero? A negative
number?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Add more tests for the following cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;next_prime(3)&lt;/code&gt; should be 5&lt;/li&gt;
&lt;li&gt;&lt;code&gt;next_prime(5)&lt;/code&gt; should be 7&lt;/li&gt;
&lt;li&gt;&lt;code&gt;next_prime(7)&lt;/code&gt; should be 11&lt;/li&gt;
&lt;li&gt;&lt;code&gt;next_prime(14)&lt;/code&gt; should be 17&lt;/li&gt;
&lt;li&gt;&lt;code&gt;next_prime(42000)&lt;/code&gt; should be 42013&lt;/li&gt;
&lt;li&gt;&lt;code&gt;next_prime(1)&lt;/code&gt; should be 2&lt;/li&gt;
&lt;li&gt;&lt;code&gt;next_prime(0)&lt;/code&gt; should be 2&lt;/li&gt;
&lt;li&gt;&lt;code&gt;next_prime(-1)&lt;/code&gt; should be 2&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;IMPORTANT:&lt;/strong&gt; For each test you add, make sure you remember to add a
corresponding call to &lt;code&gt;tcase_add_test&lt;/code&gt; in the &lt;code&gt;test_suite&lt;/code&gt; function. Always
verify that the number of checks run by the test suite matches your
expectations.&lt;/p&gt;

&lt;p&gt;Re-run the test suite. Which tests pass and which tests fail?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Write a proper implementation for the &lt;code&gt;next_prime&lt;/code&gt; function. Does
your implementation pass all the tests?&lt;/p&gt;

&lt;h2 id=&#34;pa-0:a87a12c8c675422b4d8d4db87e64becd&#34;&gt;PA 0&lt;/h2&gt;

&lt;p&gt;Don&amp;rsquo;t forget that PA 0 is due today. Hopefully you have already completed it;
if not, make sure you do so. You may download the private test suite for this
PA (and &lt;strong&gt;ONLY&lt;/strong&gt; this PA) from Canvas. Just overwrite the old &lt;code&gt;testsuite.c&lt;/code&gt;
with the new one.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Lab 06</title>
      <link>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240/lab06/</link>
      <pubDate>Fri, 11 Sep 2015 10:55:33 -0400</pubDate>
      
      <guid>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240/lab06/</guid>
      <description>

&lt;h1 id=&#34;pointers-in-c:62945700b0816b43d60c385adb103445&#34;&gt;Pointers in C&lt;/h1&gt;

&lt;p&gt;In this lab, you will gain experience with pointer declaration and manipulation
in C.&lt;/p&gt;

&lt;p&gt;Objectives:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define a &amp;ldquo;pointer&amp;rdquo; as used in C and explain dereferencing&lt;/li&gt;
&lt;li&gt;Write code to declare and use pointers&lt;/li&gt;
&lt;li&gt;Distinguish between data and pointers in existing code&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;malloc&lt;/code&gt; and &lt;code&gt;free&lt;/code&gt; functions to manage heap memory&lt;/li&gt;
&lt;li&gt;Understand the link between arrays and pointers&lt;/li&gt;
&lt;li&gt;Use pointers to allocate and access arrays on the heap&lt;/li&gt;
&lt;li&gt;Use pointers to allocate and access structs on the heap&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;setup:62945700b0816b43d60c385adb103445&#34;&gt;Setup&lt;/h2&gt;

&lt;p&gt;Begin this lab with an empty &lt;code&gt;main.c&lt;/code&gt; and the standard makefile for your
operating system. You will want to add the following includes at the top of your
file:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;#include &amp;lt;stdbool.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Of course, you will also want a basic definition of the &lt;code&gt;main&lt;/code&gt; function. You
should follow along with the code examples in this lab, adding them to your
&lt;code&gt;main&lt;/code&gt; function and testing them.&lt;/p&gt;

&lt;h2 id=&#34;review:62945700b0816b43d60c385adb103445&#34;&gt;Review&lt;/h2&gt;

&lt;p&gt;As discussed in the first lecture, computer memory consists of a large bank of
bytes. Every byte has a unique address. Until now, we have only been referring
to a location in memory by a variable name:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    int x;
    x = 5;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The first line allocates space for an integer (usually 4 or 8 bytes), and gives
it a name: &amp;ldquo;x&amp;rdquo;. In the second line, we store the value &lt;code&gt;5&lt;/code&gt; in the memory
location referenced by the name &lt;code&gt;x&lt;/code&gt;. The association between &lt;code&gt;x&lt;/code&gt; and the
location never changes; i.e., &lt;code&gt;x&lt;/code&gt; cannot be changed to reference a different
location in memory.&lt;/p&gt;

&lt;p&gt;You can see the actual address of the variable &lt;code&gt;x&lt;/code&gt; using the &amp;ldquo;address-of&amp;rdquo;
operator (&lt;code&gt;&amp;amp;&lt;/code&gt;):&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    printf(&amp;quot;Variable x is located at %p\n&amp;quot;, &amp;amp;x);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This uses the &lt;code&gt;%p&lt;/code&gt; format specifier, which prints addresses in hexadecimal. The
above code should print a slightly different address each time you run it,
because the variable &lt;code&gt;x&lt;/code&gt; will be assigned to different places in memory during
each execution.&lt;/p&gt;

&lt;h2 id=&#34;pointers:62945700b0816b43d60c385adb103445&#34;&gt;Pointers&lt;/h2&gt;

&lt;p&gt;We are now going to introduce a major new C concept: &lt;em&gt;pointers&lt;/em&gt;. &lt;strong&gt;A pointer is
a variable that holds a memory address.&lt;/strong&gt; Pointers are declared using the
asterisk (&lt;code&gt;*&lt;/code&gt;) operator:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    int *p;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In the above example, the type of the variable &lt;code&gt;p&lt;/code&gt; is &lt;code&gt;int*&lt;/code&gt; (usually pronounced
&amp;ldquo;int star&amp;rdquo; or &amp;ldquo;int pointer&amp;rdquo;). Like other variables, it is a fixed reference to
an allocated place in memory. &lt;strong&gt;Unlike other variables&lt;/strong&gt;, that memory location
itself holds the address of another place in memory. That is what makes it a
&amp;ldquo;pointer;&amp;rdquo; it &amp;ldquo;points&amp;rdquo; to another location.&lt;/p&gt;

&lt;p&gt;Until it is initialized, however, it doesn&amp;rsquo;t point to anything in particular.
To avoid a lot
of headaches later on, pointers should always be intialized when they are
declared. If you don&amp;rsquo;t know what it should point to immediately, you can always
initialize it to a special value (zero) using the &lt;code&gt;NULL&lt;/code&gt; constant:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    int *p = NULL;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Question: How would we initialize &lt;code&gt;p&lt;/code&gt; so that it points to &lt;code&gt;x&lt;/code&gt;?&lt;/p&gt;

&lt;!-- p = &amp;x;    (see below) --&gt;

&lt;p&gt;&lt;strong&gt;WARNING:&lt;/strong&gt; If you wish to declare multiple pointers on the same line, you
&lt;strong&gt;must&lt;/strong&gt; include an asterisk with each of them. For instance:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    int *p = NULL, *q = NULL;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In this course, we strongly recommend &lt;strong&gt;against&lt;/strong&gt; declaring multiple pointers
on a single line.&lt;/p&gt;

&lt;p&gt;To assign pointers so that they point to existing variables, you can use the
address-of operator (&lt;code&gt;&amp;amp;&lt;/code&gt;) just as we used it earlier to print the address:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    p = &amp;amp;x;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;After this line, &lt;code&gt;p&lt;/code&gt; is now a pointer to the location that stores the value
associated with the variable &lt;code&gt;x&lt;/code&gt;. Note that &lt;code&gt;x&lt;/code&gt; is &lt;strong&gt;NOT&lt;/strong&gt; a pointer; it is just
a variable name.&lt;/p&gt;

&lt;p&gt;You can now print the value of &lt;code&gt;p&lt;/code&gt; (the pointer) and verify that it is the same
as the address of &lt;code&gt;x&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    printf(&amp;quot;Pointer value of p is %p\n&amp;quot;, p);
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;dereferencing-pointers:62945700b0816b43d60c385adb103445&#34;&gt;Dereferencing pointers&lt;/h2&gt;

&lt;p&gt;To retrieve the value that a pointer points to, use the &amp;ldquo;dereference&amp;rdquo; operator
(&lt;code&gt;*&lt;/code&gt;). Yes, it&amp;rsquo;s an asterisk, the same as we used to declare the pointer.&lt;/p&gt;

&lt;p&gt;In the running example, we know that &lt;code&gt;p&lt;/code&gt; is an &lt;code&gt;int*&lt;/code&gt; (an &amp;ldquo;int-pointer&amp;rdquo; or a
&amp;ldquo;pointer to an int&amp;rdquo;). We have also initialized it to point to the location of
variable &lt;code&gt;x&lt;/code&gt;. Thus, we can print the integer that &lt;code&gt;p&lt;/code&gt; points to using
the following code:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    printf(&amp;quot;Dereferenced value of p is %d, and x = %d\n&amp;quot;, *p, x);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Question: What would happen if we passed &lt;code&gt;p&lt;/code&gt; instead of &lt;code&gt;*p&lt;/code&gt; to &lt;code&gt;printf&lt;/code&gt;?&lt;/p&gt;

&lt;!-- compiler warning, and it prints &#34;random&#34; integers (decimal encoding of the
pointer) --&gt;

&lt;p&gt;Question: What would happen if we dereferenced a pointer that had the value of
&lt;code&gt;NULL&lt;/code&gt;?&lt;/p&gt;

&lt;!-- a segmentation fault, which is the C equivalent of a &#34;null pointer
exception&#34; in Java --&gt;

&lt;h2 id=&#34;exercise-tracing:62945700b0816b43d60c385adb103445&#34;&gt;Exercise: Tracing&lt;/h2&gt;

&lt;p&gt;What does the following code print? Step through it as if you were the machine.
This is sometimes referred to as &amp;ldquo;tracing&amp;rdquo; the code.  You may find it helpful to
draw diagrams with boxes for memory locations and arrows for pointers.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    int a = 42;
    int b = 7;
    int c = 999;
    int *t = &amp;amp;a;
    int *u = NULL;
    printf(&amp;quot;%d %d\n&amp;quot;, a, *t);

    c = b;
    u = t;
    printf(&amp;quot;%d %d\n&amp;quot;, c, *u);

    a = 8;
    b = 8;
    printf(&amp;quot;%d %d %d %d\n&amp;quot;, b, c, *t, *u);

    *t = 123;
    printf(&amp;quot;%d %d %d %d %d\n&amp;quot;, a, b, c, *t, *u);
&lt;/code&gt;&lt;/pre&gt;

&lt;!--
    42 42
    7 42
    8 7 8 8
    123 8 7 123 123
--&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Copy the previous code into your file and test it. Then, add more
lines of code to do the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Update &lt;code&gt;t&lt;/code&gt; to point to &lt;code&gt;c&lt;/code&gt;. Use a pointer dereference to change the value of
&lt;code&gt;c&lt;/code&gt; to &lt;code&gt;555&lt;/code&gt;. Verify that it worked by adding a printout. Does this change
any of the other values?&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Change the value of &lt;code&gt;c&lt;/code&gt; again using a direct assignment. Verify that the
pointer &lt;code&gt;t&lt;/code&gt; still points to the value by printing the result of
dereferencing it.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Question: Would happen if you tried to execute the following code? How could you
fix it?&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    int *v = &amp;amp;t;
    printf(&amp;quot;%d\n&amp;quot;, *v);
&lt;/code&gt;&lt;/pre&gt;

&lt;!-- can&#39;t assign the address of an int* to an int*;
     you would need &#39;v&#39; to be an int** for this to work;
     you would also need to dereference &#39;v&#39; twice in the printf call --&gt;

&lt;p&gt;This illustrates an important concept: pointers can point to almost anything,
even other pointers!&lt;/p&gt;

&lt;h2 id=&#34;aside-the-sizeof-operator:62945700b0816b43d60c385adb103445&#34;&gt;Aside: The &lt;code&gt;sizeof&lt;/code&gt; operator&lt;/h2&gt;

&lt;p&gt;There is a useful operator in C called the &lt;code&gt;sizeof&lt;/code&gt; operator. You can use it to
determine the size of a particular variable in memory. You can also use it to
determine the size of a type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO THIS:&lt;/strong&gt; Run the following code and take note of all the sizes.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    printf(&amp;quot;sizeof(char): %lu\n&amp;quot;,   sizeof(char));
    printf(&amp;quot;sizeof(42): %lu\n&amp;quot;,     sizeof(42));
    printf(&amp;quot;sizeof(float): %lu\n&amp;quot;,  sizeof(float));
    printf(&amp;quot;sizeof(double): %lu\n&amp;quot;, sizeof(double));
    printf(&amp;quot;sizeof(NULL): %lu\n&amp;quot;,   sizeof(NULL));
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that we must put parentheses around the value when using the &lt;code&gt;sizeof&lt;/code&gt;
operator.&lt;/p&gt;

&lt;p&gt;Note also that we are using the &lt;code&gt;%lu&lt;/code&gt; format specifier, which denotes a &amp;ldquo;long
unsigned integer&amp;rdquo; (i.e., a &lt;code&gt;size_t&lt;/code&gt; value).&lt;/p&gt;

&lt;p&gt;Question: What is &lt;code&gt;sizeof(int)&lt;/code&gt;? What is &lt;code&gt;sizeof(3.14)&lt;/code&gt;?&lt;/p&gt;

&lt;!-- aside: a character literal is actually of type &#34;int&#34; in C; this is one
thing that was changed in C++ --&gt;

&lt;p&gt;You can also use &lt;code&gt;sizeof&lt;/code&gt; to detect the size of static arrays:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    int d[3] = { 1, 2, 3 };
    printf(&amp;quot;sizeof(d) = %lu\n&amp;quot;, sizeof(d));
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;allocating-memory:62945700b0816b43d60c385adb103445&#34;&gt;Allocating memory&lt;/h2&gt;

&lt;p&gt;Now that we can track arbitrary locations in memory using pointers, we can
begin allocating memory on the &lt;em&gt;heap&lt;/em&gt; (a large chunk of memory set aside for
programs to use during execution). Heap memory is allocated in C using a call to
the &lt;code&gt;malloc&lt;/code&gt; function (from &lt;code&gt;stdlib.h&lt;/code&gt;),
which takes a single &lt;code&gt;size_t&lt;/code&gt; parameter: the number of
bytes requested. It is conventional to use the &lt;code&gt;sizeof&lt;/code&gt; operator to help
request the correct amount of memory:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    int *r = (int*)malloc(sizeof(int));
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;malloc&lt;/code&gt; function returns a pointer to the allocated memory. Technically,
its return type is &lt;code&gt;void*&lt;/code&gt;, a &lt;em&gt;void pointer&lt;/em&gt;. This is why you have to cast the
result to the type of pointer you need. Such casts are safe because all pointers
are guaranteed to be the same width.&lt;/p&gt;

&lt;p&gt;Note that &lt;code&gt;malloc&lt;/code&gt; could potentially return &lt;code&gt;NULL&lt;/code&gt; if it does not succeed (if
the system is out of memory). Therefore, you should always check the return
value to be safe:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    if (r == NULL) {
        printf(&amp;quot;Out of memory!\n&amp;quot;);
        exit(EXIT_FAILURE);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;exit&lt;/code&gt; function immediately aborts the program with the provided error code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IMPORTANT:&lt;/strong&gt; Unlike Java, C does not automatically de-allocate memory when it
is no longer needed. Thus, you will need to manually de-allocate memory when you
are done, using the &lt;code&gt;free&lt;/code&gt; function:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    free(r)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If your program allocates memory and does not free it, your program has a
&lt;em&gt;memory leak&lt;/em&gt;. The leaked memory will be reclaimed when your program exits, but
until then it will continue to clog up the system.&lt;/p&gt;

&lt;p&gt;In this course, a memory leak is considered to be a software defect. You should
thoroughly debug your program to find and fix all memory leaks. You may use
tools like &lt;a href=&#34;http://valgrind.org/docs/manual/mc-manual.html&#34;&gt;Valgrind/Memcheck&lt;/a&gt;
to help you find memory leaks. Here is a short
&lt;a href=&#34;http://valgrind.org/docs/manual/quick-start.html&#34;&gt;tutorial&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Question: What happens if you try to dereference a pointer after you &lt;code&gt;free&lt;/code&gt; it?&lt;/p&gt;

&lt;p&gt;It is conventional to set set pointers to &lt;code&gt;NULL&lt;/code&gt; immediately after
you &lt;code&gt;free&lt;/code&gt; them. This will trigger a segmentation fault at the offending
location if you ever attempt to dereference it, making it easier to diagnose and
fix software defects.&lt;/p&gt;

&lt;h2 id=&#34;arrays-and-pointers:62945700b0816b43d60c385adb103445&#34;&gt;Arrays and pointers&lt;/h2&gt;

&lt;p&gt;Here&amp;rsquo;s a little secret: you&amp;rsquo;ve actually already been using pointers. In C, array
variable names are actually just pointers to the first element. In fact, you can
find out the address of an array using the same syntax as above:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    int d[3] = { 1, 2, 3 };
    printf(&amp;quot;The array starts at %p\n&amp;quot;, d);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can also use &lt;em&gt;pointer arithmetic&lt;/em&gt; to access array elements:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    printf(&amp;quot;The first element is %d\n&amp;quot;, *d);
    printf(&amp;quot;The second element is %d\n&amp;quot;, *(d+1));
    printf(&amp;quot;The third element is %d\n&amp;quot;, *(d+2));
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This allows you to use pointers explicitly to iterate over arrays. The following
two loops are equivalent:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    for (int i=0; i&amp;lt;3; i++) {
        printf(&amp;quot;Element: %d\n&amp;quot;, d[i]);
    }

    for (int *w = d; w &amp;lt; (d+3); w++) {
        printf(&amp;quot;Element: %d\n&amp;quot;, *w);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In fact, the syntax you&amp;rsquo;re already familiar with for accessing array elements
(i.e., the square brackets used in the first loop above) actually performs
pointer dereferencing and arithmetic &amp;ldquo;under the hood.&amp;rdquo;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IMPORTANT:&lt;/strong&gt; As mentioned before, there is no automatic bounds checking in C,
so you must be careful not to read outside the bounds of the array. Now you can
see that this is because C uses pointers and pointer arithmetic &amp;ldquo;under the
hood,&amp;rdquo; and there is no sure-fire way to check whether a pointer is pointing to
a location inside a particular array.&lt;/p&gt;

&lt;p&gt;We can also allocate arrays on the heap, which is very useful when we don&amp;rsquo;t know
at compile time how big the array should be. For example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    int *e = (int*)malloc(sizeof(int) * b);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will allocate an array big enough to hold a number of elements equal to the
value of &lt;code&gt;b&lt;/code&gt;. It is conventional in such allocations to use the &lt;code&gt;sizeof&lt;/code&gt;
function to determine the number of bytes needed for each element and then to
multiply by the size of the array.&lt;/p&gt;

&lt;p&gt;This now allows us to write functions that return arrays; in fact, they
just return a &lt;em&gt;pointer&lt;/em&gt; to an array. Note that you should &lt;strong&gt;never&lt;/strong&gt; return a
reference to a static array that is local to a function, because the array will
be de-allocated at the end of the function and the pointer will be invalid. You
should only return pointers to arrays that you allocate on the heap. Here is a
function that returns a newly-allocated array:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;char* alloc_alphabet_array()
{
    char *alphabet;
    alphabet = (char*)malloc(sizeof(char) * 27);
    if (alphabet == NULL) {
        printf(&amp;quot;Out of memory!\n&amp;quot;);
    }
    for (char c = &#39;a&#39;; c &amp;lt;= &#39;z&#39;; c++) {
        alphabet[c-&#39;a&#39;] = c;
    }
    alphabet[26] = &#39;\0&#39;;    // null terminator
    return alphabet;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Question: What is &lt;code&gt;sizeof(alph)&lt;/code&gt; if &lt;code&gt;alph&lt;/code&gt; is the return value of this function?&lt;/p&gt;

&lt;p&gt;Recall that in C, strings are just null-terminated arrays of characters. Thus,
in this case we could describe &lt;code&gt;alphabet&lt;/code&gt; using any of the following terms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;char *&lt;/code&gt; (&amp;ldquo;char star&amp;rdquo;)&lt;/li&gt;
&lt;li&gt;&amp;ldquo;pointer to array of chars&amp;rdquo;&lt;/li&gt;
&lt;li&gt;&amp;ldquo;pointer to a C string&amp;rdquo;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For return values that are pointers, some people put the asterisk (&lt;code&gt;*&lt;/code&gt;) with the
type:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    char* alloc_alphabet_array()
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Others put the asterisk with the function name:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    char *alloc_alphabet_array()
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In this course, either approach is fine as long as you are consistent.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IMPORTANT:&lt;/strong&gt; Don&amp;rsquo;t forget to &lt;code&gt;free&lt;/code&gt; these arrays when you&amp;rsquo;re done with them!&lt;/p&gt;

&lt;h2 id=&#34;structs-and-pointers:62945700b0816b43d60c385adb103445&#34;&gt;Structs and pointers&lt;/h2&gt;

&lt;p&gt;As you might suspect by now, you can also create pointers to structs. Assuming
that &lt;code&gt;point_t&lt;/code&gt; is defined as it was in a previous lab, you can allocate a point
on the heap:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    point_t *pt = (point_t*)malloc(sizeof(point_t));
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To access member variables, you have to dereference the pointer:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    (*pt).x = 2;
    (*pt).y = 3;
    printf(&amp;quot;The point is (%d, %d)\n&amp;quot;, (*pt).x, (*pt).y);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Because this is cumbersome, C also provides a more concise way to dereference a
pointer and access a member variable: the &lt;em&gt;arrow&lt;/em&gt; operator (&lt;code&gt;-&amp;gt;&lt;/code&gt;). The following
code is exactly equivalent to the above code:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    pt-&amp;gt;x = 2;
    pt-&amp;gt;y = 3;
    printf(&amp;quot;The point is (%d, %d)\n&amp;quot;, pt-&amp;gt;x, pt-&amp;gt;y);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Of course, you can also use pointers to store references to arrays of structs:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    point_t *pts = (point_t*)malloc(sizeof(point_t) * 4);
    if (pts == NULL) {
        printf(&amp;quot;Out of memory!\n&amp;quot;);
        exit(EXIT_FAILURE);
    }

    for (int i=0; i&amp;lt;4; i++) {
        pts[i].x = i*10;
        pts[i].y = i*20;
    }

    for (int i=0; i&amp;lt;4; i++) {
        printf(&amp;quot;Point: (%d, %d)\n&amp;quot;, pts[i].x, pts[i].y);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Question: Is the type of &lt;code&gt;pt&lt;/code&gt; different from the type of &lt;code&gt;pts&lt;/code&gt;? Why or why not?&lt;/p&gt;

&lt;p&gt;Question: Why didn&amp;rsquo;t we need to use the &lt;code&gt;-&amp;gt;&lt;/code&gt; operator to access the &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;
member variables in the loops above?&lt;/p&gt;

&lt;p&gt;We can also pass structs into functions using pointers, and we can return
pointers to structs from functions:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;point_t* midpoint(point_t *p1, point_t *p2)
{
    point_t* mid = (point_t*)malloc(sizeof(point_t));
    if (mid == NULL) {
        printf(&amp;quot;Out of memory!\n&amp;quot;);
        exit(EXIT_FAILURE);
    }
    mid-&amp;gt;x = (p1-&amp;gt;x + p2-&amp;gt;x)/2;
    mid-&amp;gt;y = (p1-&amp;gt;y + p2-&amp;gt;y)/2;
    return mid;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Because &lt;code&gt;midpoint&lt;/code&gt; allocates memory and returns it, the calling function must
remember to &lt;code&gt;free&lt;/code&gt; the allocated memory. Otherwise, there will be a memory leak.&lt;/p&gt;

&lt;p&gt;Here is some code to test the &lt;code&gt;midpoint&lt;/code&gt; function:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    point_t pt1 = { .x =  0, .y =  0 };
    point_t pt2 = { .x = 10, .y = 10 };

    point_t *midpt = midpoint(&amp;amp;pt1, &amp;amp;pt2);

    printf(&amp;quot;Midpoint: (%d, %d)\n&amp;quot;, midpt-&amp;gt;x, midpt-&amp;gt;y);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Question: Why did we need to pass &lt;code&gt;&amp;amp;pt1&lt;/code&gt; and &lt;code&gt;&amp;amp;pt2&lt;/code&gt; into &lt;code&gt;midpoint&lt;/code&gt; instead of
just &lt;code&gt;pt1&lt;/code&gt; or &lt;code&gt;pt2&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;For large structs, this is far faster than passing or returning the struct
itself, because it is quicker to copy a pointer than the entire struct. However,
they are not exactly equivalent, because passing a pointer does not create a
copy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IMPORTANT:&lt;/strong&gt; If you pass a struct into a function using a pointer, any changes
you make to the struct inside the function will persist &lt;strong&gt;AFTER&lt;/strong&gt; the function
completes. This is because you are actually modifying the memory values of the
original struct.&lt;/p&gt;

&lt;h2 id=&#34;exercise-more-tracing:62945700b0816b43d60c385adb103445&#34;&gt;Exercise: More tracing&lt;/h2&gt;

&lt;p&gt;What does the following code print? Step through it as if you were the machine.
You may find it helpful to draw diagrams with boxes for memory location and
arrows for pointers.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;typedef struct {
    int *a;
    int b;
} stuff_t;

void foo(stuff_t value)
{
    *(value.a) = 2;
    value.b = 3;
}

void bar(stuff_t *value)
{
    *(value-&amp;gt;a) = 4;
    value-&amp;gt;b = 5;
}

void do_stuff()
{
    stuff_t my_stuff;
    int temp = 0;

    my_stuff.a = &amp;amp;temp;
    my_stuff.b = 1;
    printf(&amp;quot;a=%d b=%d\n&amp;quot;, *(my_stuff.a), my_stuff.b);

    foo(my_stuff);
    printf(&amp;quot;a=%d b=%d\n&amp;quot;, *(my_stuff.a), my_stuff.b);

    bar(&amp;amp;my_stuff);
    printf(&amp;quot;a=%d b=%d\n&amp;quot;, *(my_stuff.a), my_stuff.b);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;!--
    shows difference between &#34;void foo(int *a)&#34; and &#34;void foo(int a)&#34;

        printf(&#34;a=%d b=%d\n&#34;, *(my_stuff.a), my_stuff.b);       // should be 0, 1
        printf(&#34;a=%d b=%d\n&#34;, *(my_stuff.a), my_stuff.b);       // should be 2, 1
        printf(&#34;a=%d b=%d\n&#34;, *(my_stuff.a), my_stuff.b);       // should be 4, 5
--&gt;

&lt;h2 id=&#34;exercise-plug-the-leaks:62945700b0816b43d60c385adb103445&#34;&gt;Exercise: Plug the leaks&lt;/h2&gt;

&lt;p&gt;Review your code from this lab; does it leak any memory?&lt;/p&gt;

&lt;p&gt;We purposefully did not include all necessary &lt;code&gt;free&lt;/code&gt; calls to give you
experience with memory management. You should walk through your code and/or test
it with a tool like
&lt;a href=&#34;http://valgrind.org/docs/manual/mc-manual.html&#34;&gt;Valgrind/Memcheck&lt;/a&gt; to find and
fix any memory leaks. Here is a short
&lt;a href=&#34;http://valgrind.org/docs/manual/quick-start.html&#34;&gt;tutorial&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&#34;optional-more-exercises:62945700b0816b43d60c385adb103445&#34;&gt;(Optional) More exercises&lt;/h2&gt;

&lt;p&gt;You may find the following exercises helpful to reinforce your understanding of
pointers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;http://joequery.me/code/c-pointer-exercises/&#34;&gt;JoeQuery&amp;rsquo;s C pointer exercises&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
    </item>
    
    <item>
      <title>HW 1</title>
      <link>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240/hw1/</link>
      <pubDate>Mon, 14 Sep 2015 10:55:33 -0400</pubDate>
      
      <guid>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240/hw1/</guid>
      <description>

&lt;h1 id=&#34;homework-1:b7552e5b9df3d65c77124ec8f56d54bb&#34;&gt;Homework 1&lt;/h1&gt;

&lt;p&gt;&lt;/p&gt;&lt;/p&gt;

&lt;h2 id=&#34;introduction:b7552e5b9df3d65c77124ec8f56d54bb&#34;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;The goal of this homework is to solidify your understanding of the C language
and your ability to write simple programs in C. You will be finishing up some of
the previous labs and submitting your solutions for grading.&lt;/p&gt;

&lt;p&gt;For this homework, you must adhere to the specifications below, NOT the original
lab pages. Please read this page carefully. We will attempt to highlight any
differences from the exercises as they appeared on the labs, but you are
responsible for making sure that your code implements this homework assignment
correctly.&lt;/p&gt;

&lt;h2 id=&#34;starter-files:b7552e5b9df3d65c77124ec8f56d54bb&#34;&gt;Starter Files&lt;/h2&gt;

&lt;p&gt;You should put your code for this homework into a file named &lt;code&gt;hw1.c&lt;/code&gt;, that
should compile against our &lt;a href=&#34;../../../../files/hw1.h&#34;&gt;hw1.h&lt;/a&gt;. If you wish, you may start from
our boilerplate files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;../../../../files/c_labs.tar.gz&#34;&gt;Tarball (.tar.gz)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;../../../../files/c_labs.zip&#34;&gt;Zip file (.zip)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The boilerplate archives include a &lt;code&gt;main.c&lt;/code&gt; (for testing your graphical
functions) and a &lt;code&gt;testsuite.c&lt;/code&gt; (for testing your other functions). You will only
need to submit &lt;code&gt;hw1.c&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&#34;lab-2-grids:b7552e5b9df3d65c77124ec8f56d54bb&#34;&gt;Lab 2: Grids&lt;/h2&gt;

&lt;p&gt;Write a function with the following signature:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;void draw_grid(int x, int y, int rows, int columns, int size)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;IMPORTANT:&lt;/strong&gt; In Lab 2, this function was called &amp;ldquo;&lt;code&gt;grid&lt;/code&gt;&amp;rdquo;, but now it should be
called &amp;ldquo;&lt;code&gt;draw_grid&lt;/code&gt;&amp;rdquo;.&lt;/p&gt;

&lt;p&gt;This function should create a &lt;code&gt;rows x columns&lt;/code&gt; grid of squares each of size
&lt;code&gt;size&lt;/code&gt; with the lower-left corner at &lt;code&gt;(x, y)&lt;/code&gt;. It is important to note that the
&lt;code&gt;size&lt;/code&gt; parameter indicates the width of the individual squares, not the size of
the overall grid.&lt;/p&gt;

&lt;p&gt;Alternating squares should be filled in a checkerboard pattern with the color
blue (RGB values: 0, 0, 255). You will need to look through the documentation
for the &lt;code&gt;turtle_begin_fill()&lt;/code&gt; and &lt;code&gt;turtle_end_fill()&lt;/code&gt; functions.&lt;/p&gt;

&lt;p&gt;The output from the sample &lt;code&gt;main.c&lt;/code&gt; should look something like this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;../../../../images/grid_hw.png&#34; alt=&#34;A grid of alternating color blocks&#34; /&gt;&lt;/p&gt;

&lt;p&gt;You may assume that &lt;code&gt;rows&lt;/code&gt;, &lt;code&gt;columns&lt;/code&gt;, and &lt;code&gt;size&lt;/code&gt; are all positive integers.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;HINT:&lt;/em&gt; It is not required for this homework, but you will probably find it very
helpful to first write a &lt;code&gt;draw_square()&lt;/code&gt; function like the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;void draw_square(int x, int y, int size)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This function should draw a square of size &lt;code&gt;size&lt;/code&gt; with the lower-left corner at
&lt;code&gt;(x, y)&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&#34;lab-3-finding-the-second-minimum-value:b7552e5b9df3d65c77124ec8f56d54bb&#34;&gt;Lab 3: Finding the second minimum value&lt;/h2&gt;

&lt;p&gt;Create a function that returns the 2nd minimum value of an array. Here is the
function signature:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;int second_min(int nums[], int nums_len)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The input to this function is the array of integers and the length of the array.
If there are duplicate elements, you should only count them once. You may assume
that the input array has at least two elements.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;HINT:&lt;/em&gt; It is not required for this homework, but you may find it helpful to
first write a &lt;code&gt;min()&lt;/code&gt; function like the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;int min(int nums[], int nums_len)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This should take as input an array of integers called &lt;code&gt;nums&lt;/code&gt;. The second
parameter, &lt;code&gt;nums_len&lt;/code&gt; is the length of the array. After this function is
working, think about how you would extend it to work for the second minimum.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;HINT:&lt;/em&gt; You may wish to use the &lt;code&gt;INT_MIN&lt;/code&gt; or &lt;code&gt;INT_MAX&lt;/code&gt; constants for this
exercise. They are defined in the &lt;code&gt;limits.h&lt;/code&gt; standard header file.&lt;/p&gt;

&lt;h2 id=&#34;lab-3-palindromes:b7552e5b9df3d65c77124ec8f56d54bb&#34;&gt;Lab 3: Palindromes&lt;/h2&gt;

&lt;p&gt;Write a routine with the following signature:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;bool is_palindrome(char word[])
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Your function should determine if the given string is a palindrome word (can be
written the same way forwards and backwards - like &amp;ldquo;madam&amp;rdquo;, &amp;ldquo;otto&amp;rdquo;, &amp;ldquo;anna&amp;rdquo;,
&amp;ldquo;redivider&amp;rdquo;, &amp;ldquo;kayak&amp;rdquo;, and &amp;ldquo;racecar&amp;rdquo;. This function should do a strict
character-based test; i.e., it should &lt;strong&gt;not&lt;/strong&gt; ignore punctuation and whitespace.
Empty words and one-character words should be considered palindromes for the
purposes of this function.&lt;/p&gt;

&lt;h2 id=&#34;lab-3-same-things-in-strings:b7552e5b9df3d65c77124ec8f56d54bb&#34;&gt;Lab 3: Same things in strings&lt;/h2&gt;

&lt;p&gt;Write a routine with the following signature:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;void same_things(char string1[], char string2[], char common[])
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This function should search through the two given strings and determine the
longest sequence of characters that match at any point in the string, saving the
longest match to the string &lt;code&gt;common&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IMPORTANT:&lt;/strong&gt; In Lab 3, this function just returned the first index of the
longest match; now it should save all the characters of the match to the
&lt;code&gt;common&lt;/code&gt; character array.&lt;/p&gt;

&lt;p&gt;For example, given the following code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;char str1[] = &amp;quot;This is a long sentence of some sort.&amp;quot;;
char str2[] = &amp;quot;This is not a long sentence of some sort&amp;quot;;
char dest[1024];
same_things(str1, str2, dest);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;After that code executes, &lt;code&gt;dest&lt;/code&gt; should contain the following string:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;quot; a long sentence of some sort&amp;quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note the leading space in the output above. Also, don&amp;rsquo;t forget to add the null
terminator character (&lt;code&gt;&#39;\0&#39;&lt;/code&gt;) to the output character string!&lt;/p&gt;

&lt;p&gt;You may assume that &lt;code&gt;common&lt;/code&gt; is large enough to hold the longest common
character sequence.&lt;/p&gt;

&lt;h2 id=&#34;lab-4-drawing-icons:b7552e5b9df3d65c77124ec8f56d54bb&#34;&gt;Lab 4: Drawing icons&lt;/h2&gt;

&lt;p&gt;Define a function called &lt;code&gt;draw_icons&lt;/code&gt; with the following signature:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;void draw_icons(icon_t icons[], int num_icons, int start_x, int start_y)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This function takes as input an array of &lt;code&gt;icon_t&lt;/code&gt; structs (defined in &lt;code&gt;hw1.h&lt;/code&gt;
and the number of icons in the array as well as a starting (x, y) position. The
function should draw them in a row (according to their &lt;code&gt;icon_type&lt;/code&gt;, also defined
in &lt;code&gt;hw1.h&lt;/code&gt;) using the turtle graphics library.&lt;/p&gt;

&lt;p&gt;The output from the sample &lt;code&gt;main.c&lt;/code&gt; should look something like this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;../../../../images/row_of_shapes.png&#34; alt=&#34;A row of seven icons&#34; /&gt;&lt;/p&gt;

&lt;h2 id=&#34;submission:b7552e5b9df3d65c77124ec8f56d54bb&#34;&gt;Submission&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;DUE DATE:&lt;/strong&gt; Friday, September 18, 2015 at 23:59 EDT (11:59pm)&lt;/p&gt;

&lt;p&gt;Submit ONLY your completed &lt;code&gt;hw1.c&lt;/code&gt; file on Canvas using the appropriate
assignment submission. Make sure the file contains a comment field at the top
with your name and a statement of compliance with the &lt;a href=&#34;http://www.jmu.edu/honor/code.shtml&#34;&gt;JMU honor
code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Please check your code carefully and make sure that it complies with the project
specification. In addition, make sure that your code adheres to general good
coding style guidelines. Fix any spelling mistakes, incorrect code formatting,
and inconsistent whitespace before submitting. Make sure you include any
appropriate documentation.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>PA 1</title>
      <link>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240/pa1/</link>
      <pubDate>Wed, 16 Sep 2015 10:55:33 -0400</pubDate>
      
      <guid>https://w3.cs.jmu.edu/bowersjc/classes/fall15/cs240/pa1/</guid>
      <description>

&lt;h1 id=&#34;pa-1-geometry:2be21eef0fe2bac3acb3fe13deb01bca&#34;&gt;PA 1: Geometry&lt;/h1&gt;

&lt;p&gt;The purpose of this lab is for you to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Practice basic C programming:

&lt;ul&gt;
&lt;li&gt;Using structs and non-trivial loops (i.e. something more interesting than
&lt;code&gt;for (int i = 0; i &amp;lt; n; i++)&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Writing functions&lt;/li&gt;
&lt;li&gt;Using several different libraries (i.e. &lt;code&gt;.h&lt;/code&gt;/&lt;code&gt;.c&lt;/code&gt; files) in a single
program.&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;Learn how to read an algorithm specification and write working code from it.&lt;/li&gt;
&lt;li&gt;Practice working at the appropriate level of abstraction.&lt;/li&gt;
&lt;li&gt;Solve problems in computational geometry, an important and fascinating domain
of computer science and mathematics.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;getting-started:2be21eef0fe2bac3acb3fe13deb01bca&#34;&gt;Getting Started&lt;/h2&gt;

&lt;p&gt;First download and decompress one of the starter files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;../../../../files/01_geometry.tar.gz&#34;&gt;Tarball (.tar.gz)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;../../../../files/01_geometry.zip&#34;&gt;Zip file (.zip)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Don&amp;rsquo;t forget to download the appropriate Makefile for your platform from Piazza
or the course website.&lt;/p&gt;

&lt;h3 id=&#34;starter-code:2be21eef0fe2bac3acb3fe13deb01bca&#34;&gt;Starter Code&lt;/h3&gt;

&lt;p&gt;The starter code has 8 files. Four of these are modules that we are providing
you, and you should not need to edit these:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;geometry.h&lt;/code&gt; and &lt;code&gt;geometry.c&lt;/code&gt; : A library of relatively
simple functions for manipulating geometrical objects (called primitives).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;turtle.h&lt;/code&gt; and &lt;code&gt;turtle.c&lt;/code&gt; : The turtle graphics library.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The files you need to edit are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;main.c&lt;/code&gt; : Your main program.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;pa1.h&lt;/code&gt; and &lt;code&gt;pa1.c&lt;/code&gt; : A small library of geometry tools that you are going to
write.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;testsuite.c&lt;/code&gt; : The public tests for this assignment. You may want to try
adding your own tests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Be sure to add the following to your Makefile:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;MODS=geometry.o turtle.o pa1.o
LIBS=-lm
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This tells your Makefile to build the &lt;code&gt;geometry&lt;/code&gt;, &lt;code&gt;turtle&lt;/code&gt;, and &lt;code&gt;pa1&lt;/code&gt;
modules and include the math library (&lt;code&gt;libm.so&lt;/code&gt;) when linking.&lt;/p&gt;

&lt;h2 id=&#34;computational-geometry-a-very-very-basic-introduction:2be21eef0fe2bac3acb3fe13deb01bca&#34;&gt;Computational Geometry: A (Very, very, basic) Introduction&lt;/h2&gt;

&lt;p&gt;Computational geometry is a sub-field of computer science that focuses on
algorithms and data-structures for solving geometric problems. A few typical
examples of computational geometry in action are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;robot navigation (obstacles are polygons in the robots workspace),&lt;/li&gt;
&lt;li&gt;map overlays (finding areas of special interest on a map),&lt;/li&gt;
&lt;li&gt;computer aided design (CAD systems allow designers to build &amp;ldquo;working&amp;rdquo; models
of products in 3D),&lt;/li&gt;
&lt;li&gt;protein folding (researchers are trying to understand how proteins fold in
order to combat diseases like mad-cow disease and are using computational
geometry to do it&amp;ndash;ask Dr. Bowers about this if you are interested),&lt;/li&gt;
&lt;li&gt;and a lot more.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is also related to several other areas of computer science including (but not
limited to): computer vision, graphics, bioinformatics.&lt;/p&gt;

&lt;h2 id=&#34;polygons:2be21eef0fe2bac3acb3fe13deb01bca&#34;&gt;Polygons&lt;/h2&gt;

&lt;p&gt;In this PA you are going to be writing a few basic algorithms and data
structures for dealing with polygons and for computing something called the
&amp;ldquo;convex hull&amp;rdquo; of a polygon.&lt;/p&gt;

&lt;p&gt;For our purposes a &lt;a href=&#34;https://en.wikipedia.org/wiki/Polygon&#34;&gt;&lt;em&gt;polygon&lt;/em&gt;&lt;/a&gt;
is a drawing in the plane (i.e. on a flat sheet of paper) created by first
putting the pen down at some start location, then drawing a series of straight
line segments without lifting pen until eventually ending up back at the start
location.&lt;/p&gt;

&lt;p&gt;The points at which one line segment ends and the next begins are called the
&lt;em&gt;vertices&lt;/em&gt; and the line segments themselves are called the &lt;em&gt;sides&lt;/em&gt; of the
polygon. (These are also often called &lt;em&gt;edges&lt;/em&gt;.)&lt;/p&gt;

&lt;p&gt;You are already familiar with lots of different polygons. For instance,
triangles, squares, rectangles, pentagons, octagons, dodecagons (you all know
what a &lt;a href=&#34;https://en.wikipedia.org/wiki/Dodecagon&#34;&gt;dodecagon&lt;/a&gt; is,
right?). All of these things are formed by a bunch of straight line segments
(the &lt;em&gt;sides&lt;/em&gt;) connected end-to-end (at the &lt;em&gt;vertices&lt;/em&gt;).  All of these have
something else in common&amp;ndash;they are all &lt;em&gt;simple&lt;/em&gt;, meaning that they do not self
intersect. We will restrict our discussion to simple polygons.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;../../../../images/shapes.png&#34; alt=&#34;Polygons&#34; /&gt;&lt;/p&gt;

&lt;h3 id=&#34;geometry-primitives-library:2be21eef0fe2bac3acb3fe13deb01bca&#34;&gt;Geometry Primitives Library&lt;/h3&gt;

&lt;p&gt;Included in the starter code for this PA is a small geometry primitives library.
The word &lt;em&gt;primitives&lt;/em&gt; is used to denote basic data structures and algorithms
that are used to build more complex data structures and algorithms. This is much
the same as the difference between primitive types, like &lt;code&gt;int&lt;/code&gt; and &lt;code&gt;char&lt;/code&gt; and
the compound types you create using &lt;code&gt;struct&lt;/code&gt;s in C or &lt;code&gt;class&lt;/code&gt;es in Java.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;geometryPrimitives&lt;/code&gt; library includes the following two structures (see
&lt;code&gt;geometryPrimitives.h&lt;/code&gt;):&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    /*
     * 2D points (x,y)
     */
    typedef struct {
            int x;
            int y;
    } point_t;

    /*
     * 2D line segments ((x1, y1), (x2, y2));
     */
    typedef struct {
    	point_t p1;
    	point_t p2;
    } segment_t;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The first, &lt;code&gt;point_t&lt;/code&gt; is used to specify 2D points (i.e. $(x, y)$) in the plane
with integer coordinates. The second, &lt;code&gt;segment_t&lt;/code&gt; specifies a line segment
between its two points $(p_1, p_2)$. These structures may be useful for you in
the remainder of this PA and you can use them in your code.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;geometryPrimitives&lt;/code&gt; library also defines several &lt;em&gt;predicate functions&lt;/em&gt;,
which are functions that return true or false based on some property. We will
discuss these functions later as they become relevant.&lt;/p&gt;

&lt;!-- The ones you will need for this PA are:

* `bool is_left_hand_turn(point_t p1, point_t p2, point_t p3);` which returns
  true if and only if moving from `p1` to `p2` and then to `p3` constitutes a
  left-hand-turn.
* `bool is_right_hand_turn(point_t p1, point_t p2, point_t p3);` which returns
  true if and only if moving from `p1` to `p2` and then to `p3` constitutes a
  right-hand-turn.
* `bool segments_cross(segment_t s1, segment_t s2);` which returns true if and
  only if the two segments `s1` and `s2` cross.

__Note:__ You do not necessarily need to understand how these are implemented,
just how to use them. If you are interested in learning how their
implementations work, talk to Dr. Bowers.
--&gt;

&lt;h3 id=&#34;problem-1-drawing-a-triangle:2be21eef0fe2bac3acb3fe13deb01bca&#34;&gt;Problem 1: Drawing a triangle&lt;/h3&gt;

&lt;p&gt;Write a function called &lt;code&gt;drawTriangle&lt;/code&gt; which takes as input three &lt;code&gt;point_t&lt;/code&gt;s,
&lt;code&gt;p_1&lt;/code&gt;, &lt;code&gt;p_2&lt;/code&gt;, and &lt;code&gt;p_3&lt;/code&gt; and draws the triangle. In other words, your function&amp;rsquo;s
signature should be:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    void draw_triangle(point_t p1, point_t p2, point_t p3);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that you should declare your function in &lt;code&gt;pa1.h&lt;/code&gt; and define it in &lt;code&gt;pa1.c&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Use your function to draw several different triangles in the &lt;code&gt;main()&lt;/code&gt; function
in &lt;code&gt;main.c&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Reference solution: ~5-10 lines&lt;/p&gt;

&lt;h3 id=&#34;problem-2-struct-for-storing-polygons:2be21eef0fe2bac3acb3fe13deb01bca&#34;&gt;Problem 2: Struct for storing polygons&lt;/h3&gt;

&lt;p&gt;We store a polygon as a list of vertices. The edges are inferred by connecting
adjacent vertices in the list by line segments. In &lt;code&gt;geometryPrimitives.h&lt;/code&gt; we
have defined a type &lt;code&gt;polygon_t&lt;/code&gt; for storing a polygon. It looks like this:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;	typedef struct {
		point_t* verts;
		int num_verts;
	} polygon_t;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that this stores the vertices as a &lt;em&gt;dynamically-allocated&lt;/em&gt; array called
verts.&lt;/p&gt;

&lt;p&gt;Write a function that can create a &lt;code&gt;polygon_t&lt;/code&gt; from an array of vertices.&lt;/p&gt;

&lt;p&gt;Add the following function declaration to &lt;code&gt;pa1.h&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;	polygon_t create_polygon(point_t vertices[], int num_vertices);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Define the &lt;code&gt;create_polygon&lt;/code&gt; function in &lt;code&gt;pa1.c&lt;/code&gt;. It should create and return a
new &lt;code&gt;polygon_t&lt;/code&gt; struct with the points from the input attribute &lt;code&gt;vertices&lt;/code&gt;
copied into the &lt;code&gt;verts&lt;/code&gt; attribute of the struct. Note that this will require
first allocating enough memory to store &lt;code&gt;verts&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You should also implement the following function:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;	void free_polygon(polygon_t *poly);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This function should free any allocated memory associated with the given polygon
(setting pointers to &lt;code&gt;NULL&lt;/code&gt; as appropriate) and should should also set the
number of vertices in that polygon to zero. This function should be called by
client programs for each polygon used in order to prevent memory leaks.&lt;/p&gt;

&lt;p&gt;Note that this function is passed a polygon &lt;strong&gt;pointer&lt;/strong&gt;; this will allow you to
reset any de-allocated pointers and prevent dangling pointer problems.&lt;/p&gt;

&lt;p&gt;Reference solution: ~15-20 lines&lt;/p&gt;

&lt;h3 id=&#34;problem-3-drawing-a-polygon:2be21eef0fe2bac3acb3fe13deb01bca&#34;&gt;Problem 3: Drawing a polygon&lt;/h3&gt;

&lt;p&gt;Write a function called &lt;code&gt;draw_polygon&lt;/code&gt; which takes as input a &lt;code&gt;polygon_t&lt;/code&gt; (as you
defined above) and draws the polygon. In other words, your function&amp;rsquo;s signature
should be:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    void draw_polygon(polygon_t poly);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Declare your function in &lt;code&gt;pa1.h&lt;/code&gt; and define it in &lt;code&gt;pa1.c&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Use your function to draw several different polygons in the &lt;code&gt;main()&lt;/code&gt; function in
&lt;code&gt;main.c&lt;/code&gt; (with different numbers of points in each polygon).&lt;/p&gt;

&lt;p&gt;Reference solution: ~10-15 lines&lt;/p&gt;

&lt;h2 id=&#34;convex-polygons:2be21eef0fe2bac3acb3fe13deb01bca&#34;&gt;Convex Polygons&lt;/h2&gt;

&lt;p&gt;One classification of polygons that is important for many computational purposes
is the difference between convex and non-convex polygons.&lt;/p&gt;

&lt;p&gt;A &lt;em&gt;convex&lt;/em&gt; polygon is one where all the interior angles are less than 180
degrees. If even one angle is greater than 180 degrees, then the polygon is
&lt;em&gt;non-convex&lt;/em&gt;. See below:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;../../../../images/convexity.png&#34; alt=&#34;Polygons&#34; /&gt;&lt;/p&gt;

&lt;p&gt;Suppose you wanted to check if a polygon were convex or not. You could compute
all of the angles at each vertex and test whether any one is greater than 180
degrees. The problem with this approach is that it requires you to use
transcendental functions like $\sin()$ and $\cos()$ to compute the angles. When
possible, we often avoid the use of these functions. One reason to avoid the use
of these trig functions is that they require many more processor cycles to
compute than simple arithmatical operations like addition and multiplication.&lt;/p&gt;

&lt;p&gt;Another method for determining whether a polygon is convex is to walk around the
polygon (think of redrawing it with the pen, or better yet, drawing it in the
grass and walking along it) and keep track of how many left-hand-turns you make
and how many right-hand-turns you make. (Whether a turn is a left-hand-turn or
right-hand-turn can be computed with only simple arithmetic and without
computing the angle or using trig functions.)&lt;/p&gt;

&lt;p&gt;Notice that if the polygon is convex, then either 1) all of your turns will be
right-hand-turns (if you are walking clockwise), or 3) they will all be
left-hand-turns (if you are walking counter-clockwise). However, on a non-convex
polygon, you always have a mix of right and left-hand-turns. In the
&lt;code&gt;geometryPrimitives&lt;/code&gt; library there are two functions you will need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;is_left_hand_turn(point_t p1, point_t p2, point_t p3)&lt;/code&gt;: Returns true if and
only if traveling from p1 to p2 and then from p2 to p3 consitutes a
left-hand-turn.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;is_right_hand_turn(point_t p1, point_t p2, point_t p3)&lt;/code&gt;: Returns true if and
only if traveling from p1 to p2 and then from p2 to p3 constitutes a
right-hand-turn.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&#34;problem-4-convex-test-for-4-gons:2be21eef0fe2bac3acb3fe13deb01bca&#34;&gt;Problem 4: Convex test for 4-gons&lt;/h3&gt;

&lt;p&gt;A &lt;em&gt;4-gon&lt;/em&gt; is a polygon with 4 sides (for example, squares, parallelograms,
rectangles, kites, diamonds, etc.). Write a function called &lt;code&gt;is_4gon_convex&lt;/code&gt;
with the following signature:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    bool is_4gon_convex(point_t p1, point_t p2, point_t p3, point_t p4);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;em&gt;input&lt;/em&gt; to this function is the four vertices of the 4-gon, $(p&lt;em&gt;1, p_2, p_3,
p_4)$ and _output&lt;/em&gt; is &lt;em&gt;true&lt;/em&gt; if and only if the 4-gon is convex, false
otherwise.&lt;/p&gt;

&lt;p&gt;You may wish to write a function called &lt;code&gt;draw_4gon&lt;/code&gt; which takes as input the
four points &lt;code&gt;p1&lt;/code&gt;, &lt;code&gt;p2&lt;/code&gt;, &lt;code&gt;p3&lt;/code&gt;, &lt;code&gt;p4&lt;/code&gt; of a 4-gon and draws it blue if it is convex,
or red otherwise.  (Hint: use your &lt;code&gt;is_4gon_convex&lt;/code&gt; function). Draw at least one
convex and one non-convex 4-gon in your &lt;code&gt;main()&lt;/code&gt; function in &lt;code&gt;main.c&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Optional Challenge Question: Remember that we are assuming the input polygon is
simple, i.e. it doesn&amp;rsquo;t self-cross. Does your method always work correctly when
the polygon is not simple?&lt;/p&gt;

&lt;p&gt;Reference solution: ~10-15 lines&lt;/p&gt;

&lt;h3 id=&#34;problem-5-convex-test-for-polygons:2be21eef0fe2bac3acb3fe13deb01bca&#34;&gt;Problem 5: Convex test for polygons&lt;/h3&gt;

&lt;p&gt;Write a function called &lt;code&gt;is_polygon_convex&lt;/code&gt; with the following signature:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    bool is_polygon_convex(polygon_t poly);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;em&gt;input&lt;/em&gt; to this function is a polygon (of type &lt;code&gt;polygon_t&lt;/code&gt; which you defined
above) and the &lt;em&gt;output&lt;/em&gt; is &lt;em&gt;true&lt;/em&gt; if and only if the polygon &lt;code&gt;poly&lt;/code&gt; is convex,
false otherwise.&lt;/p&gt;

&lt;p&gt;Modify your &lt;code&gt;draw_polygon&lt;/code&gt; function to draw the polygon blue if it is convex,
red otherwise.&lt;/p&gt;

&lt;p&gt;Hint 1: if you get stuck, think about writing a test for 5-gons, and then
6-gons. What is changing each time?&lt;/p&gt;

&lt;p&gt;Hint 2: You might want to count the number of left hand turns and count the
number of right hand turns you find. If both are non-zero, then you have a mix.&lt;/p&gt;

&lt;p&gt;Hint 3: Remember that it takes 3 consecutive vertices to check whether something
is a turn. Did you check all of the vertices (where does your loop end)?&lt;/p&gt;

&lt;p&gt;Reference solution: ~15-20 lines&lt;/p&gt;

&lt;h2 id=&#34;convex-hulls:2be21eef0fe2bac3acb3fe13deb01bca&#34;&gt;Convex Hulls&lt;/h2&gt;

&lt;p&gt;One very important concept from computational geometry is the convex hull of a
polygon. The &lt;em&gt;convex hull&lt;/em&gt; of a polygon P is the smallest convex polygon that
contains P on its interior. See the figure below. Convex polygons are generally
easier to work with computationally than non-convex polygons. Because of this,
there are many applications that use the convex hulls to speed up computation.&lt;/p&gt;

&lt;p&gt;For instance, in computer graphics applications such as a game or simulation you
might have polygonal objects moving around and want to test whether two objects
collide with one another. It turns out that testing whether two non-convex
polygons have collided is a much more expensive operation than testing whether
two convex polygons have collided.&lt;/p&gt;

&lt;p&gt;A system for testing collisions for a bunch of non-convex polygons could simply
test directly whether each polygon collides with every other polygon (slow), or
instead it could store the convex hull of each polygon and as a first test
determine whether any of the convex hulls collide (fast). The system would then
only test whether the actual polygons collide if their convex hulls collide.
Thus the slow operation is invoked only when it is more likely to return true.&lt;/p&gt;

&lt;p&gt;An example of a convex hull is shown below:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;../../../../images/convexhull.png&#34; alt=&#34;A non-convex polygon red (left), its convex hull (center), and the polygon
shown on top of its convex hull (right)&#34; /&gt;&lt;/p&gt;

&lt;p&gt;The left of the image above shows a very non-convex polygon in red spelling out
the letters JMU. The convex hull of the JMU-polygon is shown in the middle, and
on the right the JMU polygon is shown on top of its convex polygon. You can
think of the convex hull as what you get if you stretch a rubber-band around the
polygon.&lt;/p&gt;

&lt;h3 id=&#34;problem-6-drawing-convex-hulls:2be21eef0fe2bac3acb3fe13deb01bca&#34;&gt;Problem 6: Drawing convex hulls&lt;/h3&gt;

&lt;p&gt;For this exercise you are going to draw the convex hull of a polygon. The
essential idea for this is to take all pairs of vertices (not necessarily
consecutive) and test the pair to see if all other points in the polygon are
either ALL left-hand-turns or ALL right-hand-turns from that pair. The basic
pseudo-code for this function is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;For all pairs (p, q):
    If all other points form a left-hand-turn with (p, q) or all other
    points form a right-hand-turn with (p, q):
        Draw the line segment (p, q)
    Otherwise:
        Do not draw (p, q)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Your function should have the following signature:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-c&#34;&gt;    void draw_convex_hull(polygon_t poly);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;Note:&lt;/em&gt; When testing your code, you should only use polygons for which no three
points are collinear. The pseudo-code above assumes that the polygon is
&amp;ldquo;generic&amp;rdquo; which means no three points are collinear.&lt;/p&gt;

&lt;p&gt;Reference solution: ~25-40 lines&lt;/p&gt;

&lt;h2 id=&#34;submission:2be21eef0fe2bac3acb3fe13deb01bca&#34;&gt;Submission&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;DUE DATE:&lt;/strong&gt; Friday, September 25, 2015 at 23:59 EDT (11:59pm)&lt;/p&gt;

&lt;p&gt;Submit ONLY your completed &lt;code&gt;pa1.h&lt;/code&gt; and &lt;code&gt;pa1.c&lt;/code&gt; file on Canvas using the
appropriate assignment submission. Submit both files separately; do &lt;strong&gt;NOT&lt;/strong&gt;
submit a zip file. Make sure both files contain a comment field at the top with
your name and a statement of compliance with the &lt;a href=&#34;http://www.jmu.edu/honor/code.shtml&#34;&gt;JMU honor
code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Please check your code carefully and make sure that it complies with the project
specification. In addition, make sure that your code adheres to general good
coding style guidelines. Fix any spelling mistakes, incorrect code formatting,
and inconsistent whitespace before submitting. Make sure you include any
appropriate documentation.&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>