# Thread: Determining 2nd order ODE

1. ## Determining 2nd order ODE

I'm having trouble identifying 1st and 2nd order ODEs. The file cp1_ODEin.txt contains the following:

-3xy' - ay'' - bcos(t) = sin(t)

However, the program identifies it as a 1st order ODE. Can anyone help?

Code:
```main()
{   int count = 0, i = 0;
char z[count + 1];
FILE *fin, *fout;
fin = fopen("cp1_ODEin.txt", "r");
fout = fopen("cp1_ODEout.txt", "w");

while(fscanf(fin, "%c", &z) != EOF){
count = count++;
fgets(z, count + 1, fin);
}
for( i = 0 ; i <= count ; i++ ){
if( (z[i] == '\'') && (z[i+1] == '\'') ){
fprintf(fout, "The above ODE is second order.\n");
printf("The above ODE is second order.\n");
}
else{
fprintf(fout, "The above ODE is first order.\n");
printf("The above ODE is first order.\n");
}
}
}``` 2. There's several problems with your code...

Code:
```int count = 0, i = 0;
char z[count + 1];```
This is the same as declaring char z, because count is 0 at the moment you declare z. And judging from your use of fgets and how you are indexing z further below, you want the z array to be of a much bigger size than just one element.

Code:
```fin = fopen("cp1_ODEin.txt", "r");
fout = fopen("cp1_ODEout.txt", "w");```
Not good because you move on to using fin and fout a few lines below this without ever checking if the files were actually opened. In case of failure, fin or fout will be null pointers and you'll get into trouble trying to use them. Instead you should guard against failure to open a file. And since opening both files seems essential for this code to work, you probably want just to abort in case of failure.:

Code:
```#include <stdlib.h>

if ( (fin = fopen("cp1_ODEin.txt", "r")) == NULL ) {
fprintf(stderr, "Can't open file for reading. Aborting...\n");
exit(EXIT_FAILURE);  // or return 1, on which case you don't need <stdlib.h>
}
if ( (fout = fopen("cp1_ODEout.txt", "w")) == NULL ) {
fclose(fin);       // at this stage fin was opened. needs to be closed. see also notes below.
fprintf(stderr, "Can't open file for writing. Aborting...\n");
exit(EXIT_FAILURE);  // or return 1, on which case you don't need <stdlib.h>
}```
Next,

Code:
`count = count++;`
Not only is this unnecessary because count++ is the same as count = count + 1, so the assignment is superfluous, but more importantly it is undefined (and unspecified). C doesn't guarantee that the variable count will be incremented only after the assignment takes place. If count was equal to 1 before that statement, count can be 1 or 2 after it. There's no guarantee which. If you need to increment count just write count++; or count = count + 1;

Finally,

Code:
`}  // line 22`
Not good. You are not closing the opened files, potentially leaking these two resources. Always close files that you have opened.

Code:
```fclose(fin);
fclose(fout);```
Even better than this is to check if the files were actually closed. There's very little you can do if for some reason you are not able to dispose of a resource opened by the same process that created it. But at least you can leave a message on the screen, since this is something your user should know about.

Code:
```int disposed = fclose(fin) + fclose(fout);  // fclose returns an int. 0 if successfully closed.
if (disposed != 0) {
fprintf(stderr, "error: could not close all files on exit.\n");
exit(EXIT_FAILURE);  // or return 2, on which case you don't need <stdlib.h>
}``` 3. Here is my revised code. However, my output is still multiple lines of " The above ODE is first order." The output should only be one line of "The above ODE is second order."

Code:
```main()
{	int i = 0, count = 0;
char z;
FILE *fin, *fout;
fin = fopen("cp1_ODEin.txt", "r");
fout = fopen("cp1_ODEout.txt", "w");

while(fscanf(fin, "%c", &z) != EOF){
count = count + 1;
}
for( i = 0 ; i <= count ; i++ ){
if( (z[i] == '\'') && (z[i+1] == '\'') ){
fprintf(fout, "The above ODE is second order.\n");
printf("The above ODE is second order.\n");
}
else{
fprintf(fout, "The above ODE is first order.\n");
printf("The above ODE is first order.\n");
}
}
fclose(fin);
fclose(fout);
}``` 4. Thank you. I implemented some changes, but still have the same problem. 5. Where do you store what's in file to your z buffer? Hint - explain extensively what line 8 does.
What happens if there's more than 1000 characters in the file?
What happens if all 999 or 1000 characters in z are utilized and you get to line 12?

Explain your logic of how the program is supposed to check what it's supposed to check in your own words.

For now what you have is:
- loop through characters written to buffer, plus one beyond:
- if current character is \' and next character (possibly outside the set of characters written to the buffer, or even outside the buffer) is also \', then print message
- else print second message
(continue looping) Popular pages Recent additions 2nd, char, count, ode, order 