pvitkovsky
2/6/2018 - 3:19 AM

matrix*vector.c

print_matrix() print_vector() matrix_x_vector()

#include <stdio.h>

// Define Functions
void print_matrix(int n1, int m[n1][n1]); // Prints an nxn matrix
void print_vector(int n1, int v[n1]); // Prints a vector of n elements
void matrix_x_vector(int n1, int y[n1], int x[n1][n1], int A[n1]); // Performs the calculation
  
int main()
{
  // Define Variables
  int n1 = 3;
  int m1[3] = {4, 2, 1};
  int m2[3][3] = {
    {1,0,0},
    {0,1,0},
    {0,0,1},
  };
  int multiplied_matrix[3] = {
      {0},
      {0},
      {0},
  };

  // Execute Functions
  print_vector(n1, m1);
  print_matrix(n1, m2);
  matrix_x_vector(n1, m1, m2, multiplied_matrix);
  printf("\n");
}
void print_matrix(int n1, int m[n1][n1])
{
  int i, j; // i = row; j = column;
  printf("\nMatrix Given\n");
  for (i=0; i<n1; i++)
  {
    for (j=0; j<n1; j++)
      printf("%3i", m[i][j]);
    printf("\n");
  }
}
void print_vector(int n1, int v[n1])
{
  int i;
  printf("\nVector Given\n");
  for (i=0; i<n1; i++)
    printf("%3i", v[i]);
  printf("\n");
}
void matrix_x_vector(int n1, int y[n1], int x[n1][n1], int A[n1])
{
	int i, j;
	for (i=0; i<n1; i++)
	{
	    A[i] = 0;
		for (j=0; j<n1; j++)
		{
			A[i] += x[i][j] * y[j];
		}
	
	}
	print_vector(n1, A);
}