CS 101
PreLab 6


Name: ___________________________________

Netid:____________________________ Section: _______________

Complete and hand in Prelab 6 at the beginning of Lab 6. You should use the material found in Lecture 8 and Lecture 9 and 10 (slides 14-21) of the course notes. If you are not sure of the answer you can ask anyone of the CS101 staff for help.

Objectives


 

Part A: Basic UNIX commands

For this part of the prelab, refer to the lecture notes for Lecture 8 and the beginning of Lecture 9. For more readings see the CCSO Unix Tutorial. 
In particular, you should understand the concepts of the home directory and of the current or working directory.
  1. Let's say you are currently in your home directory. Write a UNIX command to list all the files and directories in your home directory. Don't worry about file names that begin with a  '.' (a dot).


    ___________________________________________________________

  2. Still in your home directory, write a UNIX command to list all the files that end with '.m' ( Matlab files)


    ___________________________________________________________

  3. Write the UNIX command that displays the name of your current directory. (a.k.a. print working directory)

    ___________________________________________________________

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


  5. ___________________________________________________________
     
  6. Now, assuming that you are still in your home directory and you have a file there named lab6.c, what UNIX command would you use to move this file to the lab6 directory?
  7. ___________________________________________________________
     

  8. Still in your home directory, without using the cd command to change directories, write the UNIX command to list the contents of your lab6 directory.

    ___________________________________________________________
     

  9. Say you just used the cd command to change to the lab6 directory. Without using the cd command to change back to your home directory, write the UNIX command to list the contents of your home directory.

    ___________________________________________________________
     

  10. You are still in the lab6 directory. Now you want to copy the file lab6solutions.c, which is in your home directory, to the lab6 directory. Write the UNIX command to copy this file to your current directory. Do not use the cd command to change back to your home directory.
  11. ___________________________________________________________
     

  12. You are back in your home directory again and you decide to delete the lab6.c file from your lab6 directory. Write  the UNIX command would to delete the file from its directory. Do not use the cd command to change to the lab6 directory.
  13. ___________________________________________________________

Part B: C Programming Language

Refer to the Lab 6 instructions if you want to test your C program code.

Every C program has a main function. A very simple C program is:

#include <stdio.h>

void main(void)
{
  int seconds;
  printf("How many seconds: ");
  scanf("%i", &seconds);
  printf("You entered %i seconds ", seconds);
  printf("which equals  %i minutes and %i seconds", seconds/60 , seconds%60);

}

Important!!! Each C statement must have a ; (semicolon) at the end. In Matlab the ; was optional but in C it's requied.
  1. int seconds; ...Each variable you use in your program must be declared in your program. One form of a declaration in C is:
    datatype   variablename;
    where datatype is one of int, float, double, char (only these for now in CS101). The int datatype means integer (-12, 0, 17 are examples of integers). The float and double datatypes mean real numbers (3.14159, 17.0, -3.4, 2.7e-5 are examples).  The float datatype retains a low precision 6-7 digits accuracy and the double datatype retains a high precision 14-15 digits. The char datatype means character (a single character like 'a' or 'A' or '$' or '\n').

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

    ___________  attendance;     /* where the attendance variable holds the daily attendance of Wrigley field */


    ___________  temperature;   /*high precision temperature measurement in Celsius */


    ___________ response;     /* single character response the user enters, either a 'Y' or an 'N' */


  2. printf("How many seconds: "); ...To display a string on the computer moniter use the printf function.
    printf("blah blah blah");
    displays

    blah blah blah

    on the computer monitor.
      printf("You entered %i seconds ", seconds); ... If you want to display the value of a variable you must use the correct conversion specifier that matches the datatype of the variable. For example,
    datatype       conversion specifier
    int                 %i or %d  (for now we will use %i only)
    float              %f
    double           %lf   (NOT %d)
    char               %c  

    For example, suppose you had typed the following C code,
    int x;
    x = 1;
    and you wanted to display

    x = 1

    on your computer monitor, then you could use the printf function as follows:
    printf("x = %i", x);
    Note that the %i doesn't get printed. The value of the variable x replaces the %i.

    Given the code,
    float y;
    y = 1.2345;

    we want to display:

    y = 1.2345

    on the monitor.

    Fill in the following blank with the correct conversion specifier:
  3. printf( "y = ________ ", y);

    Given the code,
    char z;
    z = 's';

    display the string

    Yes

    on the monitor.
    Fill in the following blank with the correct conversion specifier:

    printf("Ye____", z);


  4. printf("which equals  %i minutes and %i seconds", seconds/60 , seconds%60); ... C has only +, - , * ,  /  and % as arithmetic operators. Read lecture 11 slide 16 and answer the following:


    int a = 3;
    int b = 2;
  5. a % b = __________________________

     
    (a+1)%b = _______________________


    a/b = ___________________________


  1. scanf("%i", &seconds); ... To read the value(s) the user typed at the keyboard use the scanf function. One form of the scanf function is,
    scanf("conversion specifier", &variablename);
    Important!!!  You must put an & before the variable name.
    For example, to read a low precision real number the user types at the keyboard into the variable c declared as,
    float c;
    you would write,
    scanf("%f", &c);


    Fill in the blank to read a single character into the variable d declared below.

    char d;
    scanf( "%c" , __________);