- You are given the following declarations:
char job[30] = "Hello World";
char *dest;
Jval j;
IS input_file = new_inputstruct(0);
Use a checkmark to indicate which of the following statements will be
flagged as a warning or error
by the compiler and which statements may cause the program to arrive
at a wrong result or to core dump. Consider each set of statements separately
and do not assume that any variable other than input file or job
has been initialized (by the way job is really a 30 character array even
though it is initialized to a smaller character string). If you
think that a statement will be flagged by the compiler explain why.
If you think a set of statements will compile correctly but will
cause a problem during execution try to rewrite them. It may be possible to
either correct the problem or to write a conditional that ensures the
statement(s) is executed only if a certain pre-condition is met.
- strcpy(job, "brad"); dest = job;
- job = (char *)malloc(sizeof(char) * 50);
- if (get_line(is) >= 0 && is->NF >= 2) { dest = is->fields[1]; }
- if (get_line(is) >= 0 && is->NF >= 2) { strcpy(job, is->fields[1]); }
- strcpy(dest, job);
- strcpy(j.s, job);
- j.s = strdup(job); return j.s;
- dest = strdup("brad"); job = dest;
- dest = (char *)malloc(sizeof(char) * 10); strcpy(dest, "bradley"); j.s = dest;
- For the following set of statements assume that the string being read
is less than 30 characters:
scanf("%s", job); dest = job; return dest;
- How many bytes will be allocated to the following union?
union Item {
struct {
int grades[5];
char name[20];
char sex;
} student;
struct {
Dllist courses;
char name[20];
char office[10];
int job_type;
char *address;
} professor;
int ssn;
};
- How many bytes will be allocated to the following struct?
struct account {
int type;
char name[30];
double balance;
union {
struct {
int check_num;
Dllist checks;
} checking;
struct {
double interest_rate;
double quarterly_interest;
double annual_interest;
} savings;
} acct_info;
};
Assume that a double requires 8 bytes of storage.
-
- Explain the difference between a source file, an object file, and
an executable file.
- What file extension is each of the above files likely to have
(e.g., .doc, .c, .cpp, .o, .a, etc)?
- What flag will cause gcc to generate an object file?
- What flag will cause gcc to generate an executable file?
- What flag will cause gcc to generate a file suitable for debugging
with gdb?
- Rewrite Dr. Plank's new_stack, stack_push, and stack_pop functions so
that they use Dllists. The functions should take the same parameters
and return the same types but should use his Dllist library rather
than his linked data structure.
You will have to also re-write Dr. Plank's stack struct to
use a dllist. Do not worry about compiling or executing your code.
- For each of the following problems indicate whether you would use
a singly-linked list, doubly-linked list, or array.
- You want to read an unknown number of names from a file and use
insertion sort to sort them into ascending order.
- You want to read an integer, n, from the command line
and print out the last n lines of a file.
- You want to read an unknown number of lines from a file and print
the lines in reverse order.
- You want to simulate a line at a grocery store where customers
enter the line at the back and are served from the front of the line.
- Write a short program that simulates pushing and popping functions from
a stack. Your input consists of lines
from standard input that begin with either
the keyword "call" or "return". If a line starts with "call" it
also has a second field which is the name of a function. If a line
starts with return it has an optional second field which is the name
of a function. You are to write a program that uses the fields library
to read these lines. Your program should use Dr. Plank's stack
library and it should perform the following actions:
- If a line starts with the keyword "call" then
you should push the function name onto a stack.
- If a line starts with the keyword "return" but does not have
a second field you should pop the most recent function name
from the stack.
- If a line starts with the keyword "return" and has a second
field then you should pop the stack until the top of the
stack has the indicated function name.
The following example shows some sample input and what the stack
would look like after each line is processed. Of course the function
names would be pointed to by character strings and the actual values
on the stack would be Jvals that pointed to the character strings.
call a // stack = a
call b // stack = b a
call a // stack = a b a (a was pushed onto the front)
return // stack = b a (b was popped)
call c // stack = c b a
call d // stack = d c b a
call a // stack = a d c b a
return c // stack = c b a (a and d got popped)
Do not worry about printing anything, checking for error conditions,
or compiling/executing your code.