Link to home
Start Free TrialLog in
Avatar of pedro10
pedro10

asked on

arrays and file i/o

     /*this programs calculates the temperature of a piece of metal and the new temperature adquire from the formula.*/
 #include <stdlib.h>
 #include <stdio.h>
 #include <math.h>

 void main (void)
 {
      /* Declaracion de variables*/
 float tvnorte, tvsur, tveste, tvoeste;
 float tv[10][15], tn[10][15], error;
 int i, j;

      /*Leer valores constantes de un file*/
      FILE*viejo;
      FILE*nuevo;
      nuevo=fopen("nuevo.out","w");
      viejo=fopen("viejo.dat","r");

      fscanf(viejo, "tvnorte= %f",&tvnorte);
      fscanf(viejo, "tvsur= %f",&tvsur);
      fscanf(viejo, "tveste= %f",&tveste);
      fscanf(viejo, "tvoeste= %f",&tvoeste);


      tv[0][0] = (tvnorte +tvoeste)/2.0;
      tv[0][14] = (tvnorte + tveste)/2.0;
      tv[9][0] = (tvsur + tvoeste)/2.0;
      tv[9][14] = (tvsur + tveste)/2.0;

      tn[0][0] = tv[0][0];
      tn[0][14] = tv[0][14];
      tn[9][0] = tv[9][0];
      tn[9][14] = tv[9][14];

      for(j=1;j<=13; j++)
      {
      i=0;
      tv[i][j] = 100.0;
      tn[i][j] = tv[i][j];
      }
      for (i=1; i<=8; i++)
      {j=0;
      tv[i][j] = 0.0;
      tn[i][j] = tv[i][j];
      }
      for(j=1;j<=13; j++)
      {i=9;
      tv[i][j] = 50.0;
      tn[i][j] = tv[i][j];
      }
      for(i=1;i<=8; i++)
      {j=14;
      tv[i][j] = 75.0;
      tn[i][j] = tv[i][j];
      }
      for(i=1;i<=8;i++)
            {
            for(j=1;j<=13;j++)
                  {
                   tv[i][j] = 24.0;
                  }
            }
      fprintf(nuevo,"tabla temperatura vieja:/n");
            for(i=0;i<=9;i++)
                  {
                        for(j=0;j<=14;j++)
                         {fprintf(nuevo,"%3.1f", tv[i][j]);}
                  fprintf(nuevo,"\n");
                  }


      /*tv ends here*/
      error = fabs(tv[i][j] - tn[i][j]);
      while(error>= 1.0E-6)
            for(i=1;i<=8;i++)
             {
                  for(j=1;j<=13;j++)
                        {tn[i][j] = (tv[i-1][j]+tv[i][j-1]+tv[i][j+1]+tv[i+1][j])/4.0;}
             }
      fprintf(nuevo,"tabla temperatura nueva:/n");
            for(i=0;i<=9;i++)
                  {
                        for(j=0;j<=14;j++)
                         {fprintf(nuevo,"%3.1f", tn[i][j]);}
                  fprintf(nuevo,"\n");
                  }
      fclose(nuevo);
      fclose(viejo);
      }


im having problems with the file of the output!!!!!!!
Avatar of pedro10
pedro10

ASKER

Edited text of question
Avatar of pedro10

ASKER

need help soon
What problems? Give us some input data to test the program with.
#include <errno.h>
#include <string.h>
FILE *nuevo = NULL;
FILE *viejo = NULL;
errno = 0;
nuevo=fopen("nuevo.out","w");
if (nuevo == NULL || errno != 0) {
   perror("problema con usar nuevo.out");
   exit( error );
}
viejo=fopen("viejo.dat","r");
if (nuevo == NULL || errno != 0) {
   perror("problema con usar viejo.dat");
   exit( error );
}
whene working with files, it is recommended to close a file between writing and reading commands. there's a bug that sometimes causes problems with the buffer. so try to close the file after your last scanf, and reopen before fprintf.together
with ahoffmann addition that shuld do it.
let me know if it all worked out.     :)
Well what exactly is the problem?
1) The program does not execute :: any specific errors
2) The files can not be opened :: try using ahoffmann's code to test it.
3) The Output is Incorrect :: then it is a logical error check your algorithm

