Google Answers Logo
View Question
 
Q: Basic C programming problem ( No Answer,   4 Comments )
Question  
Subject: Basic C programming problem
Category: Computers > Programming
Asked by: noggywoggy-ga
List Price: $5.00
Posted: 22 Nov 2002 13:04 PST
Expires: 23 Nov 2002 18:06 PST
Question ID: 112742
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);

}
Answer  
There is no answer at this time.

Comments  
Subject: Re: Basic C programming problem
From: arcsine99-ga on 22 Nov 2002 13:18 PST
 
temp_c is uninitialized.

In your for loop you fail to initialize "i" so:

array_c[i]=temp_c; is most likely failing.
Subject: Re: Basic C programming problem
From: arcsine99-ga on 22 Nov 2002 13:29 PST
 
Upon further review I see that you init. temp_c so it is
more likely that you fail latter is the problem.

array_c[i]=temp_c; is most likely failing.

Also, you don't increment "i" in the for loop. so each through you 
write to the same location. I am guessing you meant to do "i++" in

 // like this?
 for(temp_c=-100; temp_k<=200; temp=temp+5, i++) 

Anothter note -
You have limited the file_name to 21 characters so if type in 
a filename path that's longer than that you overwrite this buffer.
Subject: Re: Basic C programming problem
From: arcsine99-ga on 22 Nov 2002 14:58 PST
 
You must set this to zero:

int i = 0;

or you can initialize in the for loop:

for(i = 0, temp_c=-100; temp_c<=200; temp_c=temp_c+5, i++)
Subject: Re: Basic C programming problem
From: arcsine99-ga on 22 Nov 2002 15:02 PST
 
C and C++ don't guarantee that local variables are set to zero. When
you that int the way you have it is a random value from 0 to
0xffff_ffff.

As it's written you can is reads:

array_c[random number for 0 to 4GIG ]=temp_c;

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy