Exam 2
Like Exam 1, the focus of Exam 2 is on C programming; evaluating
expressions, knowing type of expression, tracing through code,
problem solving and writing code. You may want review my
Some Tips for Taking CS21 Exams.
Exam 2 Topics
The topics that will be covered on the exam are:
arrays:
-------
ordered, homogenous collection of data
actual size vs effective size
declaring and accessing array buckets:
float farray[30];
farray[4] = 6.3; // index must be an int
passing arrays to functions: passes base addr of array argument
always pass array's effective size
int foo(int array[], int size)
2D arrays:
int array[ROWS][COLS];
array[i][j] = 7;
function prototype w/2D must specify the column dimension:
void foo(int array[][COLS], int num_rows);
sorting and searching:
should be able to implement simple sorts given a description of
their algorithm (ex. bubble, selection)
should know linear and binary search
should be able to say something about the complexity of search
and sort algorithms (big-O)
pointers:
---------
declaring pointer variables, & operator, * dereferencing operator
int *p, x;
p = &x; // p points to x
*p = 6; // store the value 6 in the location pointed to by p
pass by reference: pass address of argument
function parameter is pointer to function argument
parameter's type is pointer to argument's type
dynamic memory allocation: malloc, free, heap memory vs. stack memory
int *arr, i;
arr = malloc(sizeof(int)*10); // dynamically allocated array of 10 ints
if(arr != NULL) {
for(i=0; i < 10; i++) { // use the dynamically alloced array
arr[i] = 0;
}
...
free(arr); // free heap space when done using it
}
pointer arithmetic: you should be able to trace through something like this:
int array[10];
int *ptr;
ptr = array; // or ptr = &(array[0]);
for(i=0; i < 10; i++) {
printf("%d\n", *ptr);
ptr++;
}
char:
-----
know that chars are encoded as numeric values using ASCII encoding
know that '0'-'9', 'a'-'z', 'A'-'Z' are contiguous (you do not need
to know their ASCII values)
char ch;
for(ch = 'a'; ch < 'z'; ch++) {
printf("%c", ch);
}
ch = getchar(); // read in a character from stdin
know what ctype.h functions do: toupper, isdigit, etc.
-------
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)
// these can store strings of length 29 or less
// (need extra char for '\0')
char str1[30]; // a statically declared string
char *str2; // a dynamically declared string
str2 = malloc(sizeof(char)*30);
strcpy(str1, "Jo Schmoe"); // strcpy 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 < 30) {
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
DO NOT USE the Book Library GetLine function, String Library functions, or
string type on the exam (we have not talked about any of these in class).