Suggested::

/*this programs calculates the temperature of a piece of metal and the new temperature adquire from the formula.*/

     #include <stdlib.h>
     #include <stdio.h>
     #include <math.h>

     int main (void)
     {
    /* Declaracion de variables*/
     float tvnorte, tvsur, tveste, tvoeste;
     float tv[10][15], tn[10][15], error;
     int i, j;

    /*Leer valores constantes de un file*/
    FILE  *viejo;  /* Input File */
    FILE  *nuevo;  /* Output File */

   if( ( viejo = fopen("viejo.dat","r") ) == NULL)
   {
      printf(" \n problema con usar viejo.dat");
      exit(0); /* exit from the program */
   }

    fscanf(viejo, "tvnorte= %f",&tvnorte);
    fscanf(viejo, "tvsur= %f",&tvsur);
    fscanf(viejo, "tveste= %f",&tveste);
    fscanf(viejo, "tvoeste= %f",&tvoeste);

   fclose(viejo);

    if( (nuevo=fopen("nuevo.out","w")) == NULL)
    {
       printf(" \n problema con usar nuevo.out");
       exit( 0 ); /* exit from the program */
    }

    tv[0][0] = (tvnorte +tvoeste)/2.0;
    tv[0][14] = (tvnorte + tveste)/2.0;
    tv[9][0] = (tvsur + tvoeste)/2.0;
    tv[9][14] = (tvsur + tveste)/2.0;

    tn[0][0] = tv[0][0];
    tn[0][14] = tv[0][14];
    tn[9][0] = tv[9][0];
    tn[9][14] = tv[9][14];

    for(j=1;j<=13; j++)
    {
    i=0;
    tv[i][j] = 100.0;
    tn[i][j] = tv[i][j];
    }
    for (i=1; i<=8; i++)
    {j=0;
    tv[i][j] = 0.0;
    tn[i][j] = tv[i][j];
    }
    for(j=1;j<=13; j++)
    {i=9;
    tv[i][j] = 50.0;
    tn[i][j] = tv[i][j];
    }
    for(i=1;i<=8; i++)
    {j=14;
    tv[i][j] = 75.0;
    tn[i][j] = tv[i][j];
    }
    for(i=1;i<=8;i++)
    {
    for(j=1;j<=13;j++)
    {
    tv[i][j] = 24.0;
    }
    }
    fprintf(nuevo,"tabla temperatura vieja:/n");
    for(i=0;i<=9;i++)
    {
    for(j=0;j<=14;j++)
    {fprintf(nuevo,"%3.1f", tv[i][j]);}
    fprintf(nuevo,"\n");
    }


    /*tv ends here*/
    error = fabs(tv[i][j] - tn[i][j]);
    while(error>= 1.0E-6)
    for(i=1;i<=8;i++)
    {
    for(j=1;j<=13;j++)
    {tn[i][j] = (tv[i-1][j]+tv[i][j-1]+tv[i][j+1]+tv[i+1][j])/4.0;}
    }
    fprintf(nuevo,"tabla temperatura nueva:/n");
    for(i=0;i<=9;i++)
    {
    for(j=0;j<=14;j++)
    {
        fprintf(nuevo,"%3.1f", tn[i][j]);
    }
        fprintf(nuevo,"\n");
    }

    fclose(nuevo);
    return 0;

    }
Avatar of ozo
I don't seee error changing inside of
while(error>= 1.0E-6)
Avatar of pedro10

ASKER

dont understand what you mean. i will elaborate more so you can help me
Is tv ever supposed to change in the loop?
como ozo dice, creo que el ultimo while(error>=1.0E-6) esta infinito, porque hay no assignment de error=
Hi,
    Delete this question and maintain the second question you have asked. You can retain your points and also get an immediate answer.
    As to the question itself, can you be a bit more clear, as to what the problem is? You have the program, but it does not compile? Or do you have problems in the output?
ASKER CERTIFIED SOLUTION
Avatar of RONSLOW
RONSLOW

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial