Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problems In this assignment you will gain experience writing functions in MATLAB as well as doing user (console) and file input and output. The main

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Problems In this assignment 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 calculate GPA.m. The calculateGPA function takes one input parameter: 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 Nx E matrix of exam scores, a NxH matrix of homework scores, and a NxQ 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 (program2.m) The main program will be the program.m script. The file program2.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 shown in bold italics): Enter filename: studenti.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 shown in bold italics): 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 1/0 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 shown in bold italics): 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 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 shown in bold italics): 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). (Note also that when you view output4.txt using Canvas, you will see some extra spaces after the "1." at the beginning of each line. These spaces are not in the actual output4.txt file, which you can check by downloading the file and opening from your computer; this is a known bug in Canvas' file previewer.) Tips and hints: First, download the main program outline file: program2.ma 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: o student1.txt a o student2.txt a o student3.txt o student4.txt First, download the main program outline file: program2.m D 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: o student 1.txt o student2.txt > o student3.txt a student4.txt Note: Make sure to download the test files to your computer (and do not, for example, look at the file in Canvas and copy-paste the contents). There is a (known) bug in the way Canvas' file preview works for text files: text files that have consecutive rows that start with small positive integers (e.g., 1, 2, 3, ...) get mangled so that a dot shows up after the number and the numbers themselves may be changed (you can see the most egregious example of this in student4.txt by comparing your downloaded file to the preview you see in Canvas). This mangling happens whether you preview the file by clicking on the "Preview the document" icon next to the file name (so it shows up within the current page) or clicking on the file name itself (a link which opens in a separate Canvas page). 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): o exams: O, O, O, homeworks: O, O, quizzes: 0 o exams: 60, 60, homeworks: 30, 30, 30, quizzes: 10, 10, 10, 10 o 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 1/0 option. Here are some files you can use to test your calculatePercent file I/O option: O scores1.txt o scores2.txt o scores3.txt o scores4.txt a scores5.txt Vou do not need to do anorrer checking of urinnut or any of the value in innut filer Problems In this assignment 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 calculate GPA.m. The calculateGPA function takes one input parameter: 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 Nx E matrix of exam scores, a NxH matrix of homework scores, and a NxQ 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 (program2.m) The main program will be the program.m script. The file program2.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 shown in bold italics): Enter filename: studenti.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 shown in bold italics): 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 1/0 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 shown in bold italics): 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 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 shown in bold italics): 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). (Note also that when you view output4.txt using Canvas, you will see some extra spaces after the "1." at the beginning of each line. These spaces are not in the actual output4.txt file, which you can check by downloading the file and opening from your computer; this is a known bug in Canvas' file previewer.) Tips and hints: First, download the main program outline file: program2.ma 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: o student1.txt a o student2.txt a o student3.txt o student4.txt First, download the main program outline file: program2.m D 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: o student 1.txt o student2.txt > o student3.txt a student4.txt Note: Make sure to download the test files to your computer (and do not, for example, look at the file in Canvas and copy-paste the contents). There is a (known) bug in the way Canvas' file preview works for text files: text files that have consecutive rows that start with small positive integers (e.g., 1, 2, 3, ...) get mangled so that a dot shows up after the number and the numbers themselves may be changed (you can see the most egregious example of this in student4.txt by comparing your downloaded file to the preview you see in Canvas). This mangling happens whether you preview the file by clicking on the "Preview the document" icon next to the file name (so it shows up within the current page) or clicking on the file name itself (a link which opens in a separate Canvas page). 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): o exams: O, O, O, homeworks: O, O, quizzes: 0 o exams: 60, 60, homeworks: 30, 30, 30, quizzes: 10, 10, 10, 10 o 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 1/0 option. Here are some files you can use to test your calculatePercent file I/O option: O scores1.txt o scores2.txt o scores3.txt o scores4.txt a scores5.txt Vou do not need to do anorrer checking of urinnut or any of the value in innut filer

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions