Write C header program matrix.h to define the function prototypes of the following matrix operations, and C program matrix.c to implement the functions of the
Write C header program matrix.h to define the function prototypes of the following matrix operations, and C program matrix.c to implement the functions of the matrix operations.
/* * Computes and returns the sum of all elements of n-dimensional vector v */ float vsum(int *v, n); /* * Computes and returns the sum of all elements of the n-by-n matrix m. */ float msum(float *m, n); /* * Transposes the n-by-n matrix m1 and save the resulted matrix in m2. */ void transpose_matrix(float *m1, float *m2, int n); /* * Computes the matrix multiplication m1*m2 and saves the resulted matrix in m3. */ void multiply_matrix(float *m1, float *m2, float *m3, int n); /* * Computes n-by-n matrix multiplies n-vector m*v1 and saves the result vector in v2. */ void multiply_vector(float *m, float *v1, float *v2, int n);
Use the provided the main function program matrix_main.c to test the above functions. The output is like the following.
Public test
command: gcc matrix.c matrix_main.c -o matrix command: matrix m1: 1.00 2.00 -1.00 -2.00 0.00 1.00 1.00 -1.00 0.00 msum(m1):1.00 m2=m1': 1.00 -2.00 1.00 2.00 0.00 -1.00 -1.00 1.00 0.00 msum(m2):1.00 m3=m1*m2': 6.00 -3.00 -1.00 -3.00 5.00 -2.00 -1.00 -2.00 2.00 msum(m3):1.00 v1: 1.00 1.00 1.00 vsum(v1):3.00 v2=m1*v1': 2.00 -1.00 0.00 vsum(v2):1.00
matrix_main.c:
#include
void display_vector(float *v, int n); void display_matrix(float *m, int n);
int main() { int n = 3, i; float m1[n][n]; float m2[n][n]; float m3[n][n];
// testing data float v[9] = { 1, 2, -1, -2, 0, 1, 1, -1, 0};
// assign value to m1 float *p = &v[0]; float *p1 = &m1[0][0]; for (i = 0; i < n * n; i++) *p1++ = *p++;
printf(" m1: "); display_matrix(&m1[0][0], n); printf("msum(m1):%.2f ", msum(&m1[0][0], n));
printf(" m2=m1': "); transpose_matrix(&m1[0][0], &m2[0][0], n); display_matrix(&m2[0][0], n); printf("msum(m2):%.2f ", msum(&m2[0][0], n));
multiply_matrix(&m1[0][0], &m2[0][0], &m3[0][0], n); printf(" m3=m1*m2': "); display_matrix(&m3[0][0], n); printf("msum(m3):%.2f ", msum(&m3[0][0], n));
float v1[] = {1, 1, 1}; printf(" v1: "); display_vector(v1, n); printf("vsum(v1):%.2f ", vsum(v1, n));
float v2[n]; multiply_vector(&m1[0][0], v1, v2, n); printf(" v2=m1*v1': "); display_vector(v2, n); printf("vsum(v2):%.2f", vsum(v2, n));
return 0; }
void display_vector(float *v, int n) { float *p = v; int i, j; for (i = 0; i < n; i++) { printf("%-6.2f", *p++); } printf(" "); }
void display_matrix(float *m, int n) { float *p = m; int i, j; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) printf("%-6.2f", *(m+i*n+j)); // or printf("%-6.2f", *p++); printf(" "); } } matrix.h:
/* * your program signature */ #ifndef MATRIX_H #define MATRIX_H float vsum(float *v, int n); float msum(float *m, int n); void multiply_vector(float *m, float *v1, float *v2, int n); void transpose_matrix(float *m1, float *m2, int n); void multiply_matrix(float *m1, float *m2, float *m3, int n); #endif
matrix.c:
/* * your program signature */ #include#include "matrix.h" float vsum(float *v, int n) { // your implementation } float msum(float *m, int n) { // your implementation } void multiply_vector(float *m, float *vin, float *vout, int n) { // your implementation } void transpose_matrix(float *m1, float *m2, int n) { // your implementation } void multiply_matrix(float *m1, float *m2, float *m3, int n) { // your implementation }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started