plugin-lab01 - CS 2150 Laboratory 1 Introduction to C Week...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 2150 Laboratory 1 Introduction to C++ Week of Monday, 31 August 2009 Objective: This is laboratory is intended to get you up to speed quickly with both C++ and Unix. Background: Every week we will have a lab meeting. Each lab will consist of three parts: a pre‐lab (to be completed BEFORE coming to lab), an in‐lab (activity to be done during lab), and a post‐lab. The pre‐lab will also include a tutorial. Parts of all three of these may be required to turned in. Unless otherwise stated, post‐labs will be due electronically by noon on Friday. You MUST keep copies of the source files you submit in your home directory. This will be discussed in this lab. Reading(s): 1. The introduction to Unix tutorial found on Collab 2. Weiss C++ for Java Programmers Chapters 0, 1, and 2 (pp. 1‐33) CS 2150, fall 2009 Page 2 of 9 Procedure Pre­lab 1. Read Weiss C++ for Java Programmers Chapters 0, 1, and 2 (pp. 1‐33) – see description of the contents on the next page. 2. Complete the introductory tutorial on using Unix found in Collab (in the tutorials folder of the Resources section of Collab). 3. Write the xToN.cpp file, as described in the pre‐lab section. If you are unfamiliar with the Collab submission system, read through the in‐lab section of the document. Submit this file. It is due by the time that your lab session starts. 4. Examine the Object life‐cycle code posted in the labs directory on Collab. You may not understand everything in this program by the beginning of lab, but you should by the end of next week. We will be using this program during the in‐lab activity. 5. Read how to reboot into Linux for the in‐lab. Although the TAs will be able to help you, you should still have an idea how to do it. 6. Files to download: lifecycle.cpp 7. Files to submit: xToN.cpp In­lab 1. Read through the in‐lab section of this document. You should be sure to be familiar with the Collab submission system. 2. Complete the in‐lab requirements as described. 3. Be sure to include your name, email ID, the date, and the name of the file in a header comment at the beginning of each file you submit (including text files!) 4. Files to download: svtest.cpp, svutil.cpp, svutil.h, and lifecycle.cpp 5. Files to submit: lifecycle.questions.txt, vectors.questions.txt, LifeCycle.cpp, LifeCycle.h, and TestLifeCycle.cpp Post­lab 1. See details in postlab document 2. For this lab you will be submitting your code electronically through Collab. Your fully functional code is due at noon on the Friday of the week of the lab. Be sure to include your name, email ID, the date, and the name of the file in a header comment at the beginning of each file you submit (including text files!). 3. Files to download: dsexceptions.h, LinkedList.h, LinkedList.cpp, and TestLinkedList.cpp 4. Files to submit: postlab1.question.txt CS 2150, fall 2009 Page 3 of 9 Pre­lab First, complete the Unix tutorial found in Collab. For this course, you will need to be familiar with Emacs. You are welcome to use any editor that is not an IDE (Integrated Development Environment), but we expect that you are very familiar with Emacs for theexams, including the keyboard shortcuts! The readings (pages 1‐33) in Weiss C++ for Java Programmers (CFJP) cover the following topics: Introduction, Basic Types and Control Structures, and Functions, Arrays, Strings, and Parameter Passing. Here is Figure 2‐9, on p. 30 in Weiss CFJP. It is a sample function with a main() function that calls it. The source code for this and other examples from the book is on the Collab site (in the Resources section under misc/textbooks). You may find this and other examples helpful in writing your function to compute xn and the main function to call it. Note that this code has been modified from the book version to allow for user input (the second and third lines of the main() function). #include <iostream> using namespace std; // Correct implementation of swap2 void swap2( int & a, int & b ) { int tmp = a; a = b; b = tmp; } int main( ) { int x, y; cin >> x; cin >> y; swap2( x, y ); cout << x << " " << y << endl; return 0; } For the pre‐lab, you will need to write a recursive function called xton() to compute xn for nonnegative integers n. Assume that x0 = 1. Put this function in a program with a main() function. Your program should prompt the user for two integer values, and raise the first to the power of the second by calling your xton() function. The file should be called xToN.cpp, and should be submitted to the pre‐lab 1 assignment in the Collab submission system. Be sure to include your name, email ID, the date, and the name of the file in a header comment at the beginning of each file you submit (including text files!). If you need to resubmit your file, see the directions in the in‐lab section of this document. CS 2150, fall 2009 Page 4 of 9 Note that your program should taken in exactly two inputs and nothing else. 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. You should save a copy of your assignment in your home directory. If a grading issue arises, only the copy in your home directory will be allowed to be re‐submitted for credit. Note, however, that Cygwin doesn’t always play nicely with home directory. It is easiest to develop the files on the local computer (i.e. the desktop), and then copy them to your home directory afterwards. This is especially true (and necessary) for when you are working on the computers in Olsson 001 or any ITC machine. In Cygwin, your Windows desktop is at /cygdrive/c/Documents\ and\ Settings/Aaron/Desktop/; your home directory (your J: drive) is at /cygdrive/j. Lastly, take a look at the object life‐cycle code (on Collab, under Resources, under labs/lab01/code and called lifecycle.cpp). Use it as a mechanism for understanding how various aspects of C++ work. Compile and run the program (in Unix – see step 3a in the in‐lab section). Try stepping through it by hand. Use Weiss CFJP, the web, and any other C++ references to help you look up parts of the program you do not understand. Linux in Olsson 001 This section describes how to log into Linux in Olsson 001. Although this is really part of the in‐lab, you won’t be able to access this document until you have logged in, which is why the directions are listed in the pre‐lab section. The machines in Olsson 001 have two operating systems installed – Windows XP and Linux (the Ubuntu distribution). You have accounts on each, although the passwords will be separate. You will need to reboot the computer into Linux – to do this, do a regular reboot from Windows. Upon the reboot, after the Dell logo screen is shown, a boot loader screen will appear, allowing you to choose which operating system to load. There are a number of choices, but the one we are interested in is the Ubuntu choice (there are two – ignore the ‘safe mode’ version). You will note that the default selection is Windows – you will have to use the arrow keys to select the Ubuntu line, and then hit Enter. Once the login screen appears, you can log in. Be aware that it may take a full minute to log you into the machine. Linux has a number of desktop environments that one can choose from – each of these has a different look‐and‐feel, in the same way that Windows XP is different from Mac OS X. We are currently using Gnome (pronounced ‘ga‐nome’, not ‘nome’). We will later see another one called KDE, and you will be able to choose whichever one you prefer more. In Gnome, you will notice that the task bar is at the top. There is a Firefox icon there that you can use to launch a web browser to log into Collab ( and complete the lab. You will also need to load up a terminal window – to do this, click on ‘Applications’ at the top, and select Accessories‐ >Terminal. When you are finished, you should log out (from the menu at the top, or you can hit Ctrl‐Alt‐Backspace), and reboot the computer (through the options menu at the bottom of the login screen). CS 2150, fall 2009 Page 5 of 9 In­lab General In­lab Procedure 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. Note that each file you electronically submit (including text files!) should contain a header containing your name, email ID, file name, and the date. All of the files required for this lab are in the Resources section of the Collab site (under ‘labs/lab01’). Understanding C++ 1. Ask the TAs if you have questions about your xn function. 2. Ask the TAs if you have questions about using Unix. 3. Object Lifecycle Program a. Download the lifecycle.cpp file from Collab ( Compile with g++ (i.e., ‘g++ lifecycle.cpp’) and execute the program. b. Break lifecycle.cpp into multiple files. It may be helpful to look at class slides on the Collab site. Compile and run this program. Note that to compile a C++ program with multiple files, just put them on the same line: ‘g++ LifeCycle.cpp TestLifeCycle.cpp’ (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: i. LifeCycle.h ii. LifeCycle.cpp iii. TestLifeCycle.cpp c. Comment out the prototype for getMaxMyObj. Recompile. What happens and why? Talk to TA if you are unsure. Now un‐comment the prototype. d. 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. 4. Using C++ vector container class with strings a. There are 3 files on the Collab site that demonstrate the use of multi‐file programs and the use of the vector collection class in C++: svutil.cpp, svutil.h, and svtest.cpp. This class 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 ‘g++ svutil.cpp svtest.cpp’ will compile the program. b. Comment out the #include <iostream> preprocessor directive in the file svtest.cpp, and then rebuild the program. Was there an error? CS 2150, fall 2009 Page 6 of 9 c. Now undo what you did in the previous step, but now comment out the using namespace std; in svutil.h, and rebuild the program. Was there an error? What objects are now undeclared and why? d. 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 Troubleshooting Does your program not compile? Here are a few things to try – these are problems that previous students have encountered. • • • You must have #include <iostream> at the beginning of each file (well, really each file that uses cin and cout, but it doesn’t hurt to have it at the beginning of each file). Likewise, you will need using namespace std; at the beginning of each file. 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. Make sure that your subroutine names (whether they be function names or method names) are consistent between the .h files and the .cpp files Have a compiler error that you don’t understand? We have the translation! See the wiki 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. • • Assignment submission All assignments will be submitted through Collab’s Assignments tool. Each assignment has 3 dates: an open date (when you can start submitting the assignment), a due date (when it’s due), and a accept until 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). Pre‐labs Open date Sunday at noon Due date Accept until date Upon the start of your 24 hours after the start lab session of your lab session In‐labs The start of your Upon the completion 24 hours after the end lab session. of the lab session your lab session Tuesday at noon Friday at noon Saturday at noon Post‐labs Please note, however, that there are a number of rules that we will strictly follow: CS 2150, fall 2009 • • • • Page 7 of 9 Pre‐labs are due at the time that your scheduled lab session starts, regardless of what Collab states. In‐labs are due at the end of your scheduled lab session, regardless of what Collab states. See the part about in‐lab extensions, below. In‐labs MUST be done from the lab in Olsson 001, or zero credit will be received for the ENTIRE lab. Any late lab part will receive 25% off (for just that part) for the first 24 hours that it is late, after which no credit will be given. Note that a computer program does this deduction – so if your lab is 2 seconds late, it still receives 25% off. Collab does not handle due dates such as “at the end of your lab session” very well – indeed, Collab lists the due date for pre‐labs as 7:30 p.m. on Tuesdays (the start of the last lab session). Note that we have a script that will test the assignment’s due date against your lab date, so please be sure to follow the above due dates. Home Directory You must have a working home directory account for this class. Currently, the easiest way to access home directory is to log in through the web interface at home.php. We are working to make an easier way to log in to home directory for Linux, but it is not ready yet. Submitting your files All assignments for this course will be submitted through the Assignments link in Collab. There is no check to make sure you have submitted all of the correct files – on the ‘Procedure’ page (always the second page of the lab document), we clearly state which files should be submitted for each part. For example, for this in‐lab, you should submit the following files for in‐lab 1: lifecycle.questions.txt, vectors.questions.txt, LifeCycle.h, LifeCycle.cpp, and TestLifeCycle.cpp. Lastly, you should ensure that your program compiles before you submit it. This is an advanced programming class, and there is no reason why you should submit a program that does not compile. If you are having problems with your code, you should comment out parts so that it does compile – you will receive some credit for a program that has part of the code commented out than a program that does not compile. Just to be clear: you will receive zero credit for a lab component which does not compile. Resubmitting your assignment If you submit your assignment, and you realize you made a mistake (didn’t submit all the files, etc.), you can resubmit your assignment. 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 ENTIRE assignment will have the late submission date. The directions for resubmitting an assignment are listed in the Collab wiki – it’s not very intuitive, but it works. CS 2150, fall 2009 Page 8 of 9 CS 2150, fall 2009 Page 9 of 9 Post­lab Complete the exercise below, and submit the text file described there to the submission system. For this lab you will be submitting your code electronically via Collab. Your fully functional code is due by noon onthe Friday of the week of the lab. Be sure to include: your name, the date, and the name of the file in a banner comment at the beginning of each file you submit. Linked List Template code Download the four linked list files, from Collab (, which are from Weiss (dsexceptions.h, LinkedList.h, LinkedList.cpp, and TestLinkedList.cpp). Compile and run the program. The compile command is ‘g++ TestLinkedList.cpp’ – if you try to compile the LinkedList.cpp file, it won’t work correctly. And the dsexceptions.h file is necessary for this (you don’t have to understand it, but it must be present for the program to compile). Find a #include statement at the end of LinkedList.h. This is not the way that #include is typically used. We normally only include files that contain declarations of classes, constants, function prototypes, etc., but not definitions of C++ functions (i.e. the bodies of the functions). However, when implementing template classes, this is something that is necessary to make the class compile successfully. Look more closely at TestLinkedList.cpp. The line: List<int> theList; declares a List of integers, called theList. The line: List<int> list2; declares another List of integers, called list2. Now all the functions in the List class will be available to theList and list2. In LinkedList.h and LinkedList.cpp, everywhere “Object” is used, int will be used instead. 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. ...
View Full Document

This note was uploaded on 09/25/2009 for the course CS 216 taught by Professor Bloomfield during the Spring '08 term at UVA.

Ask a homework question - tutors are online