Exam 3
Exam 3 will focus on material covered since exam 2. However, since this
course is cumulative, you should know material from the entire course.
Like the previous exams, Exam 3 is on C programming; evaluating
expressions, knowing the type of expressions, tracing through code,
problem solving, understanding operations on data structures, and
writing code. You may want review
Some Tips for Taking CS21 Exams.
Exam 3 Topics
The main topics that will be covered on the exam are:
---------------------------------------------------------------------------
string: an array of characters with special '\0' terminating character
------- (functions that take a string parameter don't need to be passed
the effective size of the string because they can find the
end of the string by its terminating '\0' char)
char str1[30]; // a statically declared string
char *str2; // a dynamically declared string
str2 = malloc(sizeof(char)*30);
strcpy(str1, "Jo Schmoe"); // adds '\0' to end of string
strcpy(str2, "Joseph Schmoe");
printf("%s %s", str1, str2);
// to read in a string, need to read in one char at a time
i=0;
ch = getchar();
while(ch != '\n') { // read in a line from stdin
if(i < 29) {
str2[i] = ch;
i++;
}
}
str2[i] = '\0';
know what the string.h functions do:
strcpy, strcat, strlen, strcmp, etc.
**I will give you function prototypes from string.h and ctype.h on the exam,
so you don't need to memorize the prototypes, but you should know what
the functions do and how they are called so that you can use them in code
you write or trace through
---------------------------------------------------------------------------
file I/O:
---------
opening, closing, reading from, writing to a file, and rewinding a file.
FILE *infile, *outfile;
int ch; // need to use int, since EOF is -1
infile = fopen("foo.txt", "r"); // open file foo.txt for reading
outfile = fopen("blah.txt", "w"); // open file blah.txt for writing
ch = getc(infile); // read one character value from infile
putc(ch, outfile); // write the character's value to outfile
sequential access to characters in the file stream:
there is a current position in the file stream, reading or writing a
character moves the current position ahead by 1 character in the stream
(to read the 10th character you need to first read the first 9 characters)
rewind(infile); // reset current position to begining of file
special EOF character
---------------------------------------------------------------------------
structs:
--------
A struct is a compound data structure. It allows us to combine items
of many different types into a coherent unit.
use . (or ->) to access fields of a struct variable (or pointer to struct var)
struct gradebookT {
int numgrades;
float average_grade;
char *classname;
int grades[20];
};
typedef struct gradebookT gradebookT;
a struct is an lvalue: can use it on the "left-hand side" of an assignment
statement
it is passed BY VALUE to functions (the value of
fields in the argument cannot be changed inside the
function unless you pass the argument struct by
reference)
this leads to some, perhaps, unexpected results:
for fields that are statically declared arrays,
the entire contents of the array field is passed
to the parameter (changing bucket values inside
the function does NOT change corresponding bucket
values of the argument).
For field values that are pointers to dynamically
allocated space in the heap, the parameter field gets
a copy of its argument's field value (i.e. they point
to the same heap space).
You have to think about type of each field (is it a type or a pointer to
a type, do you need to dynamically allocate heap space for it, do you
access field vaules using '.' or '->', ...)
examples:
--------
gradebookT g1, *g2, g3;
g1.numgrades = 2;
g1.classname = malloc(sizeof(char)*20);
if(g1.classname != NULL) {
strcpy(g1.classname, "CS 21");
}
g1.grades[0] = 90;
g1.grades[1] = 95;
g3 = g1; // note: g1 and g3's name field point to the same string...this
// is often not what you want to do
g2 = malloc(sizeof(gradebookT));
if(g2 != NULL) {
g2->numgrades = 10;
g2->grades[0] = 100;
g2->name = malloc(sizeof(char)*20);
if(g2->name != NULL) {
strcpy(g2->name, "CP 21");
g2->name[1] = 'S';
}
}
---------------------------------------------------------------------------
linked lists:
-------------
You should be able to create, insert at head, at tail, between two nodes,
and insert in sorted order, traverse the list, and search for elements in a
linked list.
An example struct definition for a linked-list:
struct person_node {
int height; // one or more data fields
int weight;
char name[50];
struct node *next; // one field is a pointer to a struct person_node
};
typedef struct person_node person_node;
person_node *head; // only need a single head pointer to the beginning of the
// linked list, sometimes a tail pointer is additionally used
---------------------------------------------------------------------------
Binary Search Trees:
---------------------
We will not ask you to write binary search tree code, but you should know
what a binary search tree is and know how some of the BST operations work
(insert, search).