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, as 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. 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 occurence 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.

  3. 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 minimum sixth word at the end of the program.
    It is not necessary to compile and execute this program although you are welcome to do so.

  4. 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 and explain why it would cause a compiler error. Place an "X" next to any statement that could cause a runtime execution error and explain what type of error it might cause. 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;