C Language file operation 04--input matrix A, B, product C, and print a,b,c to file

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.