CS140: Homework 6

  1. Consider the following code:
    	FILE *score_file;
    	int *int_array = (int *)malloc(sizeof(int) * 40);
    1. Write a statement that opens a file named "scores" and assigns the returned pointer to score_file.
    2. 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.
    3. 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).

  2. 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.

  3. 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)

  4. 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
    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.

  5. Roughly how many bytes will be allocated to the following union? You should make the following assumptions:

    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];

  6. Suppose I have the declaration:
    union Item a;
    Write expressions to:

    1. Assign 85 to grades[3] of student
    2. Copy the string "bvz" to the name field of student
    3. Copy the string "bvz" to the address field of professor
    4. Assign 3 to the job_type field of professor
    5. Assign the string "123456789" to the ssn field.

  7. 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;
  8. Suppose I have the declaration:
    struct account y;
    Write expressions to:

    1. copy the string "bvz" to the name field.
    2. assign 30 to the check_num field of checking.
    3. assign .05 to the annual_interest field.