Day 12
- Lab 6 is due at end of lab today
- Midterm #2 and Lab Quiz #2 Thursday
- Lecture Topics
int main(int argc, char *argv[])
{
if (argc != 4) {
printf("You need to give 3 command-line arguments!\n");
return 0;
}
printf("Arg 0: %s\n", argv[0]);
printf("Arg 1: %s\n", argv[1]);
printf("Arg 2: %s\n", argv[2]);
printf("Arg 3: %s\n", argv[3]);
printf("Arg 3: %d\n", atoi(argv[3])); /* #include<stdlib.h> */
return 0;
}
Exam Review
- Covers chapters 5, 6, 8, 9, and 12 in the text
- Written exam - bring a pencil
- Multiple choice, true/false, fill-in-the-blank
- Trace code (e.g. what does it print?, what do variables contain?)
- Write code segments, functions, tests, and programs
- Study suggestions:
- Read chapters in the book
- Review Chapter Review sections
- Do quick check exercises in book
- Form a study group - teach/explain things to each other
- Keep a vocabulary words list
Lab Quiz
- Solve a problem similar to the examples in the lecture notes and below
- Write a complete program including checkit_* tests
- Pass/fail grade
Example Exercises - write tests for functions as appropriate
- Write a program that reads up to twenty ints and prints them in reverse order
- Write a function that sums the elements in an array
- Write a function that sums every other element in an array
int sumEveryOther(int nums[], int size)
{
int i, sum = 0;
for (i = 0; i < size; i = i + 2) {
sum = sum + nums[i];
}
return sum;
}
Write a function that returns the smallest difference between consecutive elements in an array
int smallDiff(int nums [], int size)
{
int i, diff = 0, smallestDiff;
smallestDiff = nums[1] - nums[0]; /* assumes size > 1 */
for (i = 0; i + 1 < size; i++) {
diff = nums[i+1] - nums[i];
if (diff < smallestDiff) {
smallestDiff = diff;
}
}
return smallestDiff;
}
Write a program that reads up to twenty ints or until it reaches the end of file (assume input redirection), then print the results of one of the other function exercises
Write a program that reads up to twenty ints or until it reaches the end of file (assume reading from the file "input1.txt"), then print the results of one of the other function exercises
Write a program that reads a 5 x 5 grid of ints, and returns the sum of the descending diagonal (left top to right bottom)
Write a function that accepts a two dimensional array and the sizes of the two dimensions, then returns the sum of descending diagonal if the sizes are equal
Write a function that accepts a two dimensional array of ints, the sizes of each dimension, and a column index, then returns the sum of the elements in the column
int sumColumn(int nums[][MAX_SIZE], int dim1Size, int dim2Size, int col)
{
int i, sum = 0;
for (i = 0; i < dim1Size; i++) {
sum = sum + nums[i][col];
}
return sum;
}
Change sum to product in the appropriate exercises above
Change int to double in the appropriate exercises above
Write a function that swaps two strings
void swap(char one[], char two[])
{
char tmp[SIZE];
strcpy(tmp, one);
strcpy(one, two);
strcpy(two, tmp);
}
Write a function that swaps two ints
Write a function that swaps two doubles
Write a function that swaps two chars
void swapChars(char *one, char *two)
{
char tmp;
tmp = *one;
*one = *two;
*two = temp;
}
Write a program that reads and sums ints until the user enters a negative number
Write a program that reads chars into an array until it receives the char '!', then print the chars in reverse order
Write a program that reads chars into an array until it receives the char '!', then print the number of times each char in the array occurs