- Consider the following code:
FILE *score_file;
int *int_array = (int *)malloc(sizeof(int) * 40);
- Write a statement that opens a file named "scores" and
assigns the returned pointer to score_file.
- Write a loop that uses fscanf to read 40 integers from
score_file directly into the elements of
int_array. In other words, you
cannot pass an integer variable to fscanf and then assign the
integer variable to the appropriate element in the array.
That requires an intermediate step and is less efficient than a
direct read.
- Modify the above loop so that it works properly if there are
fewer than 40 integers in score_file (i.e, you will need
to add an appropriate check for EOF).
- Suppose you have the following declaration for a doubly-linked list
with a sentinel node:
struct _Node {
int score;
struct _Node *next;
struct _Node *prev;
}
Write a function named compute_avg that takes a pointer to a
sentinel node and returns the integer average of the scores in the
list.
- Suppose you have the above declaration for a doubly-linked list with
a sentinel node. Write a function named append that adds a
new node to the end of the list. The function's prototype should be:
void append(Node *sentinel_node, Node *new_node)
- Write a program (i.e., write a main function) that reads lines of
input from stdin, and writes the minimum word on each line and the
line's line number to a file specified on the command line by the user.
Use the fields library to read the lines from stdin. As an example, given
the input:
the quick brown
fox jumped over
the white picket
fence
your program would produce the output:
1: brown
2: fox
3: picket
4: fence
The output would be written to the appropriate file. Your program should
ensure that the number of command line arguments is correct. You may
assume that stdin opens correctly and therefore you do not have to check
the return value when you create the inputstruct for stdin.
- Roughly how many bytes will be allocated to the following union? You should make
the following assumptions:
- ints and pointers are 4 bytes
- doubles are 8 bytes
- a single char takes 1 byte
- fields are packed together as tightly as possible and word
alignment is ignored. In real applications the
compiler will group together fields that must be word-aligned
(e.g., ints, floats, pointers), then double word-aligned (longs and
floats), and then fields that can be packed tightly together (chars
and char arrays). That is why
assuming that fields are packed tightly together yields only a
rough estimate of the size of the union.
union Item {
struct {
int grades[5];
char name[20];
char sex;
} student;
struct {
Node *courses;
char name[20];
char office[10];
int job_type;
char *address;
} professor;
char ssn[10];
};
- Suppose I have the declaration:
union Item a;
Write expressions to:
- Assign 85 to grades[3] of student
- Copy the string "bvz" to the name field of student
- Copy the string "bvz" to the address field of professor
- Assign 3 to the job_type field of professor
- Assign the string "123456789" to the ssn field.
- How many bytes will be allocated to the following struct? Use the
same assumptions you used in the earlier problem on estimating the
size of unions.
struct account {
int type;
char name[30];
double balance;
union {
struct {
int check_num;
char *checks;
} checking;
struct {
double interest_rate;
double quarterly_interest;
double annual_interest;
} savings;
} acct_info;
};
- Suppose I have the declaration:
struct account y;
Write expressions to:
- copy the string "bvz" to the name field.
- assign 30 to the check_num field of checking.
- assign .05 to the annual_interest field.