Solved

# arrays and file i/o

Posted on 1998-12-08
215 Views
/*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
Question by:pedro10
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2
• 2
• +5

Author Comment

ID: 1254959
Edited text of question
0

Author Comment

ID: 1254960
need help soon
0

LVL 1

Expert Comment

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

LVL 51

Expert Comment

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

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

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

LVL 84

Expert Comment

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

Author Comment

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

LVL 84

Expert Comment

ID: 1254967
Is tv ever supposed to change in the loop?
0

LVL 51

Expert Comment

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

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

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn hoâ€¦
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address.Â This address might be address of another variable/address of devices/address of fuâ€¦
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
###### Suggested Courses
Course of the Month3 days, 9 hours left to enroll