Thread: Determining 2nd order ODE

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    16

    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. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Angola
    Posts
    8,445
    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[1], 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>
    }
    Last edited by Mario F.; 10-07-2016 at 03:08 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Jun 2016
    Posts
    16
    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[1000];
    	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. #4
    Registered User
    Join Date
    Jun 2016
    Posts
    16
    Thank you. I implemented some changes, but still have the same problem.

  5. #5
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    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 subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-01-2011, 04:13 PM
  2. Determining order of magnitude of user inputed number?
    By alizzle in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2010, 10:48 PM
  3. Replies: 4
    Last Post: 03-06-2008, 03:38 PM
  4. Determining order from a list of integers...
    By AngKar in forum C Programming
    Replies: 12
    Last Post: 04-23-2006, 03:53 PM
  5. what is the significance of low order and high order bits
    By Shadow12345 in forum Windows Programming
    Replies: 1
    Last Post: 11-16-2002, 11:46 AM

Tags for this Thread