Homework 3

  1. Suppose you are writing a program that takes two command line arguments, an integer representing the number of lines of input to read and a floating point number representing the proportion of the input to write out. For example, a sample invocation might be:
    	print_lines 60 .40
    Write a code fragment that you would have to place in main to convert these command line arguments to an integer and a floating point number respectively.

  2. Write a code fragment that reads lines from a file named "bvz" and stores them in an array named "bvz_lines". The first line of "bvz" is a single number representing the number of lines of input. The rest of the file is the input. Each line may consist of an arbitrary number of words, but the length of each line should be less than 128 characters. Your code fragment should

    1. Open "bvz" for input
    2. malloc an array of character pointers large enough to store all the input lines
    3. Use feof to test for end of file, and
    4. At each point where your code fragment reads input, write a line which reads input using fgets and then write a line which reads input using getline. To make it easier for the TA to read your code, put an "or" between your lines. For example:

  3. You are given the following declarations:
    	char first_name[10];
    	char last_name[20];
    	char save_name[10];
    	char *temp_name;
    	int length;
    1. Write a statement that assigns the sum of the lengths of first name and last name to length.
    2. Write a statement that copies first_name to save_name.
    3. Write a statement that copies first_name to temp_name and that assures that memory will be allocated for temp_name.
    4. Write a statement that finds the first occurrence of the letter 'a' in first_name and assigns the result to temp_name.
    5. Write a statement(s) that concatenates first_name and last_name and places the result in temp_name. You should make sure that temp_name points to enough memory to hold the result.

  4. Write a program using the fields library that reads a file from standard input and that:
    1. prints the 6th word from each line of input or "None" if the line is shorter than 6 words,
    2. prints the total number of words in the file, and
    3. prints the total number of characters in the words in the file
    It is not necessary to compile and execute this program although you are welcome to do so.

  5. Suppose you have the following declarations:
    	struct person {
    	    int start_year;
    	    char name[15];
    	    char *occupation;
            struct person *new_employee;
    	struct person employee1;
    	char first_name[10];
    	char last_name[10];
    	struct person employee_array[5];
    	void person_lookup(struct person *e);
    Consider each of the following statements or group of statements in isolation from the others. Place a checkmark next to any statement that would cause a compiler error, explain why it would cause a compiler error, and fix it. Place an "X" next to any statement that could cause a runtime execution error and explain what type of error it might cause. You do not have to fix this type of error. Do not assume that any malloc statements have been executed before these statements are executed.
    1. employee1->start_year = 1960;
    2. person_lookup(&employee_array[3]);
    3. scanf("%d %s %s", employee1.age, employee1.name, employee1.occupation);
    4. person_lookup(employee1);
    5. strcpy(employee1.name, first_name); strcat(employee1.name, last_name);
    6. strcpy(employee1.occupation, first_name);
    7. new_employee = malloc(struct person);
    8. employee_array[2].start_year = 2003;

  6. Behold the following declarations:
        typedef struct {
          char *part_name;
          int part_id;
          int quantity_on_hand;
          double cost;
        } Part;
        IS input = new_inputstruct(NULL);
        Part *part_array;

    1. Declare a Part variable named "my_part" and initialize it with the values "water filter", "368", "45", "49.99".

    2. Repeat the above exercise but this time initialize only the part_id field to "368" and the cost field to "49.99".

    3. Use the fields library to read a line from input, convert the fields to the appropriate types, and assign the converted fields to the appropriate field in my_part. Put all the assignments and conversions on a single line and inside {}'s using the struct assignment idiom discussed in class. It is ok to use atoi and atof in your answer so that you can keep your answer compact. Normally you would use sscanf to do the conversions.

    4. malloc an array of 10 Parts and assign it to part_array.

    5. design a new anonymous struct and use a typedef to name it SupplierAddress. It should have fields named street_address, city, state, and zip_code. The street address and the city may be arbitrary length strings, the state will always be a two character abbreviation, and the zip code will be a 5 digit integer.