- (8 points) Write an anonymous struct for a tree and use typedef to name it "Tree".
The struct should contain the following fields:
- common_name: a string of at most 15 characters. The string should
be stored using a character array.
- latin_name: a string of unknown length
- year_discovered: an integer denoting the year in which the tree was
first discovered
- evergreen: a boolean variable indicating whether or not the tree
is an evergreen
- (6 points)
- 0x1e8368cc + 0x4 = _______________ (I want the hexadecimal result)
- 0xfe93ab43 + 0x1d7 = ________________ (I want the hexadecimal result)
- (24 points) Consider the following declarations:
char name[35];
char first_name[10];
char last_name[20];
char *min_name;
char *new_name;
char **parts;
float speed;
Write statements to accomplish the following tasks:
- copy the string pointed to by min_name to first_name.
You may assume that min_name has fewer than 10 characters.
- print the lesser (i.e., earlier in alphabetical order)
of the two strings pointed to by min_name and new_name
in a left-justified field of 20 characters.
- print speed in a right-justified field of 8 characters
with 3 digits allocated to the part after the decimal point.
- do a safe copy of the string in new_name to min_name
- print the combined length of first_name and last_name
in a right-justified field of 3 digits.
- malloc an array of 50 string pointers and assign the address of
the newly created array to parts.
- do a safe copy of the string in new_name to the fifth
element of parts.
- create a string of the form "last_name, first_name" and store it
in name (for full credit you must build the string in
name rather than creating it separately and then
copying it to name).
- (15 points) Consider the following code:
main(int argc, char *argv) {
FILE *input_file;
char buffer[101];
int num_lines;
int price
double quantity;
...
}
Assume that the above program takes two command line arguments. The
first argument is the name of the input file and the second argument is
the number of lines in the file.
- Write a fragment of code to open the input file, test whether or not
it was opened, and, if it failed to open, prints an error message
that explains why it failed to open.
- Write an fscanf statement that reads the first two fields
from input_file into price and quantity
respectively.
- Write a statement that converts the second command line argument to
an integer and assigns it to num_lines. You may assume that
the argument is correct and choose your conversion function
accordingly.
- Write an fgets statement that reads a line of
input_file into buffer.
- Write a conditional statement that exits with a status code of 0
if the end of file has been reached for input_file.
- (10 points) Consider the following code:
int x;
char month[6];
char *string_ptr;
int *int_ptr;
strcpy(month, "febr.");
x = 20;
string_ptr = month;
int_ptr = &x;
*int_ptr = 40;
and the following memory layout:
---------------------------
0xffbea600 x:________________ | |
0xffbea604 month: ___________ | |
0xffbea608 __________________ | |
0xffbea60c string_ptr: ______ | |
0xffbea10 int_ptr: _________ | |
---------------------------
Fill in the memory contents for the above diagram (do it on your
answer sheet). Note that
each memory location is 4 bytes.
- (12 points) For each of the following scenarios specify whether you would
use a linked list or an array and explain why.
- You have a sorted set of 1000 names and you want to efficiently
answer user queries about whether or not certain names are in this
dataset.
- You want to read an unknown number of lines into memory and print
out the last ten (think carefully about this problem--the answer
may not seem as obvious as you think).
- You want to read a file that contains 500 names of trees and
associated information about these trees. As you read each tree's
line into memory, you want to create a struct for that tree and
insert that tree in alphabetical order into your data set.
- (25 points)
Write a function named last_field
that takes an IS inputstruct as a parameter and that
1) prints the last field on each line, along with that line's line
number, and 2) prints the maximum of these last words, where maximum
means the last of these words in alphabetical order. You should assume
that the inputstruct has already been opened and is ready for reading.
As an example, given the input:
How are you
doing today and how
do YOU think
you will
do tomorrow
your function should produce the output:
1: you
2: how
3: think
4: will
5: tomorrow
you