1. Engineering
  2. Computer Science
  3. i need help with this question please whats give is...

Question: i need help with this question please whats give is...

Question details

Write C programs named matrix.h and matrix.c containing the following the headers and implementations of following functions. 1. void magic square(int m, int n)* which generates an n by n magic square and stores it in matrix m (only n-3 is required) 2. void display matrix(int m, n)* which displays n by n matrix m in 2D style 3. inl is magic square(int m, int n)* which checks if the given matrix is a magic square, returns 1 if true otherwise0 4. void transpose_matrix(int ml, int *m2, int n) which transposes the n by n matrix ml and save the resulted matrix in m2. 5. void multiply matrix(int ml, int* m2, int m3 int n) which computes the matrix multiplication ml*m2 and saves the resulted matrix in m3 6. Use the provided the main function in a3help to create a3q2.c to test the above functions, the output is like the following public test gcc matrix.c a3q2.c -o a3q2 a3q2 m1 3 5 7 is_magic_square:1 m1 8 34 is-magic-square: 1 m1*m1 101 71 53 71 83 71 53 71 101 is_magic_square:e

I need help with this question please, Whats give is below. The program is also written in C

matrix.h

#include <stdio.h>
#include <stdlib.h>

void magic_square(int *m, int n);
int is_magic_square(int *m, int n);
void display_matrix(int *m, int n);
void transpose_matrix(int *m1, int *m2, int n);
void multiply_matrix(int *m1, int *m2, int *m3, int n);

matrix.c

//---------------------------------------------------------
void magic_square(int *m, int n) {
    /* assign 3X3 matrix to following values
     8     1     6
     3     5     7
     4     9     2
     */
    int values[9] = { 8, 1, 6, 3, 5, 7, 4, 9, 2 };
    int i, len = n*n, *p = values;
    for (i = 0; i < len; i++) *m++ = *p++;
}


//---------------------------------------------------------
void display_matrix(int *m, int n) {
    int *p = m, i, j;
    for (i = 0; i < n; i++) {
        printf("
");
        for (j = 0; j < n; j++) printf("%4d", *p++); 
    }
    printf("
");
}

//---------------------------------------------------------
int is_magic_square(int *m, int n) {
// your implementation
}

//---------------------------------------------------------
void transpose_matrix(int *m1, int *m2, int n) {
// your implementation      
}

//---------------------------------------------------------
void multiply_matrix(int *m1, int *m2, int *m3, int n) {    
// your implementation
}

a3q2.c

#include "matrix.h"

int main() {
    int n = 3;
    int m1[n][n];
    int m2[n][n];
    int m3[n][n];
    
    int *p1 = &m1[0][0];
    magic_square(p1, n);
    printf("
m1:");
    display_matrix(p1, n);
    printf("is_magic_square:%d
", is_magic_square(p1, n)); 
    
    int *p2 = &m2[0][0];    
    printf("
m1':");
    transpose_matrix(p1, p2, n);
    display_matrix(p2, n);
    printf("is_magic_square:%d
", is_magic_square(p2, n)); 
    
    int *p3 = &m3[0][0];
    multiply_matrix(p1, p2, p3, n); 
    printf("
m1*m1':");
    display_matrix(p3, n);
    printf("is_magic_square:%d
", is_magic_square(p3, n)); 
    
    return 0;
}
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution