Question

Q2. Write a recursive function (C++ like code) that takes factor table and outputs the parenthesization of matrix that corres

0 0
Add a comment Improve this question Transcribed image text
Answer #1

void ShowOrder(int i, int j, int factor[][])
{
   static char name = 'A';
    if (i == j)
    {
        cout << name++;
    }
    else
        cout << "(";
    {
        ShowOrder(i, factor[i][j], factor );
        ShowOrder(factor[i][j] + 1, j, factor);
        cout << ")";
    }
}

int * MatrixChainOrder(int p[], int n)
{
    static   int m[100][100];
    static int s[100][100];
    int j, q;
    int min = INT_MAX;
    for (int i = 1; i <= n; i++)
        m[i][i] = 0;

    for (int L = 2; L <= n; L++) {
        for (int i = 1; i <= n - L + 1; i++) {
            j = i + L - 1;
            m[i][j] = min;
            for (int k = i; k <= j - 1; k++) {
                q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
                if (q < m[i][j]) {
                    m[i][j] = q;
                    s[i][j] = k;
                }
            }
        }
    }
    return (*s);
}

If you have any query feel free to ask in comments :)

Add a comment
Know the answer?
Add Answer to:
Q2. Write a recursive function (C++ like code) that takes factor table and outputs the parenthesi...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • a) You must write a recursive function that takes something of the form: ([(Int, Int, Int),...

    a) You must write a recursive function that takes something of the form: ([(Int, Int, Int), Int, Int) as an argument and returns something of the form: LE(Int, Int, Int)]] which should be interpreted as a list of lists of 3-tuples of RGB values. You may use helper functions if you wish, but your solution must be recursive. You must document the name of your function at the beginning of your code using a comment like -- foo :: ([(Int,...

  • Please write the complete code in C. Write a C function that prints the minimum spanning...

    Please write the complete code in C. Write a C function that prints the minimum spanning tree of a graph. At the end, print the weight of the spanning tree. A suggested report format is shown in the following example. Source Vertex To Vertex Weight A B 2 A C 4 B D 3 D E 1 Total weight of spanning tree: 10 Your main program will read a graph from DataIn file to an adjacency table before calling the...

  • In C++, write a recursive function power that takes two positive integers base and n as...

    In C++, write a recursive function power that takes two positive integers base and n as inputs and computes base to the n power. Example power(3, 2) is 9. Do not use any loops in your program. This is what I got so far but I'm not sure what I'm doing wrong: #include <iostream> using namespace std; int calc(int x, int y); int main() {       calc(2, 3);    return 0; } int calc() {          cout...

  • C++ 9) Write a recursive function printAll that takes an int num argument and prints all...

    C++ 9) Write a recursive function printAll that takes an int num argument and prints all the numbers in the range [num, 0]. The function returns nothing. e.g. printAll(5) prints 5, 4, 3, 2, 1, 0. Call your function in the main

  • Write a function which takes as input a matrix and outputs a matrix in which each...

    Write a function which takes as input a matrix and outputs a matrix in which each positive element is doubled. For instance, for the matrix A=[3, -1; -2, 4], and output should be [6,-1; -2,8].

  • Answer: cout << k/(i+j) Q1) Provide code that outputs a random number 10 through 20? Q2)...

    Answer: cout << k/(i+j) Q1) Provide code that outputs a random number 10 through 20? Q2) Provide code that outputs a random character 'a' through ‘y'? Q3) Using char c = 'A'; Output “BCD” without changing the value of c. Q2) Provide code that outputs a random character ‘a' through ‘y'?

  • Please code this in Java, thank you! (a) Implement a sublinear running time complexity recursive function...

    Please code this in Java, thank you! (a) Implement a sublinear running time complexity recursive function in Java public static long long x, int n) to calculate X Note: In your function you can use only the basic arithmetic operators (+, -,, %, and /) (b) What is the running time complexity of your function? Justify (c) Give a number of multiplications used by your function to calculate x63.

  • Write a recursive function (C++) that searches a binary tree that is NOT ordered like a...

    Write a recursive function (C++) that searches a binary tree that is NOT ordered like a BST. In other words, there is no ordering relationship among the parent, child or sibling node values. Use the following prototype and data structure: struct node { node *left; node *right; int val; }; // First parameter: pointer to a node // Second parameter: the value to find bool searchValue(node *, int); The function searchValue() should return true if the integer value in the...

  • c++ please. Write a recursive method that takes the following parameters: * a sorted array of...

    c++ please. Write a recursive method that takes the following parameters: * a sorted array of ints * an int representing the size of the array * an int representing a value to be searched for Your function should return a bool. If the element is in the array, return true. Otherwise, return false. Your function should never produce memory access errors, regardless of the size of the array.

  • IN C++: Please write a function called coin_row that takes in a vector (of type int)...

    IN C++: Please write a function called coin_row that takes in a vector (of type int) of coins in order and outputs a coin row solution. ( Find maximum possible sum of elements such that there are no 2 consecutive elements present in the sum.) val set to the value of the optimal solution coin_indices (0-indexed) set to the indices of the coins forming the optimal solution MUST BE DONE WITH DYNAMIC PROGRAMMING. NO RECURSION typedef struct coin_row_solution{ int val;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT