CS 1103 Spring 2025
Introductory Programming for Engineers and Scientists
EXAM 2 Practice
Exam 1 Practice requires you to download the grader program from BrightSpace. It is called e2practice.p and you can find it under "Exam" and then "Exam 2 Practice". Create a new blank folder where you intend to work on this test and copy the p file there. Make sure that it was not renamed in the process to something like e2practice(1).p or similar. When you start MATLAB, set your current folder to this very same folder. Solve each problem in MATLAB. Make sure to name your solutions (that is, your m-files) as instructed below and that they are located in the same folder as the grader. Run the grader in MATLAB by typing the command: e2practice. The program will display a menu where you have the option to test individual problems one by one. To get the score for your test, select to grade "All Problems." The grader will ask for your vunetid. Makes sure you type it in correctly, all lower case and no extra spaces. The grader will display your score.
Submission: N/A (for the real test you will need to submit all your m files)
Grading: Number of problems solved: points: 1: 40; 2: 70; 3: 85; 4: 95; 5: 100;
Password: LoopyTest Problems:
1. Write a function called pe21 that takes two input arguments indicating the current time: hr for hours and min for minutes. The function checks whether the two inputs can in fact represent time according to the international format. That is, it needs to check that they are both positive integer scalars and hr is between 0 and 23 inclusive and min is between 0 and 59 inclusive. The function returns a logical value: true if the input arguments satisfy these rules and false otherwise.
2. Write a function called pe22 that takes a positive integer scalar input argument called k. Make sure to check that k satisfies these assumptions and if it does not, return -1. The function computes and returns a row vector consisting of the first k elements of the sequence s defined as
s(1) = 1
s(n) = n*s(n-1) - 1 for all n >= 2
Note that this is not MATLAB code: it is the mathematical definition of the sequence where s(n) means the nth element of the sequence.
3. Write a function called pe23 that takes A, a matrix and returns another matrix with two columns called ind. The rows of the matrix contain the row and column indexes of all the negative elements of A. The row indexes go into the first column, while the column indexes go to the second column. The function provides these indexes according to row major order of A. For example, the call
>> negs = pe23([-1 -2 0; -3 2 2]);
will make negs equal to [1 1; 1 2; 2 1]. If there are no negative elements in A, the function returns the empty array.
4. Write a function that is defined like this: function w = pe24 (v,a,b,c). The first input argument v is a vector, while a, b, and c are all scalars. You do not need to check them. The function replaces every element of v that is equal to a with b and c. For example, the command
>> x = pe24 ([1 2 3],2,4,5); makes x equal to [1 4 5 3].
5. Write a function called pe25 that takes four scalar positive integer inputs, month1, day1, month2, day2. These represent two days in 2025. It is guaranteed that the first date is no later than the second one. The function returns a positive integer scalar that is equal to the difference between the two dates in days. For example, this call to the function pe25 (1,30,2,1); would return 2. You do NOT have to check that the input values are of the correct types and they represent valid dates. You are not allowed to use the built-in functions datenum or datetime. Here are the number of days in each month from January to December in 2025: 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31.