```

```

## Fall 2009

### Instructions

1. Write your name clearly at the top of each of page of the exam.

2. You must answer all the questions on the exam. The exam has 12 problems. Make sure that you have all 12 problems.

4. You have until the end of the class period to finish the exam. When I call time you must immediately drop your pen/pencil and hand in your exam.

5. Ensure that you write clearly and legibly. Failure to do so may result in the deduction of points.

6. The exam is open note, open book, closed neighbor.

7. No electronic devices are allowed and all electronic devices that you have with you must be turned off.

8. Good luck!

1. (8 points) Write an anonymous struct for a student's record in a class and use typedef to name it "Student". The struct should contain the following fields:

1. name: a string that contains the student's full name. A student's name can be up to 30 characters long.
2. midterm: an integer denoting the student's score on the midterm
3. final: an integer denoting the student's score on the final
4. hw: an array that records the results of 10 homework assignments. The scores on the assignments are all integers.
5. avg: a floating point number denoting the weighted average of the midterm, final, and homework assignments
6. sex: a character denoting the sex (male/female) of the student.
```

```

2. (4 points) Suppose you have the following two declarations:
```      int score;
char *student_name;
```
and assume that score and student_name have been appropriately initialized. Based on the typedef that you just wrote:

1. Declare a variable named new_student, which points to a Student.
2. Malloc a Student struct and assign the pointer returned by malloc to new_student.
3. Assign the value of score to the midterm field in your newly allocated struct.
4. Copy the string pointed to by student_name to the name field in your newly allocated struct.

```

```
3. (3 points) 0x1d08 + 0x6 = _______________ (I want the hexadecimal result)
```

```
4. (8 points) Suppose I have the following declarations and code:
```     int a;
int *b;
char day[20];
char *save_day;

b = &a;
*b = 30;
save_day = day;
```
Also suppose that the above variables are assigned the following memory addresses:
```     a: 0x1000
b: 0x1004
day: 0x101c
save_day: 0x1008
```
After the above code executes, what is the value of:

1. a:
2. b:
3. day:
4. save_day:

5. (3 points) What is the memory address of the last character in day, assuming that a character occupies one byte of memory.
```

___________________
```
6. (21 points) Consider the following declarations:
```       char buffer[100];
char field[20];
char *max_field;
char *state;
char *count;
int *scores;
int area_code;
int
```
Write statements to accomplish the following tasks:

1. do a safe copy of the string in field to max_field.
2. if the string in buffer is small enough to copy it to field without overflowing field's array then do so. Do nothing if the field is too big to copy to field.
3. malloc an array of 50 ints and assign the address of the newly created array to scores.
4. use scanf to read an integer into area_code.
5. use scanf to read a string into buffer.
6. use scanf to read an integer into scores[3].
7. suppose that license_plate points to a string that represents a license plate of the form "state-digits", such as "tennessee-941". Assume that you do not know how many letters precede the dash, nor how many digits are in the number. Write a piece of code to extract the strings representing the letters and the digits from license plate and assign pointers to these two strings to state and count. You may make copies of the strings if you wish, but that is unnecessary. You may modify the string pointed to by license_plate in order to divide it into two strings.
```

```

7. (9 points) What is the Big-O running time for the following functions?

1. ___________________: T(n) = 5n2 + 10n * log n + 1000n
2. ___________________: T(n) = 1000000
3. ___________________: T(n) = 100n3 + 20n2 + 100 + 2n

8. (3 points) If you had three programs with the above running times, which one would you prefer if you were unsure about your input size but wanted to assume the worst (i.e., that the input sizes could be fairly large). Circle one of the following three letters to denote which program you would choose:
```     a        b        c
```

9. (8 points) Behold the following function:
```     int mystery(int a[], int size) {
int i;
int sum;
sum = 0;
for (i = 0; i < size; i++) {
sum = sum + a[i];
}
return i;
```
1. What is an appropriate measure for the input size (i.e., n)?
2. Write a function that gives a rough estimate for T(n). You do not need to show your work.
```

```
10. (9 points) Consider the following code:
```     int main(int argc, char *argv[]) {
FILE *output_file;
double sales_price;
...
}
```
Assume that the above program takes five command line arguments that represent 1) the name of the output file, 2) the month, 3) the day, 4) the year, and 5) the sales_price.

1. Write a fragment of code to open the output file for writing, test whether or not it was opened, and, if it failed to open, prints an error message that explains why it failed to open.
2. Write a statement to convert the last command line argument to a double and assign it to sales_price.
3. Write an fprintf statement that writes a string to the output_file in the format:
```mm/dd/20yy \$xxxx.xx    (e.g., 5/ 6/2008 \$12.39)
```
where:

• mm represents the month.
• dd represents the day
• yy represents the year
• xxxx.xx represents the sales price
• The month, day, and year fields should all be two characters wide, even if the fields are only single digits.
• The sales price should be left_justified. For example, if sales_price is 12.39, it should be printed as \$12.39, not \$ 12.39.
• You do not need to declare any more variables than the ones I have declared for you.
```
```
11. (9 points) For each of the following scenarios, choose from the following list the data structure that most efficiently and simply achieves the stated goal. Do not justify your answer.

• array

1. ___________________________ You want to read photo files in which the first line gives the number of rows and columns of pixels in the photo, and the remaining lines contain the pixels. You want to do certain operations on the photos, such as negate the pixels or print them out in reverse order

2. ___________________________ You want to maintain a set of slides and you want to be able to conveniently and efficiently 1) delete slides, and 2) insert slides before or after another slide.

3. ___________________________ You want to model a line of people waiting for tickets to a UT football game. You should be able to efficiently remove people from the front of the line and add people to the end of the line. You do not know the maximum number of people that may be in the line.
```

```
12. (15 points) Write a function named max_word with the following characteristics:

1. it has one parameter, which is a pointer to a character string that denotes the name of an input file.
2. it creates an inputstruct for the file
3. for each line in the file it prints the smallest word in alphabetical order on that line, prefixed with the number of that line. You may assume that each line has at least one field.
4. it releases the inputstruct when the file is fully read
5. it returns a count of the number of words in the file
6. it does no error checking
7. it does not show me any include statements
 As an example, given the input: ``` how are you doing today and how do you will do tomorrow? ``` your function should produce the output: ``` 1: are 2: and 3: do 4: will 5: do ``` and return the value 12.