Lab 6 Answer Sheet

This lab is to be worked individually.

Name: ________________________ Netid: _______________ Section: _______


Part 1: Prelab

Each blank worth 1/5 point (16 blanks * 1/5 = 3 points + one extra blank).

 

Part A: Basic UNIX commands


1. Let's say you are currently in your home directory (directory named after your netid. you can type 'cd ~/' and hit enter to move to your home). Write a UNIX command to list all the files and directories in your working directory.


_______________________________ls____________________________

2. Now suppose you also want to know the size of all the files in your working directory. Write a UNIX command that will list all files in your working directory listing the size of the files.


_______________________________ls -l____________________________

3. In general, a simple list command will not display the files and folders whose names start with a dot. You can, however, tell 'ls' to display these hidden files. Write a UNIX command to list all the files and directories in your home directory, including the files whose names begin with a '.' (a dot).

_______________________________ls -a____________________________

4. Still in your home directory, write the UNIX command to create a new subdirectory called prelab6 that is inside your home directory.


_______________________________mkdir prelab6____________________________

5. Now, assuming that you are still in your home directory and you have a file there named prelab6.c, write a UNIX command to rename this file to the pre6.c

_______________________________mv prelab6.c pre6.c____________________________

6. Write a command to make another copy of this file pre6.c to the new subdirectory named prelab6. 

_______________________________cp pre6.c prelab6/____________________________

7. Write a command to copy the entire prelab6 directory to a directory called prelab6_copy in your home directory.

_______________________________cp -r prelab6 prelab6_copy____________________________

8. Still in your home directory you decide to delete the pre6.c file from your home directory. Write the UNIX command to remove the file from its directory.

_______________________________rm pre6.c ____________ OR ______________ rm ~/pre6.c____________________________

9.    Oops! We didn't want to delete that file! Luckily we have another copy of the file. From your home directory,  copy the file pre6.c, which is in your prelab6 directory, to the home directory with a new name p6.c. Write the UNIX command to copy this file to your current directory with the new name. Do not use the cd command to change back to your home directory.

_______________________________cp prelab6/pre6.c p6.c____________________________

10. We were luck we had a copy of pre6.c when we "accidentally" removed it, however, UNIX does not have a recycle bin, so we should be careful when removing files, specially when we use the -r flag to remove everything (including sub-directories) from a directory. Write an UNIX command that uses the -i flag to remove the prelab6_copy folder while resquesting confirmation before removing each file or folder.


_______________________________rm -ri prelab6_copy _______ OR ________ rm -r -i prelab6_copy ___________

11. Still in your home directory, write a UNIX command to list all the C files that begin with letter 'p' (C files end with '.c').


_______________________________ls p*.c____________________________

12. Use the cd command to change to the prelab6 directory. Without using the cd command again to change back to your home directory, write the UNIX command to list the contents of your home directory.

_____________________________ ls ~______________________________

                                                OR
_____________________________ ls ..______________________________



Part B: C Programming Language

13. Fill in the blanks below with the most appropriate datatype to declare each of the variables below.

___ int________ studentNo; /* the number of students in a class */


____ double_______ avgScore; /* the average score of a class in CS101 midterm (a high precision real number) */


____ char_______ gender; /* single character indicating the gender of a student, either a 'M' or an 'F' */

14. Fill in the following blank with the correct conversion specifier:

printf( "y = __ %f___ ", __ y___ );

Fill in the following blank with the correct conversion specifier:

printf("UIN: __ %i__, Gender:__ %c__", ___ UIN__, ___ gender_);


OR

printf("UIN: __ %d__, Gender:__ %c__", ___ UIN__, ___ gender_);

15.
int a = 11;
int b = 4;

a % b = ________ 3__________________

(a+1)%b = _____ 0__________________

a/b = __________  2________________

16. Fill in the blank to read a single character value into the variable s and a single double value into variable t declared below.

char s;
double t;
scanf( "%c" , __ &s________);
scanf("_____ %lf_______", ____ &t______);

 

Part 2: Navigating the filesystem with the command prompt


6 points total. Each question is worth 1 point except for question 5 which is worth 2 points.

 

1. Write the commands to navigate to your home directory from the root directory:

Accept a command(or multiple commands) that would get the student to the same path as if they had typed the command: cd

 

2. Write the error message(s) that gcc shows:

lab6.c: In function ‘main’: no deduction if student omits this line
lab6.c:12: error: expected ‘;’ before ‘else’
the line number 12 may differ depending on whether the student added extra blank lines

3. Write what happens when you run your program for the first time after it compiles:

Segmentation Fault


or ... program terminates without producing output or "nothing" or anything equivalent.




4. Write the output your corrected program now produces: __________7 is odd__________________________

 

5. ( 2 points) Change the line of code

 if (x % 2)

so that we are testing for x being a multiple of three rather than two .


Write the new line of code: ________________________if ( x % 3)_________________________________________________________