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.
· 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 gcc compiler and gedit editor, which you will use for the rest of the semester to write your C programs. This week's prelab & lab will introduce you gcc and gedit as well as teach you some basic UNIX commands.
First, you'll need to open a terminal. Follow the Linux menu at
the top left of the screen to Applications->System
Tools->Terminal.
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
(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.
___________________________________________________________
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.
___________________________________________________________
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).
___________________________________________________________
4. Still in your home directory, write the UNIX command to
create a new subdirectory called prelab6 that is inside your home
directory.
___________________________________________________________
At the Unix command line type the following command:
gedit &
When the editor window opens copy/paste the following code into
the editor and then save the file with the name prelab6.c and
close the editor.
#include <stdio.h>
void main(void)
{
int apples;
printf("How many apples? ");
scanf("%i", &apples);
printf("You entered %i apples ", apples);
printf("which equals %i dozens with %i more apples", apples/12
, apples%12);
}
To see what
this file does, at the Unix command line enter the following
commands:
gcc
prelab6.c
./a.out
Though, the
output of the file does not matter. At this stage, we learn how
to use basic Unix commands like cp, mv, cd and rm on any file in
Questions 5-11.
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
___________________________________________________________
6. Write a command to make another copy of this file pre6.c to the new subdirectory named prelab6.
___________________________________________________________
7. Write a command to copy the entire prelab6 directory to a directory called prelab6_copy in your home directory.
___________________________________________________________
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.
___________________________________________________________
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.
_____________________________________________________________
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.
___________________________________________________________
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').
___________________________________________________________
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.
___________________________________________________________
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 apples;
printf("How many apples? ");
scanf("%i", &apples);
printf("You entered %i apples ", apples);
printf("which equals %i dozens with %i more apples", apples/12
, apples%12);
}
Important!!! Each C statement must
have a ; (semicolon) at the end. In Matlab the ; was optional
but in C it's requied.
13. int apples; ...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.
___________ studentNo; /* the number of students in a class */
___________ avgScore; /* the average score of a class in CS101
midterm (a high precision real number) */
___________ gender; /* single character indicating the gender of
a student, either a 'M' or an 'F' */
14. printf("How many apples? ");
...To display a string on the computer monitor use the printf
function.
printf("blah blah blah");
displays
blah blah blah
on the computer monitor.
printf("You entered %i apples ", apples);
... 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 = 10;
and you wanted to display
x = 10
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.23;
we want to display:
y = 1.23
on the monitor.
Fill in the following blank with the correct conversion
specifier:
printf( "y = _____ ", _____ );
Given the following variables,
char gender; /* The gender of the student */
int UIN; /* An integer representing the UIN of a student */
Fill the blanks in the printf statement to print the UIN and
the gender of the student.
printf("UIN:____, Gender:____", _____, ____);
15. printf("which equals %i dozens with
%i more apples", apples/12 , apples%12); ... C has only +, - , * , / and % as
arithmetic operators. Read lecture 11 slide 16 and answer the
following:
int a = 11;
int b = 4;
a % b =
__________________________
(a+1)%b = _______________________
a/b = ___________________________
16. scanf("%i", &apples); ...
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 value into the
variable s and a single double value into variable t declared
below.
char s;
double t;
scanf( "%c" , __________);
scanf("____________", __________);
For
the
first part of this lab, you should start by opening a command
prompt using the menu option
Applications->Accessories->Terminal. Once the terminal
has appeared, you should type the command:
cd /
This
locates
you at the root directory in the computer. It is like the root
of a tree, where all the branches come out from. You can get
to any place on the hard drive from this location. Answer the
question 1 by typing a series of commands (or one command)
that will have you arrive at your home directory. Remember
that you can always check where you are by typing the pwd
command. You are not allowed to use shortcuts such as ~
or typing cd by itself.
For
this
section, please refer to the lecture notes for Lectures 8
,9 and 10.
If you've programmed in C before, you may notice that there's an error in the code. This is done on purpose to show you what happens when you make a mistake while programming.
#include <stdio.h>
int main(void)
{
int x = 0;
printf("Enter an integer: ");
scanf("%i", x);
if (x % 2)
printf("%i is odd\n",x)
else
printf("%i is even\n",x);
return 0;
}
gcc
lab6.c
Your program will ask you to enter a integer. Enter the value
7.
3. Write what happens when you run your program for the
first time after it compiles:
Oops, it looks like we have another problem with our program, and the compiler didn't catch it.
It seemed to happen just after entering the number, so let's look at the line where our program reads the input.
scanf("%i", x);
The problem is that this function expects to be given the location of an integer, but we gave it the integer itself. Since the value of a was zero, the function tried to use memory at location 0, which caused a segmentation fault. (and thus no output)
To fix the error, we put a & in front of the variable x.
scanf("%i", &x);
Save and run your program again and at the Unix prompt type,
gcc lab6.c
./a.out
4. Write the output your corrected program now produces:
_________________________________________
Modify your program so that it detects whether the integer is
a multiple of 3 rather than two.
Change the code,
printf("%i is odd\n",x)
to
printf("%i is not a multiple of three\n",x)
and
printf("%i is even\n",x)
to
printf("%i is a multiple of three\n",x)
5.
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:
_________________________________________
Hint: x % 2 equals the value of the remainder of division of x
by 2. In C , zero means FALSE and ANY other number means TRUE.
Save, compile and run your code and test your new program.
Congratulations! You have now been initiated into the world
of C.
That's it. You're done for Lab 6!