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
- As you leave the wonderful world of Matlab behind and enter the
even
more
wonderful world of C, you will need to get acquainted with several new
programs. One of these is the the Eclipse/CDT IDE
(Integrated Development Environment) , which you will use for
the rest of the semester to write your C programs. This week's prelab
&
lab will introduce you Eclipse, as well as teach you some basic
UNIX commands.
.
Each blank worth 1/6 point (18 blanks * 1/6 = 3 points).
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.
- 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).
ls
- Still in your home directory, write a
UNIX
command to list all the files that end with '.m' ( Matlab files)
ls *.m
- Write the UNIX command that displays the name of your current
directory. (a.k.a. print working directory)
pwd
- Let's say you are currently in your home directory. Write the
UNIX command to create a new subdirectory called lab6 that is
inside your home directory.
mkdir lab6
- 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?
mv lab6.c lab6/
or mv
lab6.c lab6 or mv lab6.c ./lab6 or mv lab6.c ./lab6/
- 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.
ls lab6/ or ls lab6 or ls ./lab6/ or ls ./lab6
- Say you just used the cd command to change to the lab6
directory. Without using the cd command to chage back to your home
directory, write the UNIX command to list the contents of your home
directory.
ls ../ or
ls ~/ or
ls ~netid/ or ls .. or ls ~ or ls ~netid
( here netid is your university netid, not the other
students')
- 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.
cp ../lab6solutions.c . or cp ~/lab6solutions.c . or
cp ~netid/lab6solutions.c .
- 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.
rm lab6/lab6.c or rm ./lab6/lab6.c or rm ~/lab6/lab6.c or rm ~netid/lab6/lab6.c
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.
- 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.
___int________
attendance; /* where the
attendance variable holds the daily attendance of Wrigley field */
____double_______
temperature; /*high precision temperature
measurement in Celsius */
______char_____
response; /* single character
response the user enters, either a 'Y' or an 'N' */
- 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:
printf( "y = __%f______
", 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%c____", z);
Fill in the following blank with the correct conversion specifier:
- 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;
a % b = __________1________________
(a+1)%b = ________0_______________
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" , ___&d_______);