Post Reply 
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;
Find all posts by this user
Quote this message in a reply
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.

  1. You can write your function in such a way that it takes a matrix and a vector from the command line, and then you would call it like this:
    SavingsMatrix([[1,2,3],[4,5,6],[7,8,9]],[1,2,3])

    The code that implements this does not need to include data input, as the data is provided as parameters.
    Code:
    EXPORT SavingsMatrix(A,B)
    BEGIN
    LOCAL S, n, i, j;

    // Validate dimensions
    n := length(B); // Size of B must match rows/cols of A
    IF rowDim(A)≠n OR colDim(A)≠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;

    RETURN S;
    END;
    This is probably the most sensible approach, as it allows the use of such functions in various programs and the direct return of results to variables, which can then be used in further calculations within the program.
     
  2. The second approach is to retrieve the matrix and vector using the INPUT command, but to do this, you must specify the type of data you want to retrieve (by default, a real number or string is expected). The type can be specified in two ways: assign an initial value of a specific type to a variable before using INPUT (if the variable to which you want to enter a new value already has some value, then the value of the same type is expected), or specify the type with an argument for INPUT, for example, for a matrix it is type 4: INPUT({{A,[4]},"Enter matrix A")

    The code that implements this method looks like this:
    Code:
    EXPORT SavingsMatrix()
    BEGIN
    // Input the square matrix A
    LOCAL A, B, S, n, i, j;

    // Prompt the user for inputs
    INPUT({{A,[4]}},"Enter matrix A");
    INPUT({{B,[4]}},"Enter vector B");

    // Validate dimensions
    n := length(B); // Size of B must match rows/cols of A
    IF rowDim(A)≠n OR colDim(A)≠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:");
    PRINT(S);
    END;

    In this case, the result will be printed in the terminal (single line in algebraic mode), but it will also be returned as a result in the command line (if it is run from the command line).
     
  3. The third approach is to use a matrix editor, which will open editing dialogs to input matrix data. The result can also be presented in this way by providing the readonly argument with a value of 1. This approach requires assigning an initial value (an empty/zero matrix [[0]] or [0]) to a variable or using matrix variables (M0-M9).
    Here is the code that implements this:
    Code:
    EXPORT SavingsMatrix()
    BEGIN
    LOCAL A=[[0]], B=[0], S, n, i, j;

    // Prompt the user for inputs
    EDITMAT(A, "Enter matrix A: ");
    EDITMAT(B, "Enter vector B: ");

    // Validate dimensions
    n := length(B); // Size of B must match rows/cols of A
    IF rowDim(A)≠n OR colDim(A)≠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;

    // Show the result (readonly mode)
    EDITMAT(S,"Savings Matrix S:",1);
    END;

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

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;
instead of using two loops, you can perform the calculations directly in MAKEMAT using just one line that initializes the matrix and immediately calculates the individual values:
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
Find all posts by this user
Quote this message in a reply
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.

  1. You can write your function in such a way that it takes a matrix and a vector from the command line, and then you would call it like this:
    SavingsMatrix([[1,2,3],[4,5,6],[7,8,9]],[1,2,3])

    The code that implements this does not need to include data input, as the data is provided as parameters.
    Code:
    EXPORT SavingsMatrix(A,B)
    BEGIN
    LOCAL S, n, i, j;

    // Validate dimensions
    n := length(B); // Size of B must match rows/cols of A
    IF rowDim(A)≠n OR colDim(A)≠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;

    RETURN S;
    END;
    This is probably the most sensible approach, as it allows the use of such functions in various programs and the direct return of results to variables, which can then be used in further calculations within the program.
     
  2. The second approach is to retrieve the matrix and vector using the INPUT command, but to do this, you must specify the type of data you want to retrieve (by default, a real number or string is expected). The type can be specified in two ways: assign an initial value of a specific type to a variable before using INPUT (if the variable to which you want to enter a new value already has some value, then the value of the same type is expected), or specify the type with an argument for INPUT, for example, for a matrix it is type 4: INPUT({{A,[4]},"Enter matrix A")

    The code that implements this method looks like this:
    Code:
    EXPORT SavingsMatrix()
    BEGIN
    // Input the square matrix A
    LOCAL A, B, S, n, i, j;

    // Prompt the user for inputs
    INPUT({{A,[4]}},"Enter matrix A");
    INPUT({{B,[4]}},"Enter vector B");

    // Validate dimensions
    n := length(B); // Size of B must match rows/cols of A
    IF rowDim(A)≠n OR colDim(A)≠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:");
    PRINT(S);
    END;

    In this case, the result will be printed in the terminal (single line in algebraic mode), but it will also be returned as a result in the command line (if it is run from the command line).
     
  3. The third approach is to use a matrix editor, which will open editing dialogs to input matrix data. The result can also be presented in this way by providing the readonly argument with a value of 1. This approach requires assigning an initial value (an empty/zero matrix [[0]] or [0]) to a variable or using matrix variables (M0-M9).
    Here is the code that implements this:
    Code:
    EXPORT SavingsMatrix()
    BEGIN
    LOCAL A=[[0]], B=[0], S, n, i, j;

    // Prompt the user for inputs
    EDITMAT(A, "Enter matrix A: ");
    EDITMAT(B, "Enter vector B: ");

    // Validate dimensions
    n := length(B); // Size of B must match rows/cols of A
    IF rowDim(A)≠n OR colDim(A)≠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;

    // Show the result (readonly mode)
    EDITMAT(S,"Savings Matrix S:",1);
    END;

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

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;
instead of using two loops, you can perform the calculations directly in MAKEMAT using just one line that initializes the matrix and immediately calculates the individual values:
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.


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!!! Smile I appreciate it
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 2 Guest(s)