Solved

arrays and file i/o

Posted on 1998-12-08
12
200 Views
Last Modified: 2010-04-15
     /*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
Comment
Question by:pedro10
  • 3
  • 2
  • 2
  • +5
12 Comments
 

Author Comment

by:pedro10
ID: 1254959
Edited text of question
0
 

Author Comment

by:pedro10
ID: 1254960
need help soon
0
 
LVL 1

Expert Comment

by:warren_dale
ID: 1254961
What problems? Give us some input data to test the program with.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1254962
#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
 

Expert Comment

by:liorliat
ID: 1254963
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
 
LVL 1

Expert Comment

by:lafanga
ID: 1254964
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 84

Expert Comment

by:ozo
ID: 1254965
I don't seee error changing inside of
while(error>= 1.0E-6)
0
 

Author Comment

by:pedro10
ID: 1254966
dont understand what you mean. i will elaborate more so you can help me
0
 
LVL 84

Expert Comment

by:ozo
ID: 1254967
Is tv ever supposed to change in the loop?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1254968
como ozo dice, creo que el ultimo while(error>=1.0E-6) esta infinito, porque hay no assignment de error=
0
 

Expert Comment

by:bflVijay
ID: 1254969
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
 
LVL 10

Accepted Solution

by:
RONSLOW earned 150 total points
ID: 1254970
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now