D
dmoran21
I am a mathematician trying to write a program in C to do some curve
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code is
below.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
/* Integer declarations */
const int exitcode = -1;
float arraylength = 0;
float angfreq = 0;
float* time = (float*)NULL;
float* output = (float*)NULL;
float ysinesum = 0;
float ycossum = 0;
float sumy = 0;
float acoef = 0;
float bcoef = 0;
float ccoef = 0;
int count = 0;
/* Input section */
printf("How many pairs of data are available?");
scanf("%f", &arraylength);
if(arraylength<1) {
printf("Invalid array length\n");
exit(exitcode);
}
printf("Enter the angular frequency: ");
scanf("%f", &angfreq);
for(count = 0;count<=(arraylength-1);count++) {
printf("Enter the time for entry %d: ",count);
scanf("%f", &time[count]);
printf("Enter the output cooresponding to t = %d: ", count);
scanf("%f", &output[count]);
}
/* Calculation Section */
for(count = 0;count<=(arraylength-1);count++) {
sumy = sumy + output[count];
ysinesum = ysinesum + output[count]*sin(angfreq*time[count]);
ycossum = ycossum + output[count]*cos(angfreq*time[count]);
}
acoef = sumy / (arraylength - 1);
bcoef = (2/(arraylength - 1)) * ycossum;
ccoef = (2/(arraylength - 1)) * ysinesum;
/* Output Section */
printf("The equation of the sinusoid is: \n");
printf("%3.3f + %3.3f cos(%3.3f t) + %3.3f sin(%3.3f t)\n");
return 0;
}
Thanks,
Dave
fitting. When I get to the point where I attempt to enter data in my
arrays, I get a General Protection Exception error message. What is
this and how can I fix it. I am writing in Turbo C++. My source code is
below.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
/* Integer declarations */
const int exitcode = -1;
float arraylength = 0;
float angfreq = 0;
float* time = (float*)NULL;
float* output = (float*)NULL;
float ysinesum = 0;
float ycossum = 0;
float sumy = 0;
float acoef = 0;
float bcoef = 0;
float ccoef = 0;
int count = 0;
/* Input section */
printf("How many pairs of data are available?");
scanf("%f", &arraylength);
if(arraylength<1) {
printf("Invalid array length\n");
exit(exitcode);
}
printf("Enter the angular frequency: ");
scanf("%f", &angfreq);
for(count = 0;count<=(arraylength-1);count++) {
printf("Enter the time for entry %d: ",count);
scanf("%f", &time[count]);
printf("Enter the output cooresponding to t = %d: ", count);
scanf("%f", &output[count]);
}
/* Calculation Section */
for(count = 0;count<=(arraylength-1);count++) {
sumy = sumy + output[count];
ysinesum = ysinesum + output[count]*sin(angfreq*time[count]);
ycossum = ycossum + output[count]*cos(angfreq*time[count]);
}
acoef = sumy / (arraylength - 1);
bcoef = (2/(arraylength - 1)) * ycossum;
ccoef = (2/(arraylength - 1)) * ysinesum;
/* Output Section */
printf("The equation of the sinusoid is: \n");
printf("%3.3f + %3.3f cos(%3.3f t) + %3.3f sin(%3.3f t)\n");
return 0;
}
Thanks,
Dave