I'm working on a small project in MS Visual C++... after compiling the
following code and running the .exe produced, i get an error message
saying that "The insruction at 0x0040127a" reference memory at
"0x667961d4". The memory could not be "written"", and then asks me
whether I want to terminate or debug.
As I'm a complete novice (I wrote the code from bits of notes on C I
have lying around), I can't decipher the debugging information.
Could someone take a look at the code and tell me where I went wrong?
(Hopefully, not too much modification is necessary! Explanations of
any changes in laymans terms would be appreciated too)
TIA
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "mtfconst.h"
main()
{
/* decleration of variables */
double res_0; /* resistance of the wire at zero degrees */
double alpha; /* coefficient of resistivity alpha */
double beta; /* coefficient of resistivity beta */
double gamma; /* coefficient of resistivity gamma */
double temp_c; /* temperature in degrees celsius */
double temp_k; /* temperature in degrees kelvin */
double res_t; /* resistance of wire at given temperature */
int i; /* counter */
/* decleration of arrays */
double array_k[61]; /* list of temperatures in kelvin */
double array_c[61]; /* list of temperatures in celsius */
double array_r[61]; /* list of resistances in ohms */
/* reserve characters for filename */
char file_name[21]; /* name of output file */
FILE *ofp;
/* inputting variables into equation */
printf("What is the resistance of the wire at zero degrees
celsius?\n ");
scanf("%lf", &res_0);
printf("What is the value of temperature coeffecient alpha?\n ");
scanf("%lf", &alpha);
printf("What is the value of temperature coeffecient beta?\n ");
scanf("%lf", &beta);
printf("What is the value of temperature coefficient gamma?\n ");
scanf("%lf", &gamma);
/* convert degrees celsius into degrees kelvin */
temp_k=temp_c-T_ABS;
/* print confirmation on screen and name output file */
printf("What would you like to name the output file?\n ");
scanf("%s", file_name);
printf("Your inputted resistance of the wire at zero degrees is:
%12.3e ohms\n", res_0);
printf("Your inputted value for temperature coefficient alpha is:
%12.3e\n", alpha);
printf("Your inputted value for temperature coefficient beta is:
%12.3e\n", beta);
printf("Your inputted value for temperature coefficient gamma is:
%12.3e\n", gamma);
/* write to output file */
ofp=fopen(file_name, "w");
fprintf(ofp, "The inputted resistance of the wire at zero degrees
was: %12.3e ohms\n", res_0);
fprintf(ofp, "The inputted value for the temperature coefficient
alpha was: %12.3e\n", alpha);
fprintf(ofp, "The inputted value for the temperature coefficient
beta was: %12.3e\n", beta);
fprintf(ofp, "The inputted value for the temperature coefficient
gamma was: %12.3e\n", gamma);
fprintf(ofp, "Calculations are tabulated below");
fprintf(ofp, "Wire Temperature(degrees Celsius\t\t Wire
Temperature(degrees Kelvin)\t\tResistance of wire(ohms)\n");
for(temp_c=-100; temp_k<=200; temp=temp+5)
{
res_t=res_0*(1+(alpha*temp_c)+(beta*pow(temp_c,2))+(gamma*pow(temp_c,3)));
array_c[i]=temp_c;
array_k[i]=temp_k;
array_r[i]=res_0;
fprintf(ofp, "%12.3e\t\t,%12.3e\t\t,%12.3e\t\n",
array_c[i],array_k[i],array_r[i]);
}
fprintf(ofp, "\n\n End of results");
/* close output file */
fclose(ofp);
printf("The results have been saved in the active directory, under
filename: %s\n", file_name);
}
P.S. mtfconst.h is simply a header file to call the variable T_ABS,
which is defined as -273.15 degrees celsius. |
Clarification of Question by
noggywoggy-ga
on
22 Nov 2002 14:13 PST
The code above was actually my old version, I had included the "i++"
comment in the newer version, as shown below.
You are correct in saying that the array you mentioned is the one that
fails... that's the line that the debugger takes me to - can you
explain a bit further why this is?
Thanks!
#include "stdafx.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "mtfconst.h"
int main()
{
/* decleration of variables */
double res_0; /* resistance of the wire at zero degrees */
double alpha; /* coefficient of resistivity alpha */
double beta; /* coefficient of resistivity beta */
double gamma; /* coefficient of resistivity gamma */
double temp_c; /* temperature in degrees celsius */
double temp_k; /* temperature in degrees kelvin */
double res_t; /* resistance of wire at given temperature */
int i; /* counter */
/* decleration of arrays */
double array_k[61]; /* list of temperatures in kelvin */
double array_c[61]; /* list of temperatures in celsius */
double array_r[61]; /* list of resistances in ohms */
/* reserve characters for filename */
char file_name[21]; /* name of output file */
FILE *ofp;
/* inputting variables into equation */
printf("Welcome to my program for calculating the resistance of a
wire at temperatures between -100 and 200 degrees celsius\n\n");
printf("What is the resistance of the wire at zero degrees celsius?
");
scanf("%lf", &res_0);
printf("What is the value of temperature coeffecient alpha? ");
scanf("%lf", &alpha);
printf("What is the value of temperature coeffecient beta? ");
scanf("%lf", &beta);
printf("What is the value of temperature coefficient gamma? ");
scanf("%lf", &gamma);
/* convert degrees celsius into degrees kelvin */
temp_k=temp_c-T_ABS;
/* print confirmation on screen and name output file */
printf("What would you like to name the output file?\n ");
scanf("%s", file_name);
printf("Your inputted resistance of the wire at zero degrees is:
%7.2e ohms\n", res_0);
printf("Your inputted value for temperature coefficient alpha is:
%7.2e\n", alpha);
printf("Your inputted value for temperature coefficient beta is:
%7.2e\n", beta);
printf("Your inputted value for temperature coefficient gamma is:
%7.2e\n", gamma);
/* write to output file */
ofp=fopen(file_name, "w");
fprintf(ofp, "The inputted resistance of the wire at zero degrees
was: %12.3e ohms\n", res_0);
fprintf(ofp, "The inputted value for the temperature coefficient
alpha was: %12.3e\n", alpha);
fprintf(ofp, "The inputted value for the temperature coefficient
beta was: %12.3e\n", beta);
fprintf(ofp, "The inputted value for the temperature coefficient
gamma was: %12.3e\n", gamma);
fprintf(ofp, "Calculations are tabulated below");
fprintf(ofp, "Wire Temperature(degrees Celsius\t\t Wire
Temperature(degrees Kelvin)\t\tResistance of wire(ohms)\n");
for(temp_c=-100; temp_c<=200; temp_c=temp_c+5, i++)
{
res_t=res_0*(1+(alpha*temp_c)+(beta*pow(temp_c,2))+(gamma*pow(temp_c,3)));
array_c[i]=temp_c;
array_k[i]=temp_k;
array_r[i]=res_0;
fprintf(ofp, "%12.3e\t\t,%12.3e\t\t,%12.3e\t\n",
array_c[i],array_k[i],array_r[i]);
}
fprintf(ofp, "\n\n End of results");
/* close output file */
fclose(ofp);
printf("The results have been saved in the active directory, under
filename: %s\n", file_name);
}
|