#include <stdio.h>

#define M 21
#define N 2
#define INPUTFILE "C:\\Documents and Settings\\Duy Anh Bui\\My Documents\\Uni Stuff\\Eng Computing\\Sourcecodes\\Exercise 2\\in.dat"
#define ARRAYFILE "C:\\Documents and Settings\\Duy Anh Bui\\My Documents\\Uni Stuff\\Eng Computing\\Sourcecodes\\Exercise 2\\array.dat"

float Quadratic(float,float,float);                                               //Quadratic Function Prototype

int main ()                                                                       // Start main()
{
	float a[M][N] = {0};                                                          // Array a declaration, this array is used for holding data read from in.dat.
    float sumY=0.0;                                                               // Sum of Y
    float sumX=0.0;                                                               // Sum of X
    float sumX2=0.0;                                                              // Sum of X^2
    float sumX3=0.0;                                                              // Sum of X^3
    float sumX4=0.0;                                                              // Sum of X^4
    float sumYX=0.0;                                                              // Sum of Y*X
    float sumYX2=0.0;                                                             // Sum of Y*X^2

    float MAdelta[3][3]  = {0};                                                   // Array Matrix Delta declaration
    float MAa1[3][3] = {0};                                                       // Array Matrix a1 declaration
    float MAa2[3][3] = {0};                                                       // Array Matrix a2 declaration
    float MAa3[3][3] = {0};                                                       // Array Matrix a3 declaration

    float detDelta;                                                               // Determinant of Matrix Delta
    float deta1=0;                                                                // Determinant of Matrix a1
    float deta2=0;                                                                // Determinant of Matrix a2
    float deta3=0;                                                                // Determinant of Matrix a3

    float a1=0;                                                                   // Coefficient a1
    float a2=0;                                                                   // Coefficient a2
    float a3=0;                                                                   // Coefficient a3

    float temp;                                                                   // This variable holds the temporary value of the temperatures asked.

   FILE *fp1;                                                                     // File pointer: fp1 is for reading from in.dat.
   FILE *fp2;                                                                     // File pointer: fp2 is for writing to array.dat.
   int i, j;



   fp1=fopen(INPUTFILE, "r");                                                     // Open in.dat for reading.



          for(i=0; i<M; i++)                                                             // Read from in.dat the data and input in the array a[][].
             {
               	for(j=0;j<N;j++)
                   {
                     	fscanf(fp1," %f", &a[i][j]);
                   }
             }



   fclose(fp1);                                                                   // Close the stream.

printf(" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ \n");
printf("|   Engineering Computing 1          |\n");
printf("|   Assignment/Tutorial 3 Program    |\n");
printf("|   Duy Anh Bui - a1176112           |\n");
printf("|_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |\n\n");

/*Use printf() function to print the data, that was read from the file, to the monitor screen.*/
printf("//   DATA ENTRY \n\n");
printf("Temperature\tMeasured Voltage\n");

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

/*Compute sum of Y*/

for(i=0;i<M;i++)
{
sumY += a[i][1];
}

/*Compute sum of X*/

for(i=0;i<M;i++)
{
sumX += a[i][0];
}

/*Compute sum of X^2*/

for(i=0;i<M;i++)
{
sumX2 += (a[i][0])*(a[i][0]);
}

/*Compute sum of X^3*/
for(i=0;i<M;i++)
{
sumX3 += (a[i][0])*(a[i][0])*(a[i][0]);
}

/*Compute sum of X^4*/
for(i=0;i<M;i++)
{
sumX4 += (a[i][0])*(a[i][0])*(a[i][0])*(a[i][0]);
}

/*Compute sum of Y*X*/
for(i=0;i<M;i++)
{
sumYX += (a[i][1])*(a[i][0]);
}

/*Compute sum of Y*X^2*/
for(i=0;i<M;i++)
{
sumYX2 += (a[i][1])*(a[i][0])*(a[i][0]);
}

/* Assigning Variables to Matrix Delta */
MAdelta[0][0] =  21 ;
MAdelta[1][0] =  sumX ;
MAdelta[2][0] =  sumX2 ;

MAdelta[0][1] =  sumX ;
MAdelta[1][1] =  sumX2 ;
MAdelta[2][1] =  sumX3 ;

MAdelta[0][2] =  sumX2 ;
MAdelta[1][2] =  sumX3 ;
MAdelta[2][2] =  sumX4 ;


/* Assigning Variables to Matrix a1 */
MAa1[0][0] = sumY ;
MAa1[1][0] = sumYX ;
MAa1[2][0] = sumYX2 ;

MAa1[0][1] = sumX ;
MAa1[1][1] = sumX2 ;
MAa1[2][1] = sumX3 ;

MAa1[0][2] = sumX2 ;
MAa1[1][2] = sumX3 ;
MAa1[2][2] = sumX4 ;


/* Assigning Variables to Matrix a2 */
MAa2[0][0] = 21 ;
MAa2[1][0] = sumX ;
MAa2[2][0] = sumX2 ;

MAa2[0][1] = sumY ;
MAa2[1][1] = sumYX ;
MAa2[2][1] = sumYX2 ;

MAa2[0][2] = sumX2 ;
MAa2[1][2] = sumX3 ;
MAa2[2][2] = sumX4 ;

/* Assigning Variables to Matrix a3 */

MAa3[0][0] = 21 ;
MAa3[1][0] = sumX ;
MAa3[2][0] = sumX2 ;

MAa3[0][1] = sumX ;
MAa3[1][1] = sumX2 ;
MAa3[2][1] = sumX3 ;

MAa3[0][2] = sumY ;
MAa3[1][2] = sumYX ;
MAa3[2][2] = sumYX2 ;

// Print out the matrices

 fp2= fopen(ARRAYFILE,"w");                                                       // Open array.dat for writing.

 printf("\n");
 printf("Matrix Delta /_\\ \n");
 fprintf(fp2,"Matrix Delta /_\\ \n");

 for(i=0; i<3; i++)
   {
   	for(j=0;j<3;j++)
      {
      	 fprintf(fp2,"%f\t", MAdelta[i][j]);                                      // Printing Matrix Delta out to array.dat.
         printf("%f\t", MAdelta[i][j]);                                           // Printing Matrix Delta out to monitor.
      }
      fprintf(fp2,"\n");
      printf("\n");
   }



 printf("\n");
 printf("Matrix a1 \n");
 fprintf(fp2,"\n");
 fprintf(fp2,"Matrix a1 \n");

  for(i=0; i<3; i++)
   {
   	for(j=0;j<3;j++)
      {

      	 fprintf(fp2,"%f\t", MAa1[i][j]);                                          // Printing Matrix a1 out to array.dat.
         printf("%f\t", MAa1[i][j]);                                               // Printing Matrix a1 out to monitor.
      }
      fprintf(fp2,"\n");
      printf("\n");
   }



 printf("\n");
 printf("Matrix a2 \n");
 fprintf(fp2,"\n");
 fprintf(fp2,"Matrix a2 \n");

  for(i=0; i<3; i++)
   {
   	for(j=0;j<3;j++)
      {

      	 fprintf(fp2,"%f\t", MAa2[i][j]);                                          // Printing Matrix a2 out to array.dat
         printf("%f\t", MAa2[i][j]);                                               // Printing Matrix a2 out to monitor.
      }
      fprintf(fp2,"\n");
      printf("\n");
   }



 printf("\n");
 printf("Matrix a3 \n");
 fprintf(fp2,"\n");
 fprintf(fp2,"Matrix a3 \n");

  for(i=0; i<3; i++)
   {
   	for(j=0;j<3;j++)
      {

      	 fprintf(fp2,"%f\t", MAa3[i][j]);                                           // Printing Matrix a3 out to array.dat
         printf("%f\t", MAa3[i][j]);                                                // Printing Matrix a3 out to monitor.
      }
      fprintf(fp2,"\n");
      printf("\n");
   }
printf("\n");
printf("\n");
 fclose(fp2);

/* Determinant Calculations */

// Calculating the determinant of Matrix Delta
detDelta = MAdelta[0][0]*(MAdelta[1][1]*MAdelta[2][2] -MAdelta[1][2]*MAdelta[2][1])+MAdelta[0][1]*(MAdelta[1][2]*MAdelta[2][0]-MAdelta[1][0]*MAdelta[2][2])+MAdelta[0][2]*(MAdelta[1][0]*MAdelta[2][1]-MAdelta[1][1]*MAdelta[2][0]);


// Calculating the determinant of Matrix a1
deta1 = MAa1[0][0]*(MAa1[1][1]*MAa1[2][2] -MAa1[1][2]*MAa1[2][1])+MAa1[0][1]*(MAa1[1][2]*MAa1[2][0]-MAa1[1][0]*MAa1[2][2])+MAa1[0][2]*(MAa1[1][0]*MAa1[2][1]-MAa1[1][1]*MAa1[2][0]);


// Calculating the determinant of Matrix a2
deta2 = MAa2[0][0]*(MAa2[1][1]*MAa2[2][2] -MAa2[1][2]*MAa2[2][1])+MAa2[0][1]*(MAa2[1][2]*MAa2[2][0]-MAa2[1][0]*MAa2[2][2])+MAa2[0][2]*(MAa2[1][0]*MAa2[2][1]-MAa2[1][1]*MAa2[2][0]);


// Calculating the determinant of Matrix a3
deta3 = MAa3[0][0]*(MAa3[1][1]*MAa3[2][2] -MAa3[1][2]*MAa3[2][1])+MAa3[0][1]*(MAa3[1][2]*MAa3[2][0]-MAa3[1][0]*MAa3[2][2])+MAa3[0][2]*(MAa3[1][0]*MAa3[2][1]-MAa3[1][1]*MAa3[2][0]);

printf("//   RESULTS REPORTING\n\n", a1);

/* Quadratic coefficients calculations */
a1 = deta1/detDelta;
a2 = deta2/detDelta;
a3 = deta3/detDelta;

printf("a1 = %f\n", a1);                                                         // Print out a1 on the screen.
printf("a2 = %f\n", a2);                                                         // Print out a2 on the screen.
printf("a3 = %f\n", a3);                                                         // Print out a3 on the screen.

printf("\n");

/* Finding Temperature Values for 13mV, 23mV, 50mV, 62mV, 70mV, 105mV. */

temp = Quadratic(a3,a2,(a1-13));                                                   // Using Quadractic() to calculate the temperature.
printf("For the value 13mV,  the temperature is %.2f\n", temp);                    // Print out the result to the screen.

temp = Quadratic(a3,a2,(a1-23));                                                   // Using Quadractic() to calculate the temperature
printf("For the value 23mV,  the temperature is %.2f\n", temp);                    // Print out the result to the screen.

temp = Quadratic(a3,a2,(a1-50));                                                   // Using Quadractic() to calculate the temperature.
printf("For the value 50mV,  the temperature is %.2f\n", temp);                    // Print out the result to the screen.

temp = Quadratic(a3,a2,(a1-62));                                                   // Using Quadractic() to calculate the temperature.
printf("For the value 62mV,  the temperature is %.2f\n", temp);                    // Print out the result to the screen.

temp = Quadratic(a3,a2,(a1-70));                                                   // Using Quadractic() to calculate the temperature.
printf("For the value 70mV,  the temperature is %.2f\n", temp);                    // Print out the result to the screen.

temp = Quadratic(a3,a2,(a1-105));                                                  // Using Quadractic() to calculate the temperature.
printf("For the value 105mV, the temperature is %.2f\n", temp);                    // Print out the result to the screen.

printf("\nCALCULATION COMPLETE, PLEASE PRESS [ q ] & [ ENTER ] TO QUIT...");

   while(getchar()!='q'){}
   return 0;
}


float Quadratic(float a,float b,float c)                                           // Function Quadratic declaration
{

float disc;                                                                        // Discriminant variable
float r;                                                                           // Result variable
float temp;                                                                        // Temporary variable to hold square root value of disc

disc = b*b - 4*a*c;                                                                // Calculating the Discriminant


temp = sqrt(disc);
r = (-b + temp)/(2.0*a);


return r;
}

At DATA input section(after variables declarations and before the label), i need to use EOF to find the endof file so they stop reading. This is not really necessary, but my teacher required me to do so.
(because i know how many times i need to scan anyway).

How do i intergrate the use of EOF into my program ?

This is the requirement

File Input
(Reads in file correctly? Uses EOF to stop reading?)
for(i=0; i<M && !fp1.eof(); i++)    
          {
               	for(j=0;j<N !fp1.eof();j++)
                {
                  	fscanf(fp1," %f", &a[i][j]);
                }
          }

or something like this

i= 0;
j = 0;
while( fscanf(fp1," %f", &a[i][j]) > 0)
{
     j++;
     if( j == N)
     {
          j=  0;
          i++;
      }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.