Note: the scoring on this website does not seem to be working, but try the exercises anyway and check with your neighbour or tutor if in doubt.
Remember as mentioned in lectures ++ is a pre or post increment operator, so i++ is the same as i=i+1. Also, when declaring (or prototyping) a function, including the names of the parameters is optional - including them is good practice (and should be done in the assignment), but their inclusion is not mandatory. The types of the parameters, however, do need to be included. Example: double sqrt(double); is a valid function prototype.
cp /dept/dcs/comp2300/public/lab2/* ~/comp2300/lab2/
to copy the files required for the lab into your lab2 directory. Check with ls which files have been copied. Note: The wildcard * is used in Unix as a placeholder for any name (similar, A* would stand for all files beginning with an A).
We will use an editor, e.g. emacs or kate, to edit the source code (*.c files) and then manually issue the commands to compile and link the modules. These commands can be put in scripts or a Makefile but we won't worry about that in this lab.
Towards the top of the file is a #define statement for the macro called MY_NAME. Change the replacement text for this macro to your name (in quotes), and save the file. Example: #define MY_NAME "Fred Smith"
Start by compiling program1.c using the following command
gcc -Wall -o program1 program1.c
Check that the executable was produced (there should be a file called program1 in your current working directory), and then run it by typing the command ./program1 (the ./ at the beginning is needed to tell the terminal shell that the executable is in the current directory). What's wrong with the output?
Note that the source code for the program contains a prototype for a function called length(), which takes a string (an array of char) as a parameter, and returns an int. Replace the 0 in the second printf() statement with a call to the function called length(), with MY_NAME as the parameter. Write an implementation of the length() function, putting it at the bottom of the file.
To determine the length of the string passed to it as a parameter, the function can use a while loop to step though the elements of the char array, until it finds a null character ('\0') - the not equals operator != will be useful here. The number of loop iterations is then the length of the string.
Compile and link the program again, using the same commands as before. Then run it and check that it works. If it won't compile or doesn't give the correct output, check everything you've done and try to determine the problem. If you've spent more than ten minutes trying to fix it without success, ask your tutor for help.
#include <stdio.h>
#include <ctype.h>
int main() {
char name1[]="My-COMP2300-Notes\\";
char name2[]="28-July-2003 \\noon\\";
int i, nlower=-2, nalpha=-4;
printf("C Programming Example\n" /* );
printf("Comparison Strings" */ );
printf("%s\n",name1);
printf("%s\n",name2);
for (i=0;i<15;i++){
if (islower((int) name1[i]) || islower((int) name2[i])) nlower++;
if (isalpha((int) name1[i]) && isalpha((int) name2[i])) nalpha++;
}
printf("Total number of lower case =%5d\n",nlower);
printf("Total number of alphabetical =%5d\n",nalpha);
return 0;
}
Now copy the code into a file (e.g. called exam.c)
compile and run it in the same way you did for
program1.c. Does your prediction agree with the
observed output? If not, then your prediction is wrong (not
the program)!! Consider carefully the code until you understand
exactly what it is doing and why it is giving the printout it
does. If necessary talk with your tutor as it is important that
you understand this.The exam question went on to ask you to write C code for two functions
int myislower(int c);
int myisalpha(int c);
that perform exactly the same operations as the library
routines islower() and isalpha(). Try and do this
for at least one of these functions. In both cases the code you
write should be less than about 10 lines of code. Check that it
works correctly by replacing the calls to islower() and
isalpha() with calls to myislower() and
myisalpha(), compiling the revised code and ensuring
that it gives the same result.
(If you have managed to complete this question without errors
you would have scored 11 marks out of the 100 on offer in the 2003
years exam; however, not many people managed to get this
completely correct!)
./program2 < rainfall_data.txt
to run the program, with its input being taken from the file rainfall_data.txt. Note that the output says the average rainfall was -999.99mm; if you use emacs or less to view the contents of rainfall_data.txt, you can easily see that this average is wrong.
Open the file called program2.c in emacs. There are three function prototypes, one of which has an existing but empty implementation. Complete the implementation of the first function, then check that your program still compiles and runs. Implement the other two functions, and make changes to the statements in main() so that these functions are called when appropriate.
Apart from this, do not alter otherwise the printf() statements, or add any other printf() statements. Your program will be automatically marked on the correctness of its output.
You will now need to customize your command line environment for this course (including being able to access the previewAutoMark command below). To do this, execute the command emacs ~/.cshrc &, and add to the file the line:
Once you are satisfied your program is running, test it for correctness with the command:
Once you are satisfied (or in any case make an initial submission before the end of your session, even if on an unmodified version of the program, to test the submission system), submit it using the submit command:
submit comp2300 lab2 program2.c
This is due by 10am Monday March 12 (the deadline is strict) and will contribute up to 1% of your Tute/Lab mark.
This command does an ssh onto one of the Sun systems, and may require you to type in your ANU password. Look carefully for any error messages - just because it accepted your password it does not mean the submission suceedeed! It is recommended that you always log into StReAMS to verfiy any important submission. If you have problems with submission, TELL YOUR TUTOR. It is important that we fix this ASAP!.
A good place to start programming is to get the quit option working, i.e. read in the option string and either drop out of the loop if quit was entered or go back to the top of the loop if it wasn't (you'll need to use strcmp() in the while() condition to test this).
Once you have done this much, compile and link your program and test it. Once you are sure the quit option works, make the changes needed to call the functions corresponding to the other options, and then compile and link again to test the changes.
Is your code case sensitive? Should it be? Do you want to compare the entire character string or just the first n bytes? Take a look at strcasecmp() and strncasecmp()
Last modified:
Mon Mar 5 17:12:34 EST 2007