Please help - Run time error
Hi i was wondering if some could help me quick with the run time error I have. I am quite new to programming and I have the course work that has to be in asap. Im sure my code is ok but im getting a run time error?
code:
Code:
#include <stdio.h>
#include <math.h>
#include <string.h>
float dx_dt(float x,float y); //function declaration
float dy_dt(float x,float y,float z);
float dz_dt(float x,float y,float z);
int n=1,p=1,o=1;
int i,day,month,option_file,option_1,option_2,z0;
float a,b,c,t,x,y,z,step;
float delta_t;
float k1=0,k2=0,l1=0,l2=0,m1=0,m2=0; //initalising values
char fname[20], again;
int m_num_days[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; //month array
int midx; //month index
int main() {
again='Y'; //Initialise loop to repeat
while (again == 'Y'){ //programe
n=1;
printf("******************** THE UNIVERSITY OF NOTTINGHAM ********************\n");
printf("******* NUMERICAL CALCULATION SOLUTIONS TO THE LORENZ EQUATION *******\n");
printf("****************** VIA THE IMPROVED EULER METHOD *********************\n");
printf("************************ by Craig Barker *****************************\n");
FILE*POINT; //Declaring a file pointer
while (n==1) //Loop so values can be changed
{
printf("\nPlease input the following values:\n\n"); //Vaules inputted are
printf("\nPantal Number:"); //scanned in
scanf("%f", &a);
printf("\nReynolds Number:");
scanf("%f", &b);
printf("\nBox Ratio:");
scanf("%f", &c);
printf("\nz value:");
scanf("%d", &z0);
printf("\nTime Peroid (0<t100):");
scanf("%f", &t);
while (t<=0||t>99) //Error check that time period
{ //is in its limits
printf ("INVALID - Please re-input Time Period (0<t<100)");
scanf ("%f", &t);
}
month=0;
while((month < 1) || (month > 12)) //loop to make sure month
{ //is in limits
printf("\nMonth of your birth:");
scanf("%d", &month);
midx = (int)month;
}
day=0; //checks day to be valid within
//selected month via an array
while((day < 1) || (day > m_num_days[midx-1]))
{
printf("\nDay of your birth [1-%d]:",m_num_days[midx-1]);
scanf("%d", &day);
day = (int)day;
}
printf("\nStep Size:");
scanf("%f", &step);
printf("\n\nBelow are your chosen values"); //prints values to screen
printf("\n Prantl Number = %f",a);
printf("\n Reynold's Number = %f",b);
printf("\n Box Ratio = %f",c);
printf("\n z0 = %d",z0);
printf("\n Time Period = %f",t);
printf("\n Day = %d",day);
printf("\n Year = %d",month);
printf("\n Step Size = %f",step);
o=1;
while (o==1) //loop so values can be changed
{
printf("\n\nWould you like to change these numbers?");
printf("\n [1] - YES");
printf("\n [2] - NO");
printf("\n\n Choice =");
scanf ("%d", &option_1);
switch (option_1) //Change values menu
{
case 1: //Loop back to the top
n=1;
o=2;
break;
case 2: //breaks out of loop and
n=2; //carries on
o=2;
break;
default: //invalid entry
n=2;
o=1;
printf ("\n\nINVALID - Please choose again");
}
}
}
printf ("\n\n[1] - USE AN EXISTING FILE\n"); //Where would the file like to
printf ("[2] - CREATE A NEW FILE\n"); //be saved? menu
printf("\n\n Choice =");
scanf ("%d",&option_file);
switch (option_file)
{
case 1: //asks for a valid filename,
printf ("\nPlease insert the existing file name"); //max 15 char
printf ("\n\nFile Name:");
scanf ("%s", fname);
POINT=fopen(fname, "a"); //creates the file
break;
case 2: //asks for a valid file name
printf ("please enter the new file name"); //max 15 char
printf ("\n\nFile Name:");
scanf("%s", fname);
POINT=fopen(fname,"a"); //creates the file
break;
default: //invalid entry
printf("\nplease try again");
}
while (p==1) //confirm you would like to
{ //write values to the file
printf ("\nWould you like to write the data inputted to the the chosen file");
printf ("\n[1] - Yes");
printf ("\n[2] - No");
printf("\n\n Choice =");
scanf ("%d", &option_2);
getc(stdin); //debug scanf
switch (option_2)
{
case 1: //writes values to file
fprintf (POINT,"\n\nPrantal Number = %f", a);
fprintf (POINT,"\nReynolds Number = %f", b);
fprintf (POINT,"\nBox Ratio = %f", c);
fprintf (POINT,"\nz = %d", z0);
fprintf (POINT,"\nTime Period = %f", t);
fprintf (POINT,"\nmonth = %d", month);
fprintf (POINT,"\nDay = %d\n", day);
p=2;
break;
case 2: //does not write values to file
printf ("\nNo data has been written to the file!");
p=2;
break;
default: //invalid entry
p=1;
printf ("\n\nINVALID - Please try again!");
}
}
x=day; //sets variables
y=month;
z=z0;
delta_t=(t/step); //calcs step size
fprintf(POINT,"t,x,y,z\n"); //writes headers to file
for(i=0; i<=step; i++) //loop to keep inputting
{ //new vaiables.
k1 = delta_t*dx_dt(x,y); //Receives value from function
k2 = delta_t*dx_dt(x+k1,y); // and calulates k,l and m values
l1 = delta_t*dy_dt(x,y,z);
l2 = delta_t*dy_dt(x,y+l1,z);
m1 = delta_t*dz_dt(x,y,z) ;
m2 = delta_t*dz_dt(x,y,z+m1);
x=x+(0.5*(k1+k2)); //Improve value of x
y=y+(0.5*(l1+l2)); //Improve value of y
z=z+(0.5*(m1+m2)); //Improve value of z
fprintf(POINT,"%d,%f,%f,%f\n",i,x,y,z);//write the calculated values
}
fclose(POINT); //closes the file
printf ("\n\nDATA TRANSFER COMPLETE TO %s",fname); //confirm tranfer
printf ("\n\nWould you like to start the programe again? Y/N:");
scanf("%c",&again); //option to run program again
getc(stdin);
}
return 0;
}
float dx_dt() //functions to calculate dxdt
{ return (a*(y-x));} //dydt and dzdt
float dy_dt()
{ return ((x*(b-z))-y);}
float dz_dt()
{ return ((x*y)-(c*z));}
Thanks in adavnce for the quick replies it going to to get me out of a creek.
Craig