Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C language, please. Matrices In this exercise you will write several function

ID: 3735928 • Letter: I

Question

In C language, please.

Matrices In this exercise you will write several functions that operate on matrices or two-dimensional arrays. For this exercise, we will restrict our attention to square matrices-collections of numbers with the same number of columns and rows. You will place all the function prototypes in a header file named matrix_utils.h with their definitions in a source file named matrix_utils.c. You should test your functions by writing a driver program, but you need not hand it in. (a) Write a function that constructs a new n × n identity matric. An identity matrix is a square matrix with ones on the diagonal and zeros everywhere else int **getIdentityMatrix(int n); b) Write a function that takes two matrices and determines if they are equal (all of their elements are the same int isEqual (int **A, int **B, int n); c) Write a function that takes a matrix and an index i and returns a new array that

Explanation / Answer

Answer :

(A)

int **getIdentityMatrix(int n)

{

    // dynamically allocate memory for n x n matrix

    int **arr = (int **)malloc( n * sizeof(int *) );

   

    int i, j;

   

    for( i = 0 ; i < n ; i++ )

        arr[i] = (int *)malloc( n * sizeof(int) );

   

    // initialize array with 0

    for( i = 0 ; i < n ; i++ )

        for( j = 0 ; j < n ; j++ )

            arr[i][j] = 0;

       

    // set the disgonal values to 1

    for( i = 0 ; i < n ; i++ )

        arr[i][i] = 1;

   

    return arr;

}

(b)

int isEqual( int **A, int **B, int n )

{

    int i, j;

   

    // traverse the row

    for( i = 0 ; i < n ; i++ )

        // traverse the column

        for( j = 0 ; j < n ; j++ )

            // if the current (i, j) element of the two matrix are not equal

           if( A[i][j] != B[i][j] )

                return 0;

   

    // if the matrix are equal

    return 1;

}

(c)

int *getRow(int **A, int n, int i)

{

    int *arr = (int *)malloc( n * sizeof(int) );

   

    int j;

   

    for( j = 0 ; j < n ; j++ )

        arr[j] = A[i][j];

   

    return arr;

}

(d)

int *getCol(int **A, int n, int j)

{

    int *arr = (int *)malloc( n * sizeof(int) );

   

    int i;

   

    for( i = 0 ; i < n ; i++ )

        arr[i] = A[i][j];

   

    return arr;

}

E) and F)

matrix_utils.h

#ifndef MATRIX_UTILS_H

# define MATRIX_UTILS_H

int **product(int **A, int **B, int n);

int **matrixPower(int **A, int size, int n);

#endif

matrix_utils.c

#include "matrix_utils.h"

int **product(int **A, int **B, int n)

{

                int i, j, k;

                int** result = NULL;

                //allocate memory

                result = malloc( n * sizeof(int *));

                for(i = 0; i < n; i++)

                {

                                result[i] = malloc( n * sizeof(int));

                }

               

                for (i = 0; i < n; i++)

                {

                                for (j = 0; j < n; j++)

                                {

                                                result[i][j] = 0;

                                                for (k = 0; k < n; k++)

                                                                result[i][j] += A[i][k]*B[k][j];

                                }

                }

                return result;

}

int **matrixPower(int **A, int size, int n)

{

                int c, d, i, j, k; //variables

                //pointers

                int** result = NULL;

                int** finalResult = NULL;

                //allocate memory

                result = malloc( size * sizeof(int *));

                for(i = 0; i < size; i++)

                {

                                result[i] = malloc( size * sizeof(int));

                }

                finalResult = malloc( size * sizeof(int *));

                for(i = 0; i < size; i++)

                {

                                finalResult[i] = malloc( size * sizeof(int));

                }

                //if n is 0 return identity matrix

                if(n == 0)

                {

                                for ( c = 0 ; c < size ; c++ )

                                {

            for ( d = 0 ; d < size ; d++ )

                                                {

                                                                if(c == d)

                                                                                A[c][d] = 1;

                                                                else

                              A[c][d] = 0;

            }

        }

        return A;

                }

                //if n == 1, return A

                else if(n == 1)

                                return A;

                //make result = A

                for ( c = 0 ; c < size ; c++ )

                {

        for ( d = 0 ; d < size ; d++ )

                                {

           

            result[c][d] = A[c][d];

        }

    }

    //multiply n-1 times

    for ( c = 0; c < n-1; c++)

    {

                for (i = 0; i < size; i++)

                                {

                                                for (j = 0; j < size; j++)

                                                {

                                                                finalResult[i][j] = 0;

                                                                for (k = 0; k < size; k++)

                                                                                finalResult[i][j] += A[i][k]*result[k][j];

                                                }

                                }

                                //make result = finalResult

        for (i = 0 ; i < size ; i++ )

                                {

            for (j = 0 ; j < size ; j++ )

                                                {

                result[i][j] = finalResult[i][j];

                finalResult[i][j] = 0;

            }

        }

       

    }

    return result; //return result

}

driver.c

#include<stdio.h>

#include<stdlib.h>

#include "matrix_utils.c"

int main()

{

                int c,d, i;

                //int pointer

                int** A = NULL;

                //allocate memory

                A = malloc( 4 * sizeof(int *));

                for(i = 0; i < 4; i++)

                {

                                A[i] = malloc( 4 * sizeof(int));

                }

                //initialize A

                for ( c = 0 ; c < 4 ; c++ )

                {

        for ( d = 0 ; d < 4 ; d++ )

                                {

            A[c][d] = c + d;

        }

    }

   

    //get result

                int** result = product(A, A, 4);

                //print result

                for ( c = 0 ; c < 4 ; c++ )

                {

        for ( d = 0 ; d < 4 ; d++ )

                                {

            printf("%d ",result[c][d]);

        }

        printf(" ");

    }

    //get result

                result = matrixPower(A, 4, 3);

                //print result

                for ( c = 0 ; c < 4 ; c++ )

                {

        for ( d = 0 ; d < 4 ; d++ )

                                {

            printf("%d ",result[c][d]);

        }

        printf(" ");

    }

                return 0;

}