-
#define MAX_NAME_LENGTH 15
typedef struct {
char common_name[MAX_NAME_LENGTH+1];
char *latin_name;
int year_discovered;
int evergreen;
} Tree;
-
- 0x1e8368d0
- 0xfe93ad1a
-
1. strcpy(first_name, min_name);
2. if (strcmp(min_name, new_name) < 0)
printf("%-20s\n", min_name);
else
printf("%-20s\n", new_name);
3. printf("%8.3f\n", speed);
4. min_name = strdup(new_name);
5. printf("%3d\n", strlen(first_name) + strlen(last_name));
6. parts = (char **)malloc(sizeof(char *)*50);
7. parts[4] = strdup(new_name);
8. strcpy(name, last_name);
strcat(name, ", ");
strcat(name, first_name);
-
1. input_file = fopen(argv[1]);
if (input_file == NULL)
perror(argv[1]);
2. fscanf(input_file, "%d %lf", &price, &quantity);
3. num_lines = atoi(argv[2]);
4. fgets(buffer, 100, input_file);
5. if (feof(input_file))
exit(0);
-
---------------------------
0xffbea600 x:________________ | 40 |
0xffbea604 month: ___________ | febr |
0xffbea608 __________________ | .\0 |
0xffbea60c string_ptr: ______ | 0xffbea604 |
0xffbea10 int_ptr: _________ | 0xffbea600 |
---------------------------
-
- array: since the number of names is known, I can allocate a block
of memory to hold the array and then use binary search to efficiently
answer user queries.
- array: even though you are reading an unknown number of lines, you
have been asked to print a known number of lines, which is 10. You
can therefore allocate an array of size 10 to store the lines.
Once the array fills up with the first 10 lines, you would treat
the array like a circular list and always replace the oldest line.
- list: even though you know the number of trees and could allocate
an array of fixed size, you need to insert lines into the middle
of the array, which is inefficient. Hence you should use a list
which allows for efficient insertion into the middle of the list.
If you told me that you would use an array with binary search and
that moving on average half the entries is no worse than examining
on average half the entries, then you received significant partial
credit. It is faster to compare elements than move elements so it
is still faster to use the list than an array.
-
void last_field(IS input) {
// the null string "" is less than any other string so initializing
// maxword in this way guarantees that the first time we see a non-blank
// line, maxword will get set to a real word
char *maxword = strdup("");
int i;
while (get_line(input) >= 0) {
// print a line number but no word if the line is blank
if (input->NF == 0) {
printf("%d: \n", input->line);
}
else {
char *lastword = input->fields[input->NF-1];
printf("%d: %s\n", input->line, lastword);
if (strcmp(maxword, lastword) < 0) {
free(maxword);
maxword = strdup(lastword);
}
}
}
printf("%s\n", maxword);
}