Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

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!!!!!!!
0
pedro10
Asked:
pedro10
  • 3
  • 2
  • 2
  • +5
1 Solution
 
pedro10Author Commented:
Edited text of question
0
 
pedro10Author Commented:
need help soon
0
 
warren_daleCommented:
What problems? Give us some input data to test the program with.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ahoffmannCommented:
#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 );
}
0
 
liorliatCommented:
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.     :)
0
 
lafangaCommented:
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;

    }
0
 
ozoCommented:
I don't seee error changing inside of
while(error>= 1.0E-6)
0
 
pedro10Author Commented:
dont understand what you mean. i will elaborate more so you can help me
0
 
ozoCommented:
Is tv ever supposed to change in the loop?
0
 
ahoffmannCommented:
como ozo dice, creo que el ultimo while(error>=1.0E-6) esta infinito, porque hay no assignment de error=
0
 
bflVijayCommented:
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?
0
 
RONSLOWCommented:
I have provided an answer to this elsewhere for you.

There are a number of problems with the code .. the initializing of the arrays is stuffed.  You don't calculate a new value for error in each iteration (so the loop will be infinite).  You don't copy tn back into tv so that the calc of temps iterates (you just do the first calc over and over again).  Also floats aren't accurate enough (in general) for this sort of thing - you should use doubles instead



0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now