Question

In: Computer Science

2. Using matrices, create an algorithm that takes a matrix of dimension N x N and...

2. Using matrices, create an algorithm that takes a matrix of dimension N x N and feed it in a spiral shape with the sequential number from 1 to N^2.

Then do an algorithm in PSEint

Solutions

Expert Solution

I'm writting c++ cpmplete programm please don't give thump down:

guide to to convert PSEint

// C++ Program to print a matrix spirally

#include <bits/stdc++.h>

using namespace std;

#define R 3

#define C 6

void spiralPrint(int m, int n, int a[R][C])

{

    int i, k = 0, l = 0;

    /* k - starting row index

        m - ending row index

        l - starting column index

        n - ending column index

        i - iterator

    */

    while (k < m && l < n) {

        /* Print the first row from

               the remaining rows */

        for (i = l; i < n; ++i) {

            cout << a[k][i] << " ";

        }

        k++;

        /* Print the last column

         from the remaining columns */

        for (i = k; i < m; ++i) {

            cout << a[i][n - 1] << " ";

        }

        n--;

        /* Print the last row from

                the remaining rows */

        if (k < m) {

            for (i = n - 1; i >= l; --i) {

                cout << a[m - 1][i] << " ";

            }

            m--;

        }

        /* Print the first column from

                   the remaining columns */

        if (l < n) {

            for (i = m - 1; i >= k; --i) {

                cout << a[i][l] << " ";

            }

            l++;

        }

    }

}

/* Driver Code */

int main()

{

    int a[R][C] = { { 1, 2, 3, 4, 5, 6 },

                    { 7, 8, 9, 10, 11, 12 },

                    { 13, 14, 15, 16, 17, 18 } };

    

      // Function Call

    spiralPrint(R, C, a);

    return 0;

}


Related Solutions

3. Suppose that a divide and conquer algorithm for multiplication of n x n matrices is...
3. Suppose that a divide and conquer algorithm for multiplication of n x n matrices is found such that it requires 6 multiplications and 31 additions of n/2 x n/2 submatrices. Write the recurrence for the running time T(n) of this algorithm and find the order of T(n).
create a function in matlab that sums two m x n matrices using nested loops, then...
create a function in matlab that sums two m x n matrices using nested loops, then returns result into a new matrix. Use nesed for loops to add matrices piece by piece. Basically means, dont program simply A+B Function should perform error check to make sure both matrices have same number of rows/ columns.
Recall the Matrix Chain Multiplication Algorithm for determining the optimal parenthesization for a product of matrices....
Recall the Matrix Chain Multiplication Algorithm for determining the optimal parenthesization for a product of matrices. Provide a recursive implementation of the function void print_parenth(Matrix K[], int i, int j); that takes as input the matrix K of k values that are needed to construct the optimal parenthesization for Ai · · · Aj . Assume access to a print function that takes as input a string and prints its value. You may also assume a “+” operation for string...
A m*n matrix A. P is the dimension of null space of A. What are the...
A m*n matrix A. P is the dimension of null space of A. What are the number of solutions to Ax=b in these cases. Prove your answer. a. m=6, n=8, p=2 b. m=6, n=10, p=5 c. m=8, n=6, p=0
5. Find a matrix A of rank 2 whose nullspace N(A) has dimension 3 and whose...
5. Find a matrix A of rank 2 whose nullspace N(A) has dimension 3 and whose transposed nullspace N(AT) has dimension 2.
Make a code in matlab to know the determinant of a matrix n x n, using...
Make a code in matlab to know the determinant of a matrix n x n, using the sarrus rule.
For matrices, a mulitplicative identity is a square matrix X such XA = AX = A...
For matrices, a mulitplicative identity is a square matrix X such XA = AX = A for any square matrix A. Prove that X must be the identity matrix. Prove that for any invertible matrix A, the inverse matrix must be unique. Hint: Assume that there are two inverses and then show that they much in fact be the same matrix. Prove Theorem which shows that Gauss-Jordan Elimination produces the inverse matrix for any invertible matrix A. Your proof cannot...
a. Using Matlab scripts create the following matrices (???1 and ???2) ???1 = [ 3 2...
a. Using Matlab scripts create the following matrices (???1 and ???2) ???1 = [ 3 2 −3 6 7 4 3 −6 7 ], ???2 = [ 2 1 7 3 3 9 −6 6 1 ]    b. Write code to add the second row of ???1 to the third row of ???2 and store results in the first row of ???1. c. Write code to add the second column of ???1 with the third column of ???2 and...
Derive the Dirac gamma matrices for 2-space dimensions and 1-time dimension.
Derive the Dirac gamma matrices for 2-space dimensions and 1-time dimension.
Question 1: Using Python 3 Create an algorithm The goal is to create an algorithm that...
Question 1: Using Python 3 Create an algorithm The goal is to create an algorithm that can sort a singly-linked-list with Merge-sort. The program should read integers from file (hw-extra.txt) and create an unsorted singly-linked list. Then, the list should be sorted using merge sort algorithm. The merge-sort function should take the head of a linked list, and the size of the linked list as parameters. hw-extra.txt provided: 37 32 96 2 25 71 432 132 76 243 6 32...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT