Sunday, February 1, 2015

Read input from file perform operation and print on console in C


filein.txt
1.2    2.1    3.2
3.1    5.0    5.1
2.4    2.1    2.1

#include <stdio.h>
void main()
{
  int i,j;
  float A[3][3];
  float B[3][3];
  int num;

  FILE *inputMatrix=fopen("filein.txt","r");
  for(i=0;i<3;++i){
     for(j=0;j<3;++j){
         fscanf(inputMatrix,"%f",&A[i][j]);
     }
  }
  fclose(inputMatrix);

// Operation (Addition)
 for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            B[i][j] = A[i][j] + A[i][j];
        }
    }

//Print

    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
        printf("\t %f",B[i][j]);
        }
       printf("\n");
    }
}

No comments: