COSC 1436 Lab 3
Purpose: This lab is in two parts, the first part focusing on UNIX commands and the second part on writing a C++ programs that uses arrays.
Due: Section 2: Before class on February 12, 2013
Section 3: Before class on February 14, 2013
Requirements UNIX Commands: The first part of this lab involves performing some basic UNIX operations. Here is the answer sheet for the UNIX exercise for this lab: Lab 3 Answer Sheet UNIX Exercise:
1. Open a terminal session through Secure Shell. Change directories to get inside the 1436Spring13/Lab03 directory. Once there, create a “play” directory called 'Play' and open that directory. Once you’re inside the Play directory, create a “play” text file called lab3play.txt that contains at least 50 lines of text. If you don’t want to type a file that large, save an existing file under the lab3play.txt name or copy and paste the text from this lab description. You will be using this file for the UNIX portion of this lab.
2. There are several different ways you can look at the contents of a file without actually having to open the file in a text editor.a. Type in 'more lab3play.txt' and press the <Enter>
key. Describe what you see. At the bottom of the screen, you should see --More-- followed by a percentage in parentheses. What happens when you press the <Enter> key? If you want to stop the display, press the q key.
b. Now type in 'less lab3play.txt' and press the <Enter>
key. Describe what you see. What happens when you press the <Enter> key?Try pressing <Ctrl>v. What happens now? If you want to stop the display, press the q key.
c. Type in 'head lab3play.txt' and press the
<Enter> key. Describe what you see. What does this command do?
d. Type in 'tail lab3play.txt' and press the
<Enter> key. Describe what you see. What does this command do?
3. You have created a variety of files and directories over the past couple of weeks (and semesters). How can you get rid of unneeded files and directories if you need more space? You can do this using the rm command for files and the rmdir command for directories.
a. If you want to delete a directory in your penguin account at the command line, the syntax is:
where directoryName is the name of the directory you want to delete.
b. Get out of the 'Play' directory (but stay within the Lab03 directory). Try typing 'rmdir Play' and press the <Enter>
key. Describe what you see. Do you think this is a good idea? Why or why not?
c. Now change back into the 'Play' directory.
d. If you want to delete a file in your penguin account at the command line, the syntax is:
where filename is the name of the file you want to delete.
e. Type rm lab3play.txt and press the <Enter>
key. Now do a listing of the files in the 'Play' directory. What do you see? Do you think you’d be able to delete the 'Play' directory now?
f. Be very careful when deleting files and directories in UNIX! UNIX is a multi-user operating system. That means there’s a chance that just as soon as you delete a file or directory, that space is immediately allocated to someone else making a new file or directory. So, if you accidentally delete something, it is well and truly gone forever and cannot be retrieved.
g. There is one command you never ever want to type. The command rm *.* will delete everything in the directory in which it is typed. In the next lab, we’ll look at what the asterisk (*) means in UNIX.
1. Write a program that will take as input a decimal integer number in the range of 0 to 65,535 (2 to the 16th power) and convert that number into a new base. The new base may be any base from 2 to 36. The specific requirements are as follows:
- You will be using a function to do the conversion. The function will take in the number, the base the number is to be converted into, and pass back a 16 element character array (for my advanced students, this is not a c-string array it is to be an array that stores 16 characters). Here is the function prototype I used:
- void convertBase(int iNum, int iBase, char caNumbers);
- We will develop the pseudocode for this function at the beginning of the lab.
- We will be using fixed length arrays of 16 elements for all arrays in this lab. These arrays may not be declared globally, but must be passed between the functions as needed.
- When a digit value exceeds 9, use upper-case letters. For example:
- 10 becomes A
- 11 becomes B
- 12 becomes C
- 36 becomes Z
- This program does not need to work with negative numbers.
- C++ has several library functions which will do these conversions, you may not use these functions.
- Here is a file that shows my output from this program. This output can definitely be improved upon, for example, suppressing the leading zeros. Bonus points will be awarded for superior output.
- Some hints to assist:
- Remember to validate all input
- Document your code
- Do not do any printing in any function, except for debug purposes that is commented out for the submitted program.
- You will probably need debug code inside your conversion function to display interim results
- You will need to reverse the array. This for loop will help you do this:
- for (int i = 0, j = MAX_ARRAY_SIZE -1; i < (MAX_ARRAY_SIZE/2); i++, j--)
- In this loop i starts at zero and goes to the middle of the array and j starts at one less than the size of the array (the last valid index) and counts down to the middle of the array. If you use a temp variable you can then reverse the array.
- The conversion from integers to ASCII characters can be tricky. Use your ASCII values table. The first part of the solution for numbers from 0 to 9, I used this: caNumbers[i] = '0' + iaNumbers[i]; in a loop. This will store the proper value into the character array.
2. BONUS Work. Do not attempt to earn any bonus points until you have met all the requirements of the original lab. Bonus points are not available for late labs or resubmittals. There are two opportunities for bonus points in this lab as follows:
- Superior Output (10 Additional Points). If your output is superior compared to what most students have done, you will receive 10 additional points
- Conversion of Character Array to Base 10 and Base 2 (25 Additional Points). Write an additional function that takes as input the character array that came from your original conversion and converts that array into the original base 10 value and to base 2 and places it into an array of bool's. Print out the values in your main program.The function prototype to use is as follows:
- int convertBack(char caNumbers, int iBase, bool baNumber) ;
- Where the base 10 value is the function return value, caNumbers is the 16 element character array, iBase is the base the number is in, and baNumber is a 16 element Boolean array used for returning the values base 2 values to the calling program.
- Negative Numbers (25 Additional Points). Make your program work properly for negative numbers. In this case the range of values is 32,767 to -32,768. For base 2 (binary), your output should be in 2's - complement notation. For all other bases, you may place a negative sign in front of the number.
3. Your program must compile and run on clst.tamucc.edu.
4. This lab requires a lab report/documentation package. All of these items are not typically in the same file (source code and sample output should definitely be in their own files), but the main lab report file must reference each item by file name. Here is a sample lab report. The main lab report must either be a text file (with a .txt extension) or a Microsoft Word file (with a .doc or .docx extension). The lab report must includes the following or references (the file name that was attached to the mail) to the following information:
- Your Name
- Purpose of this program.
- Program source code
- Program output. The output must be from the clst.tamucc.edu Linux server, not from Windows.
- Pseudocode. I only need the pseudocode for the conversion function, not the entire program.
- What problems/successes did you have with this program?
- An estimate of how much time you spent working on this lab
4. Submit this package via BlackBoard. Zip all of the required elements together into one file that will be submitted to Blackboard:
- The source code for your programs, called lab03***.cpp. Put your initials where the 3 asterisks appear. This files must be ready to compile using the standard g++ compiler on clst.tamucc.edu.
- A digital copy of the program output, called lab03***.txt. Put your initials where the 3 asterisks appear.
- A copy of your lab report in either a Microsoft Word document or a text file called lab03***doc.txt or lab03***doc.doc. Put your initials where the 3 asterisks appear.Grading Criteria: 100 points available for this lab. See bonus points sections for details on possible bonus points.
- The lab will be graded using this grading criteria. The points will then be scaled to the available points.
- Here is a C++ style guide from Dr. Fernandez that should assist you in coding your program. Style guide.
- 100 points if all of the above requirements are met.