Week 2
word.c
1: #include <string.h>
2:
3: /* word.c - represent a word in memory as a structure */
4:
5: typedef struct word_struct{
6: char* letters;
7: int length;
8: } word;
9:
10: word* init_word(char* charstr) {
11: word* new_word;
12: new_word = (word*) malloc(sizeof(word));
13: new_word->length = strlen(charstr);
14: new_word->letters = (char*) malloc(sizeof(char)*new_word->length);
15: strncpy(new_word->letters, charstr);
16: }
17:
18: void free_word(word* w) {
19: free(w->letters);
20: free(w);
21: }
22:
23: int equals(word* a, word* b) {
24: if (a->length != b->length) {
25: return 0;
26: }
27: if (strcmp(a->letters, b->letters) == 0) {
28: return 1;
29: } else {
30: return 0;
31: }
32: }
33:
34: void print(void) {
35: printf("%s\n", letters);
36: }
word.cpp
1: #include <string.h>
2: #include <iostream.h>
3:
4: // word.cpp - represent a word as a Word object type
5:
6: class Word {
7: public:
8:
9: Word(char* charstr) {
10: length = strlen(charstr);
11: letters = new char[length];
12: strncpy(letters, charstr, length);
13: }
14:
15: ~Word() {
16: delete letters;
17: }
18:
19: int equals(Word* w) {
20: if (w->length != length) {
21: return 0;
22: }
23: if (strcmp(w->letters, letters) == 0) {
24: return 1;
25: } else {
26: return 0;
27: }
28: }
29:
30: int equals(Word* w1, Word* w2) {
31: if (w1->length != w2->length) {
32: return 0;
33: }
34: if (strcmp(w1->letters, w2->letters) == 0) {
35: return 1;
36: } else {
37: return 0;
38: }
39: }
40:
41: void print() {
42: cout << letters << endl;
43: }
44:
45: private:
46: char* letters;
47: int length;
48: };
- Find 5 things different between word.c and word.cpp.
- How do you make a multi-line comment in C? in C++?
- How would you print the length of a word?
- How do use a get and use a new word? Free up a old word?
- How do you access the length of a word?
- Why are there two equals() methods in the cpp file?
- How do you make a copy of a word?
Debugging
- printf - easy to use, need to add code to see values of variables
- DDD - allows you to look at any variable whenever you want. You can pause the code and watch it run line by line or jump to any section you want.
How to use DDD
- Data Window - show variables values
- Code Window - shows currently code. if you hover your mouse over a variable, its value will pop up.
- gdb Window - you can enter gdb commands here or see the output to standard out/err
- Control Tool
- Breakpoints - set lines in the code where you want the program to stop running. click on the code window to the left of the line of code to add a breakpoint
- Step - go to the next line. if there is a function call, go to the start of the function
- Next - go to the next line. if there is a function call, call the function and return
- Until - keep running until the cursor is reached
Programming Assignment
- Sample input files are included on the webpage.
- You can run a reference binary from ~cs40at/bin/pa1/netlist. The output of your program should match the reference line for line.
- Assignment is due at Sunday 10/17 at 11:55pm. A lateness penalty of (N*5)% will be imposed where N is the number of hours late.
- I won't be answering personal emails about the programming assignment anymore. If you have questions, please post them to the newsgroup. You'll get a faster response since the other students and the instructor will be reading them too.
To submit your project:
- Make sure it compiles with 'make' and creates a 'netlist' binary. If this doesn't work, you won't get any credit for the assignment. If you created new files for the assignment, remember to add them to the OBJ and SRC lines of the Makefile.
- Clean out your binaries with 'make clean'
- Create a README file. Put your name, SID, how many hours you spent on this assignment and your favorite riddle/joke in the file.
- Use handin from the CSIF machines. Run 'handin cs40at pa1 filenames' where filenames is the list of all of code files, including headers, Makefile and README. If your directory only has your code files, you can run 'handin cs40at pa1 *'.
- Example:
[hsuf@ta1 proj1]$ make
[hsuf@ta1 proj1]$ ls -l netlist
-rwx------ 1 hsuf users 23776 Oct 13 15:45 netlist
[hsuf@ta1 proj1]$ make clean
rm *.o netlist
[hsuf@ta1 proj1]$ cat > README
Francis Hsu
990000000
4 hours
Two hydrogen atoms bumped into each other recently.
One said: "Why do you look so sad?"
The other responded: "I lost an electron."
Concerned, One asked "Are you sure?"
The other replied "I'm positive."
^D
[hsuf@ta1 proj1]$ handin cs40at pa1 *
Submitting BoardComponent.c... ok
Submitting BoardComponent.h... ok
Submitting BoardLocation.h... ok
Submitting DataTypes.h... ok
Submitting Makefile... ok
Submitting NetList.c... ok
Submitting NetList.h... ok