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

#include <stdio.h> #define MAX_NUM_ROWS 20 #define MAX_NUM_COLS 20 void printVec

ID: 3556768 • Letter: #

Question

#include <stdio.h>
#define MAX_NUM_ROWS 20
#define MAX_NUM_COLS 20

void printVector(int b[ ], int length);
void multMatrices(int a[ ][MAX_NUM_COLS], int x[ ], int b[ ], int rows, int cols);

int main ( )
{
int a[MAX_NUM_ROWS][MAX_NUM_COLS], x[MAX_NUM_COLS];
int b[MAX_NUM_ROWS];
int i, j, num_rows, num_cols;
FILE *infile;

infile=fopen("matvect4.dat","r");
fscanf(infile,"%d %d",&num_rows,&num_cols);

for (i=0; i<num_rows; i++){
for (j=0; j<num_cols; j++){
fscanf (infile,"%d",&a[i][j]);
}
}
for (i=0; i<num_cols; i++)
fscanf (infile,"%d",&x[i]);

multMatrices(a, x, b, num_rows, num_cols);
  
printf(" b vector");
printVector(b, num_rows);

return 0;
}

void printVector(int b[ ], int length)
{
int i;
  
for (i=0; i<length; i++)
{
printf(" %d", b[i]);
}
}

void multMatrices(int a[ ][MAX_NUM_COLS], int x[ ], int b[ ], int rows, int cols)
{
int i, j;
  
for (i=0; i<rows; i++)
{
b[i]=0;
for (j=0; j<cols; j++)
{
b[i] += a[i][j]*x[j];
}
}
}


/* OUTPUT: A6_3_4.c

  
   b vector
   55
   75
   71
   83
*/

Explanation / Answer

#include <stdio.h>
#define MAX_NUM_ROWS 20
#define MAX_NUM_COLS 20
void printmatrix(int b[ ], int row,int column);
void addMatrices(int a[ ][MAX_NUM_COLS], int b[ ][MAX_NUM_COLS ], int c[ ][MAX_NUM_COLS], int rows, int cols);
void addMatrices(int a[ ][MAX_NUM_COLS], int b[ ][MAX_NUM_COLS ], int d[ ][MAX_NUM_COLS], int rows, int cols);
int main ( )
{
int a[MAX_NUM_ROWS][MAX_NUM_COLS], b[MAX_NUM_ROWS][MAX_NUM_COLS],c[MAX_NUM_ROWS][MAX_NUM_COLS],d[MAX_NUM_ROWS][MAX_NUM_COLS];
int i, j, num_rows, num_cols;
FILE *infile;
infile=fopen("matvect4.dat","r");
fscanf(infile,"%d %d",&num_rows,&num_cols);
for (i=0; i<num_rows; i++){
for (j=0; j<num_cols; j++){
fscanf (infile,"%d",&a[i][j]);
}
}
for (i=0; i<num_rows; i++){
for (i=0; i<num_cols; i++)
fscanf (infile,"%d",&x[i]);
}
addMatrices(a, b, c, num_rows, num_cols);
subMatrices(a, b, d, num_rows, num_cols);
  
printf(" added matrix is");
printVector(c, num_rows,num_cols);
printf(" substracted matrix is");
printVector(d, num_rows,num_cols);
return 0;
}
void printVector(int e[ ], int length)
{
int i,j;
for (i=0; i<num_rows; i++)
{
for (j=0; j<num_rows; j++)
{
printf(" %d", c[i]);
}
}
}
void addMatrices(int a[ ][MAX_NUM_COLS], int b[ ][MAX_NUM_COLS], int c[ ][MAX_NUM_COLS], int rows, int cols)
{
int i, j;
  
for (i=0; i<rows; i++)
{
for (j=0; j<cols; j++)
{
c[rows][cols]=a[rows][cols]+b[rows][cols];
}
}
}

void subMatrices(int a[ ][MAX_NUM_COLS], int b[ ][MAX_NUM_COLS], int d[ ][MAX_NUM_COLS], int rows, int cols)
{
int i, j;
  
for (i=0; i<rows; i++)
{
for (j=0; j<cols; j++)
{
c[rows][cols]=a[rows][cols]-b[rows][cols];
}
}
}