Please I need help with a program for my exam
|
11-15-2024, 12:52 PM
Post: #1
|
|||
|
|||
Please I need help with a program for my exam
I want to create a program for HP Prime that calculates the savings matrix giving a matrix A and a matrix B.
How does it do it? well I give the calculator a square matrix n x n called A and a horizontal matrix n called B. What it does is for every cell in the savings matrix it puts the corresponding value: in the first cell 11 it would put: value 1 of B + value 1 of B - value 11 of A. in the cell 12 it would put: value 1 of B + value 2 of B - value 12 of A. In the cell 54 it would put: value 5 of B + value 4 of B - value 54 of A. The problem is that the program doesn't work, it says that there is an input error and I cannot even write the matrixes A and B. It just doesn't run. What I wrote is: EXPORT SavingsMatrix() BEGIN // Input the square matrix A LOCAL A, B, S, n, i, j; // Prompt the user for inputs A := INPUT({{}},"Enter matrix A: "); B := INPUT({{}}, "Enter vector B: "); // Validate dimensions n := SIZE(B); // Size of B must match rows/cols of A IF SIZE(A)≠n OR SIZE(A(1))≠n THEN PRINT("Error: A must be square, and B must match A size."); RETURN; END; // Create the savings matrix S S := MAKEMAT(0, n, n); // Initialize nxn matrix S // Calculate each element of the savings matrix FOR i FROM 1 TO n DO FOR j FROM 1 TO n DO S(i,j) := B(i) + B(j) - A(i,j); END; END; // Output the result PRINT("Savings Matrix S:"); RETURN S; END; |
|||
11-16-2024, 04:33 PM
(This post was last modified: 11-16-2024 04:34 PM by komame.)
Post: #2
|
|||
|
|||
RE: Please I need help with a program for my exam
Here are the solutions that came to my mind.
Each of these approaches has its advantages and disadvantages; the choice is yours. By the way, for this piece of code: Code: S := MAKEMAT(0, n, n); // Initialize nxn matrix S Code: S := MAKEMAT(B(I)+B(J)-A(I,J), n, n); It takes up much less space and works much faster than iterating and calculating each value separately. Piotr Kowalewski |
|||
11-18-2024, 01:51 PM
Post: #3
|
|||
|
|||
RE: Please I need help with a program for my exam
(11-16-2024 04:33 PM)komame Wrote: Here are the solutions that came to my mind. Thank you sir. I think I was trying to solve it with something similar as the 3rd approach. I understand. Thank you so much for your effort!!! I appreciate it |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)