CS 111 Assignment 7

Here are the Answers to the practice problems.

  1. Files to print out and bring to lecture and recitation
  2. Preparations before you begin your homework
  3. Practice problems
  4. Assigned programming problems
  5. Preparing to hand in your homework

For information about the due dates, including late dates and extra-credit early due dates, please see Homework policies and due dates.


  1. Files to print out and bring to lecture and recitation

    Below are tutorials and example programs and data files. Please make printouts of these and bring them with you to both lecture and recitation. However, please do NOT print them out in an on-campus lab. (On-campus printers are to be used only for your homework, i.e. for files YOU wrote.) If you do not have a computer at home, with a printer, ask a friend or classmate to print out copies of the following files for you.


  2. Preparations before you begin your homework

    On forbin, create a directory named hw07 inside your homework directory. Change your present working directory to hw07 and then copy into it the example files for this homework assignment, by typing, at the "forbin>" prompt:

       cp ~nixon/cs111/hw07/* .
    

    Please do NOT copy these files into your home directory, to avoid cluttering your home directory. If you inadvertantly copied them into your home directory, move them out using the mv command. Be very careful about deleting anything in your home directory, to avoid inadvertantly deleting your hidden files (.login, .cshrc, .profile, etc.).


  3. Practice problems

    1. Write programs average6.cpp and average7.cpp which should behave exactly like averaging programs in Assignment 3. Both programs should read integers from a textfile whose filename has been input as a command-line argument, and should output, to the terminal window, a floating-point average of the integers in the text file. If the file is empty, the programs should output "0" as the "average."

      In average6.cpp, use a do/while loop. To make it work, you'll need an extra if statement with a condition identical to that of the do/while loop. Thus, this program will NOT be a good use of a do/while loop. You are asked to write this program only to deepen your understanding of loops, and for no other reason.

      In average7.cpp, use a while loop with a break statement, similar to the loop in copyWhileBreak.cpp.

    2. Given the file prelim.cpp, which contains the following global declarations pertaining to an array of structs:

      const int NUMBER_OF_QUIZZES = 5;
      typedef float QuizArray[NUMBER_OF_QUIZZES];
      const int MAX_NAME_LENGTH = 40;  // last name first;
                                       // includes spaces between
                                       // parts of name.
      typedef char NameText[MAX_NAME_LENGTH + 1];
      
      struct Student  {
         int iD;
         NameText name;
         QuizArray quizzes;
      };
      
      const int MAX_NUMBER_OF_STUDENTS = 40;
      typedef Student Roster[MAX_NUMBER_OF_STUDENTS];
      

      And given the that the file also contains functions for file input, which are called to read from quizRoster.txt, you are now asked to add, to the main function, calls to the following functions:

      void printStudentName(const Student& student);
      void printText(char text[]);
      void printLetter(char x);
      void printScore(float x);
      

      where:

      • Function printStudentName is to be called for the Student at location 2 in array students (which has already been declared, in the main function, to be of type Roster).

      • Function printText is to be called for the name of the Student at location 3 in array students.

      • Function printLetter is to be called for first (i.e. location 0) letter of the name of the Student at location 1 in array students.

      • Function printScore is to be called for first (i.e. location 0) quiz score of the Student at location 0 in array students.

    3. Write a program which prompts the user to enter a letter grade and outputs the grade points corresponding to that letter grade (4 for A, 3 for B, etc.). Your program should be case insensitive, i.e. it should respond to lowercase letters the same as it responds to uppercase letters. If a character other than A, B, C, D, or F is entered, it should say "Not a valid grade." Use a switch stetement.


  4. Assigned programming project

    Write a program atm3.cpp exactly identical in behavior to the Assignment 6 program, escept that it should use just one array of structs rather than three seperate parallel arrays. Declare globally: (1) your struct type, (2) the length of the array of structs, as a constant, and (3) a typedef for the array of structs. Those functions which previously took the three arrays as parameters should now replace the three arrays with one parameter of your typedef'd array type. (But do NOT declare the array itself globally. Declare the array itself in main.)

    Hint: Take your Assignment 6 program and re-write the entire main function from scratch. Begin by deleting the entire body of the main function. Then, above the main function, add your global constants, struct definition, and array typedef. Make sure this compiles, so far. Then begin modifying, one at a time, those functions that used the three arrays. After modifying each function, make sure your program compiles. (Do not modify those functions that did NOT explicitly use any of your arrays.) Then write a main function which does nothing but declare an array (of your typedef'd type) and test the modified functions, Make sure that this both compiles and runs correctly. Then re-write the remainder of the main function.


  5. Preparing to hand in your homework

    Run the script file hw07.sh to make sure your programs work correctly and that they have the correct filenames. The script file hw07.sh can be copied into your present working directory as follows:

       cp ~nixon/cs111/hw07/hw07.sh .
    

    Then, create the tar file for this assignment. The tar file should contain the following files, and ONLY the following files:

    • atm3.cpp

    To create a tar file, type:

       tar -cvf lastname_firstname_7.tar *.cpp
    

    The tar file must have a filename in exactly the following format:

       lastname_accountname_7.tar
    

    replacing lastname with your actual last name (spelled as the Registrar's Office spells it, as on your tuition bill or bursar's recepet) in lower-case letters only (not capital letters), and replacing accountname with your actual forbin account name. The "7" indicates Assignment 7.

    The tar file must be submitted to your instructor at the appropriate E-mail address, as listed on the instructor E-mail address page.

    To recitatiion/lab on the due date, bring a stapled-together printout of atm3.cpp. the above-listed source code files. Your printouts must be stapled together in exactly the order listed above.


Back to: