Combining files with arrays
/*
=================================================================
Title: Input matrix A, B, product C, and print a,b,c to D disk File X.txt, and on the screen display!
Description: A row of column =b must be met.
=================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#define M 2
#define N 3
void Main ()
{
FILE *FP;
int a[m][n],b[n][m],c[n][n];
int i,j,k=0;
Fp=fopen ("D:\\x.txt", "w");
if (fp==null)
printf ("File open failed!") \ n ");
Else
{
Input matrix A
printf ("Matrix A (%dx%d): \ n", m,n);
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
scanf ("%d", &a[i][j]);
}
Input Matrix B
printf ("Matrix B (%dx%d): \ n", n,m);
for (i=0;i<n;i++)
{
for (j=0;j<m;j++)
scanf ("%d", &b[i][j]);
}
System ("CLS");
Output matrix A
printf ("a=\n");
fprintf (FP, "a=\n");
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
printf ("%3d", A[i][j]);
fprintf (FP, "%3d", A[i][j]);
}
fprintf (FP, "\ n");
printf ("\ n");
}
Output Matrix B
printf ("b=\n");
fprintf (FP, "b=\n");
for (i=0;i<n;i++)
{
for (j=0;j<m;j++)
{
printf ("%3d", B[i][j]);
fprintf (FP, "%3d", B[i][j]);
}
fprintf (FP, "\ n");
printf ("\ n");
}
Compute Matrix C
for (i=0;i<m;i++)
{
for (j=0;j<m;j++)
{
c[i][j]=0;
for (k=0;k<n;k++)
C[I][J]+=A[I][K]*B[K][J];
}
}
Output Matrix C
printf ("c=\n");
fprintf (FP, "c=a*b=\n");
for (i=0;i<m;i++)
{
for (j=0;j<m;j++)
{
printf ("%3d", C[i][j]);
fprintf (FP, "%3d", C[i][j]);
}
printf ("\ n");
fprintf (FP, "\ n");
}
}
Fclose (FP);
}
/*
========================================================================
Evaluation:
The file fprintf (FP, "%3d", A[i][j]) is just as understandable as the general document printf!
========================================================================
*/
Copyright Notice: Bo Main article can be reproduced non-commercial, but please be sure to indicate the source, because the level is limited, inevitably error, in this disclaimer.
C Language file operation 04--input matrix A, B, product C, and print a,b,c to file