forked from uva-cs/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
146 lines (144 loc) · 14.8 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>PDR: Laboratory 1: Introduction to C++</title>
<style type="text/css">code{white-space: pre;}</style>
<link rel="stylesheet" href="../../markdown.css">
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
<![endif]-->
</head>
<body>
<h1 id="pdr-laboratory-1-introduction-to-c">PDR: Laboratory 1: Introduction to C++</h1>
<p><a href="../index.html">Go up to the Labs table of contents page</a></p>
<h3 id="objective">Objective</h3>
<p>This is laboratory is intended to get you up to speed quickly with both C++ and Unix.</p>
<h3 id="background">Background</h3>
<p>There will be a lab every week, which consists of three parts: a pre-lab, an in-lab, and a post-lab. The due dates are all listed on the <a href="../../uva/labduedates.html">lab due dates</a> page. This will all be discussed in this lab.</p>
<h3 id="tutorial">Tutorial</h3>
<p>Most labs will have a tutorial. You are expected to complete this tutorial before beginning the lab, as the lab will use concepts from each tutorial.</p>
<p>The tutorial for this lab is <a href="../../tutorials/01-intro-unix/index.html">Tutorial 1: Introduction to UNIX</a>, which will help you set up your UNIX environment.</p>
<h3 id="recommended-readings">Recommended Readings</h3>
<p>We have attempted to compile a collection of readings that go over topics covered in this course. Readings are always optional and are there for you to use as you see fit.</p>
<ul>
<li>Introduction to C++ section on the <a href="../../docs/readings.html">Readings page</a></li>
</ul>
<h2 id="procedure">Procedure</h2>
<h3 id="pre-lab">Pre-lab</h3>
<ol type="1">
<li>Write a recursive function to compute exponentiation</li>
<li>Investigate the C++ object lifecycle</li>
<li>Learn about the submission system</li>
<li>Files to download: <a href="lifecycle.cpp.html">lifecycle.cpp</a> (<a href="lifecycle.cpp">src</a>)</li>
<li>Files to submit: xToN.cpp</li>
</ol>
<h3 id="in-lab">In-lab</h3>
<ol type="1">
<li>Ask the TAs if you have any questions about the pre-lab code or Unix</li>
<li>Separate the object lifecycle code to better follow C++ conventions</li>
<li>Investigate an example program and ask questions about it</li>
<li>Files to download: <a href="svtest.cpp.html">svtest.cpp</a> (<a href="svtest.cpp">src</a>), <a href="svutil.cpp.html">svutil.cpp</a> (<a href="svutil.cpp">src</a>), <a href="svutil.h.html">svutil.h</a> (<a href="svutil.h">src</a>), and <a href="lifecycle.cpp.html">lifecycle.cpp</a> (<a href="lifecycle.cpp">src</a>)</li>
<li>Files to submit: lifecycle.questions.txt, vector.questions.txt, LifeCycle.cpp, LifeCycle.h, and TestLifeCycle.cpp</li>
</ol>
<h3 id="post-lab">Post-lab</h3>
<ol type="1">
<li>Investigate a more complicated example program and ask questions about it</li>
<li>Files to download: <a href="list.h.html">list.h</a> (<a href="list.h">src</a>), <a href="list.cpp.html">list.cpp</a> (<a href="list.cpp">src</a>)</li>
<li>Files to submit: postlab1.question.txt</li>
</ol>
<hr />
<h2 id="pre-lab-1">Pre-lab</h2>
<p>Complete <a href="../../tutorials/01-intro-unix/index.html">Tutorial 1: Introduction to UNIX</a> before proceeding. Remember that you should always complete the tutorial before starting the lab.</p>
<p>For the pre-lab, you will need to write a <strong>recursive</strong> function called <code>xton()</code> to compute <em>x^n</em> for non-negative integers <em>n</em>. Assume that <em>x^0=1</em>. Put this function in a program with a <code>main()</code> function. Your program should prompt the user for two integer values, and raise the first to the power of the second by calling your <code>xton()</code> function. To keep the code simple, you can assume that your program will only be called with valid inputs.</p>
<p>The file should be called xToN.cpp, and should be submitted to the pre-lab 1 assignment in the submission system -- more details below.</p>
<p>Note that your program should take in <strong>exactly two inputs and nothing else</strong>. We are going to run it through an automated grading script prior to the TAs grading it -- if your program takes in a different number of inputs, you will receive points off.</p>
<p>To help you out, we have provided an example C++ file below. You may find this example helpful in writing your function to compute <em>x^n</em> and the <code>main()</code> function to call it.</p>
<pre><code>#include <iostream>
using namespace std;
int sum (int a, int b) {
int tmp = a;
tmp += b;
return tmp;
}
int main () {
int x, y, z;
cin >> x;
cin >> y;
z = sum (x, y);
cout << x << " + " << y << " = " << z << endl;
return 0;
}</code></pre>
<p>Lastly, take a look at the object life-cycle code (<a href="lifecycle.cpp.html">lifecycle.cpp</a> (<a href="lifecycle.cpp">src</a>)). Use it as a mechanism for understanding how various aspects of C++ work and try stepping through it by hand. Use the <a href="../../docs/readings.html">readings</a>, the web, and any other C++ references to help you look up parts of the program you do not understand.</p>
<h3 id="assignment-submission">Assignment submission</h3>
<p>All assignments will be submitted through our custom submission tool which can be accessed through Collab's Course Tools link or directly at <a href="https://libra.cs.virginia.edu/~pedagogy" class="uri">https://libra.cs.virginia.edu/~pedagogy</a>.</p>
<p>Every file submitted, including text files, should include your name, email ID, the date, and the name of the file in a header comment at the beginning of the file.</p>
<p>There is no check to make sure you have submitted all of the correct files -- on the 'Procedure' page (always at the top of the lab document), we clearly state which files should be submitted for each lab part. For example, for this pre-lab, you should submit just the following file for pre-lab 1: xToN.cpp.</p>
<p>Each assignment has 3 dates: an open date (when you can start submitting the assignment), a due date (when it's due), and a close date (the last point that you can submit the assignment). The dates are listed for the week of the lab -- the lab week starts on a Sunday and ends on a Saturday. In particular, the due date for the pre-labs, as well as the open date for the in-labs and post-labs is when the first lab section starts. The due date for the post-labs is the start of Friday's first lecture.</p>
<p>More information on open dates, due dates, and close dates can be found on the <a href="../../uva/labduedates.html">lab due dates</a> page.</p>
<p>There are a number of rules that we will strictly follow:</p>
<ul>
<li>Pre-labs are due at the same time for everybody, regardless of your lab section; that time is the beginning of the FIRST Tuesday lab.</li>
<li>In-labs are due at the end of the day on Tuesday.</li>
<li>Any late lab part will receive 25% off (for just that part) for the first 24 hours (or part thereof) that it is late, after which no credit will be given. Note that a computer program does this deduction -- so if your lab is 1 second late, it still receives 25% off.</li>
<li><strong>No credit will be given for a lab component which does not compile.</strong> If you are having problems with your code, you should comment out parts so that it does compile -- you will receive more credit for a compilable program that has part of the code commented out than you would for a program that does not compile. See the <a href="../../docs/compilation.html">compilation</a> for hints as to how to get your code to compile.</li>
</ul>
<h3 id="resubmitting-your-assignment">Resubmitting your assignment</h3>
<p>If you submit your assignment, and you realize you made a mistake (didn't submit all the files, etc.), you can resubmit your assignment as many times as you want. The date of submission is the date you re-submitted your assignment -- so if you resubmit your assignment after the due date to add one more file, the <strong>ENTIRE</strong> assignment will have the late submission date. We only look at the most recent submission.</p>
<p><strong><em>Note that you have to bring your computer to in-lab!</em></strong></p>
<hr />
<h2 id="in-lab-1">In-lab</h2>
<h3 id="general-in-lab-procedure">General In-lab Procedure</h3>
<p>The purpose of the labs is to allow you to work through the lab activity, and if you encounter questions or problems, ask for TA assistance. Be sure to include your name, email ID, the date, and the name of the file in a banner comment at the beginning of each file you submit.</p>
<h3 id="understanding-c">Understanding C++</h3>
<ol type="1">
<li>Ask the TAs if you have questions about your <em>x^n</em> function.</li>
<li>Ask the TAs if you have questions about using Unix.</li>
<li>Object Lifecycle Program
<ul>
<li>Download the <a href="lifecycle.cpp.html">lifecycle.cpp</a> (<a href="lifecycle.cpp">src</a>) file</li>
<li>Compile with clang++ (i.e., <code>clang++ lifecycle.cpp</code>) and execute the program.</li>
<li>Break lifecycle.cpp into multiple files. It may be helpful to look at the <a href="../../slides/01-cpp.html">class slides</a>. Compile and run this program. Note that to compile a C++ program with multiple files, just put them on the same line: <code>clang++ LifeCycle.cpp TestLifeCycle.cpp</code> (you usually leave out the .h files when compiling). Add the descriptive header at the top of the file containing your identifying information, and name them as follows:
<ul>
<li>LifeCycle.h</li>
<li>LifeCycle.cpp</li>
<li>TestLifeCycle.cpp</li>
</ul></li>
<li>Comment out the prototype for <code>getMaxMyObj()</code>. Recompile. What happens and why? Talk to TA if you are unsure. Now un-comment the prototype.</li>
<li>Look at the output of lifecycle.cpp. Write at least one question about something in this program. Write your questions in a text file named lifecycle.questions.txt. Remember to include the header comment at the top of the file.</li>
</ul></li>
<li>Using C++ vector container class with strings
<ul>
<li>There are 3 files on this repo that demonstrate the use of multi-file programs and the use of the vector collection class in C++: <a href="svtest.cpp.html">svtest.cpp</a> (<a href="svtest.cpp">src</a>), <a href="svutil.cpp.html">svutil.cpp</a> (<a href="svutil.cpp">src</a>), and <a href="svutil.h.html">svutil.h</a> (<a href="svutil.h">src</a>). The class in those files is similar to the ArrayList class in the Java standard library (or Java's own Vector class). Compile the program in Unix and run it. The command <code>clang++ svutil.cpp svtest.cpp</code> will compile the program.</li>
<li>Comment out the <code>#include <iostream></code> preprocessor directive in the file svtest.cpp, and then rebuild the program. Was there an error?</li>
<li>Now undo what you did in the previous step, but now comment out the <code>using namespace std;</code> in svutil.h, and rebuild the program. Was there an error? What objects are now undeclared and why?</li>
<li>Write at least one question about something in this program. This question might be about something that you don't understand completely. Write your questions in a file named vector.questions.txt.</li>
</ul></li>
</ol>
<h3 id="capitalization">Capitalization</h3>
<p>Under Windows, the case of a file name is ignored -- thus, lifecycle.cpp, LifeCycle.cpp, and LIFECYCLE.CPP all refer to the same file. However, it is <strong>NOT</strong> true for Linux, which is what we will use to test and grade your code. Thus, if your file is called 'LifeCycle.h', and you have (in your TestLifeCycle.cpp file) a line that states: <code>#include "lifecycle.h"</code>, then your program will NOT work under Linux (since case DOES matter with file names). Since your code does not compile, you will get zero credit. So make sure your file names match!</p>
<h3 id="troubleshooting">Troubleshooting</h3>
<p>Does your program not compile? Here are a few things to try -- these are problems that previous students have encountered.</p>
<ul>
<li>You must have <code>#include <iostream></code> at the beginning of each file (well, really each file that uses <code>cin</code> and <code>cout</code>, but it doesn't hurt to have it at the beginning of each file).</li>
<li>Likewise, you will need <code>using namespace std;</code> at the beginning of each file.</li>
<li>Is your subroutine a method (i.e. part of a class) or a function (i.e. not part of a class)? Make sure you know which is which! A method has its name in the form classname::methodname, whereas a function doesn't have a class name or the double-colon.</li>
<li>Make sure that your subroutine names (whether they be function names or method names) are consistent between the .h files and the .cpp files</li>
<li>Have a compiler error that you don't understand? We have the translation! See the <a href="../../docs/compilation.html">compilation</a> page, which lists common compiler error messages, what they mean, and how to solve them. If you don't see yours listed there, let us know, and we'll add it.</li>
</ul>
<hr />
<h2 id="post-lab-1">Post-lab</h2>
<p>Complete the exercise below, and submit the text file described there to the submission system.</p>
<h3 id="linked-list-template-code">Linked List Template code</h3>
<p>Download the two linked list files (<a href="list.h.html">list.h</a> (<a href="list.h">src</a>) and <a href="list.cpp.html">list.cpp</a> (<a href="list.cpp">src</a>)). Compile and run the program. The compile command is <code>clang++ list.cpp</code>. If you try to compile the list.h file, it won't work correctly.</p>
<p>Look at list.h, which is #include'd from list.cpp -- it contains the <strong>bodies</strong> of the methods, which is not the way that it is usually done (the bodies are typically in the .cpp file only). We normally only include in .h files just the declarations (i.e., prototypes) of classes, constants, function, etc., but not definitions of C++ methods (i.e. the bodies of the methods). However, when implementing template classes, this is something that is necessary to make the class compile successfully.</p>
<p>Look more closely at list.cpp. The line:</p>
<pre><code>List<int> *l = new List<int>();</code></pre>
<p>declares (a pointer to) a List of integers, called <code>l</code>. A similar line, but with a different type, would create a list of something else. For example, consider the following line:</p>
<pre><code>List<char> list2;</code></pre>
<p>This declares a (non-pointer) list of characters (this is not in the list.cpp file, by the way). Now all the methods in the List class will be available to both <code>l</code> and <code>list2</code>.</p>
<p>Write at least one question about something in this program. Put your question(s) in a text file called "postlab1.question.txt" and submit this to the submission system to the post-lab 1 assignment.</p>
</body>
</html>