CS368 Homework 3
Lec 1, Spring 2016
Due by 11:59 pm on Wednesday, March 2 (not accepted late)

Announcements

Check here periodically.

2/21/2016  Homework assigned. To ask questions about the homework and see questions posed by other students and their answers, go to: http://www.piazza.com/wisc/spring2016/cs368 and sign-in using your wisc.edu account.

Problems

In this homework you will gain experience writing functions in MATLAB as well as doing user (console) and file input and output. The main program (the outline of which is provided for you) interacts with the user to test the functions you have written.

Note: in order to get full credit for the functions you write in this assignment, you may NOT use any control-flow statements (like if, for, or while) in your functions. You must make use of MATLAB's ability to perform mathematical operations on entire vectors (or matrices) of information.

The calculateGPA function

Define a function named calculateGPA in a file named calculateGPA.m. The calculateGPA function takes one input argument: a file name (i.e., a string). The string passed in to the function must be the name of a text-only file with exactly three columns of numbers (separated by spaces). Each line in the file corresponds to one course: the first column is the semester in which the course was taken (e.g., 1, 2, 3), the second column is the grade received in the course (0.0 to 4.0), and the third column is the number of credits for the course. The function computes and returns the overall grade point average (GPA) for the student.

You may assume that the filename passed in to the calculateGPA function is the name of a file that exists and has the correct format and that all values in the file are in the correct range. Note that because the input file just contains a matrix of numbers, you can use a simple load command to read it in.

The calculatePercent function

Define a function named calculatePercent in a file named calculatePercent.m. The calculatePercent function computes a weighted percentage based on three vectors of scores passed as arguments to the function: exams, homeworks, and quizzes (in that order).

  • exams is a (row) vector of exam scores with a max of 60 pts per exam
  • homeworks is a (row) vector of homework scores with a max of 30 pts per hw
  • quizzes is a (row) vector of quiz scores with a max of 10 pts per quiz

Exams are worth 60% of the overall score, homeworks are worth 25%, and quizzes are worth 15%. The value returned by calculatePercent should be between 0 and 100 (e.g., 87.65).

Your calculatePercent function must work for matrices of input as well as row vectors of input. In other words, it must be able to take a N × E matrix of exam scores, a N × H matrix of homework scores, and a N × Q matrix of quiz scores, calculate the percentages for each of the N rows, and return a column vector of the N percentages.

The main program (homework3.m)

The main program will be the homework3.m script. The file homework3.m contains an outline; use it as a starting point. The program presents the user with a menu of choices of functions to test along with an option to quit. The user makes a selection, the program performs the selected test, and the user is presented with the menu of choices again. The process repeats until the user selects 'quit'. You will need to add code to implement the choices that test the functions so that they do the following:

calculateGPA option

When the user selects the calculateGPA option, the program should prompt the user for the name of a file, pass the name of the file to the calculateGPA function, and display the results in the following format (user input is shown in bold green):

Enter filename: student1.txt
GPA = 3.875

calculatePercent - user input option

When the user selects the calculatePercent - user input option, the program should prompt the user for the exam scores, the homework scores, and the quiz scores (separately), call the calculatePercent function with those scores, and display the results in the following format (user input is in bold green):

Enter exams scores: [60, 48, 53]
Enter homework scores: [28, 30, 25, 20]
Enter quiz scores: [6, 9, 10, 8, 4.5]
Score = 86.375%

calculatePercent - file I/O option

When the user selects the calculatePercent - file I/O option, the program should prompt the user for the name of a file for input and the name of a file for output in the following format (user input is in bold green):

Enter input filename: scores4.txt
Enter output filename: output4.txt

The program should then load the input file and call the calculatePercent function with the appropriate values.

You should assume that the input files have the following format: each line of the file contains 24 numbers: a 4-digit ID, 3 exam scores, 6 homework scores, and 14 quiz scores, in that order, separated by spaces (see scores4.txt for an example).

After calling calculatePercent, the program should save results to the specified text-only output file with each line of the output file consisting of the ID, one or more spaces, and the score (see output4.txt for an example). Note: output4.txt was created using the save command (which is why the results are in scientific notation).

Tips and hints:

  • First, download the main program outline file: homework3.m

  • Write your calculateGPA function first and add the code to the main program to implement the calculateGPA option. Here are some files you can use to test your calculateGPA function:

  • Next, write your calculatePercent function so that it works for (just) row vectors of input (i.e., don't worry about matrix input yet). Add code to your main program to implement the calculatePercent - user input option. Here's some sample user input to try (don't forget to enter vectors using MATLAB's vector notation):

    • exams: 0, 0, 0, homeworks: 0, 0, quizzes: 0
    • exams: 60, 60, homeworks: 30, 30, 30, quizzes: 10, 10, 10, 10
    • exams: 60, 48, 53, homeworks: 28, 30, 25, 20, quizzes: 6, 9, 10, 8, 4.5
  • Next, modify your calculatePercent function so that it works with matrices of input.

  • Finally, add code to your main program to implement the calculatePercent - file I/O option. Here are some files you can use to test your calculatePercent - file I/O option:

  • You do not need to do any error-checking of user input or any of the values in input files.

  • All file input and output in this assignment can be done using the basic load and save commands, respectively (as outlined in the Basic I/O handout).

Handing in

Upload your m-files to your Dropbox in Learn@UW. See these instructions for uploading files to a Learn@UW Dropbox. The files you should upload are:

  • homework3.m
  • calculateGPA.m
  • calculatePercent.m

In order for your work to be considered to have been turned in on-time, you must upload your files to your Learn@UW Dropbox by 11:59 pm Wednesday, March 2.

Last Updated: 2/21/2016     © 2016 Beck Hasti, hasti@cs.wisc.edu