Thread: seismic events program

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    26

    seismic events program

    Sorry about the length of this!

    This is a program that I was asked to write. I have the code that I have done so far below this. When I run it I get no errors, but I also don’t get any screen asking me to input values (see program for more info). Please take a look at my code someone and help me with it if possible. Thank you in advance everyone! Here is the ‘brief’ I was given for the program:


    Write pseudocode, draw flowcharts and write C code for a program that reads seismometer data from a data (text) file named seismic.dat, determines whether seismic events have occurred and reports the findings to the screen in text and graphic format.

    The first line of the file contains two values: the number of data elements or readings and the time interval in seconds that occurred between consecutive measurements. The time interval is a floating point value and it may be assumed that all the measurements were taken with the same time interval between them. After reading the data measurements the program should identify possible earthquakes or seismic events using a power ratio.

    The data window length (number of measurements) for short-time power and long-time power are to be read from the keyboard as in the threshold value.

    The inputs to this program are:
    the data (text) file named seismic.dat, the number of measurements to use for short time power and long time power, and the threshold value.

    The outputs are:
    A screen report giving the times of potential seismic events the corresponding short-time to long-time power ratio and threshold employed;
    And a data file containing the times of potential seismic events, the corresponding short-time to long-time power ratio and threshold employed

    Here is my code so far:

    #include <stdio.h>
    #include <stdlib.h>
    #define FILENAME "seismic.dat"
    #define MAX_SIZE 1000
    #define THRESHOLD 1.5

    main()
    {

    /* Declare variables and function prototypes. */
    int k, npts, short_window, long_window;
    double sensor[MAX_SIZE], time_incr, short_power,
    long_power, ratio;
    FILE *file_ptr;
    double power_w(double x[], int length, int n);

    /* Read data file. */
    file_ptr = fopen(FILENAME,"r");
    fscanf(file_ptr,"%i %lf",&npts,&time_incr);
    if (npts > MAX_SIZE)
    {
    printf("Data file too large for array. \n");
    return EXIT_FAILURE;
    }
    else
    {
    /* Read data into an array. */
    for (k=0; k<=npts-1; k++)
    fscanf(file_ptr,"%lf",&sensor[k]);
    }

    /* Read window sizes from the keyboard. */
    printf("Enter number of points for short-window: \n");
    scanf("%i",&short_window);
    printf("Enter number of points for long-window: \n");
    scanf("%i",&long_window);

    /* Compute power ratios and search for events. */
    for (k=long_window-1; k<=npts-1; k++)
    {
    short_power = power_w(sensor,short_window,k);
    long_power = power_w(sensor,long_window,k);
    ratio = short_power/long_power;
    if (ratio > THRESHOLD)
    printf("Possible event at %f seconds \n",
    time_incr*k);
    }

    /* Close file and exit program. */
    fclose(file_ptr);
    return EXIT_SUCCESS;
    }
    /*--------------------------------------------------------*/
    /* This function computes the average power in a */
    /* specified window of a double array. */

    double power_w(double x[], int length, int n)
    {
    /* Declare and initialize variables. */
    int k;
    double xsquare=0;

    /* Compute sum of values squared in the array x. */
    for (k=n; k>=n-length+1; k--)
    {
    xsquare += x[k]*x[k];
    }

    /* Return the average squared value. */
    return xsquare/length;
    }
    /*--------------------------------------------------------*/




    Code:
    #include <stdio.h>
    1+1 = 0
    if you think that's wrong then you shouldn't be here!!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Ok, now put the code tags around your whole code...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    26
    im not quite sure how to do that or what you mean. how will that affect reading the code?

    Code:
    #include <stdio.h>
    #include <stdio.h>
    #include <stdlib.h>
    #define FILENAME "seismic.dat"
    #define MAX_SIZE 1000
    #define THRESHOLD 1.5
    
    main()
    {
    
    /* Declare variables and function prototypes. */
    int k, npts, short_window, long_window;
    double sensor[MAX_SIZE], time_incr, short_power,
    long_power, ratio;
    FILE *file_ptr;
    double power_w(double x[], int length, int n);
    
    /* Read data file. */
    file_ptr = fopen(FILENAME,"r");
    fscanf(file_ptr,"%i %lf",&npts,&time_incr);
    if (npts > MAX_SIZE)
    {
    printf("Data file too large for array. \n");
    return EXIT_FAILURE;
    }
    else
    {
    /* Read data into an array. */
    for (k=0; k<=npts-1; k++)
    fscanf(file_ptr,"%lf",&sensor[k]);
    }
    
    /* Read window sizes from the keyboard. */
    printf("Enter number of points for short-window: \n");
    scanf("%i",&short_window);
    printf("Enter number of points for long-window: \n");
    scanf("%i",&long_window);
    
    /* Compute power ratios and search for events. */
    for (k=long_window-1; k<=npts-1; k++)
    {
    short_power = power_w(sensor,short_window,k);
    long_power = power_w(sensor,long_window,k);
    ratio = short_power/long_power;
    if (ratio > THRESHOLD)
    printf("Possible event at %f seconds \n",
    time_incr*k);
    }
    
    /* Close file and exit program. */
    fclose(file_ptr);
    return EXIT_SUCCESS;
    }
    /*--------------------------------------------------------*/
    /* This function computes the average power in a */
    /* specified window of a double array. */
    
    double power_w(double x[], int length, int n)
    {
    /* Declare and initialize variables. */
    int k;
    double xsquare=0;
    
    /* Compute sum of values squared in the array x. */
    for (k=n; k>=n-length+1; k--)
    {
    xsquare += x[k]*x[k];
    }
    
    /* Return the average squared value. */
    return xsquare/length;
    }
    /*--------------------------------------------------------*/
    1+1 = 0
    if you think that's wrong then you shouldn't be here!!!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I would say that it would fix the indentation of the code, but in your case I see that was a forlorn hope
    You just pasted your previous post (with the formatting already broken), rather than recopying from your text editor.

    > how will that affect reading the code?
    Well it shows you care - enough to have read the posting guidelines, and enough to make your code as presentable as possible to anyone who might help.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    26
    is this the correct way now?


    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    #define FILENAME "seismic.dat"
    #define MAX_SIZE 1000
    #define THRESHOLD 1.5
    
    main()
    {
       printf("Hello there");
       /*  Declare variables and function prototypes.  */
       int k, npts, short_window, long_window;
       double sensor[MAX_SIZE], time_incr, short_power,
    	  long_power, ratio;
       FILE *file_ptr;
       double power_w(double x[], int length, int n);
    
       /*  Read data file.  */
       file_ptr = fopen(FILENAME,"r");
       fscanf(file_ptr,"%i %lf",&npts,&time_incr);
       if (npts > MAX_SIZE)
       {
          printf("Data file too large for array. \n");
          return EXIT_FAILURE;
       }
       else
       {
          /*  Read data into an array.  */
          for (k=0; k<=npts-1; k++)
    	 fscanf(file_ptr,"%lf",&sensor[k]);
    	}
    	printf("Hello there");
       /*  Read window sizes from the keyboard.  */
       printf("Enter number of points for short-window: \n");
       scanf("%i",&short_window);
       printf("Enter number of points for long-window: \n");
       scanf("%i",&long_window);
    
       /*  Compute power ratios and search for events. */
       for (k=long_window-1; k<=npts-1; k++)
       {
          short_power = power_w(sensor,short_window,k);
          long_power = power_w(sensor,long_window,k);
          ratio = short_power/long_power;
          if (ratio > THRESHOLD)
    	 printf("Possible event at %f seconds \n",
    		time_incr*k);
       }
    
       /*  Close file and exit program.  */
       fclose(file_ptr);
       return EXIT_SUCCESS;
    }
    /*--------------------------------------------------------*/
    /*  This function computes the average power in a         */
    /*  specified window of a double array.                   */
    
     double power_w(double x[], int length, int n)
     {
        /*  Declare and initialize variables.  */
        int k;
        double xsquare=0;
    
        /*  Compute sum of values squared in the array x.  */
        for (k=n; k>=n-length+1; k--)
        {
           xsquare += x[k]*x[k];
        }
    
        /*  Return the average squared value.  */
        return xsquare/length;
     }
     /*--------------------------------------------------------*/

    if it is still wrong then i'm sorry. im just not sure what way to present, that thing doesn't make sense. when i click on insert source code i click copy and paste but only the first line appears.
    1+1 = 0
    if you think that's wrong then you shouldn't be here!!!

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    26
    so can you please help me to understand what is wrong the code? i have looked on the internet at tutorials but to my dismay have not found a solution. any help is gratefully received.

    i have clearly been having problems with how to layout the code in my posts, therefore i have added it as an attachment. hope this helps. ho ho ho
    1+1 = 0
    if you think that's wrong then you shouldn't be here!!!

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Sometimes I wonder how someone could get this far in a program, and yet still not understand how to get some user input...

    ~/

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Your code worked fine for me (the attached version). A couple of tips:
    - the initial printf() in main() is technically illegal in C (becuase its before the declarations)
    - main() returns int - leaving off the "int" is old-school
    - use spaces instead of tabs in your source files, it'll post better next time

    gg

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > is this the correct way now?
    Yes, much better now

    > main()
    Whilst this implies int main, you should really get into the habit of being explicit with your function return types.
    Such implicit declarations will be removed from C at some point.

    > printf("Hello there");
    > /* Declare variables and function prototypes. */
    > int k, npts, short_window, long_window;
    The first printf() is in C++, not C
    C does not allow statements to precede variable declarations. Apart from that, the rest looks OK

    > #define FILENAME "seismic.dat"
    > #define THRESHOLD 1.5
    This might be OK for testing, but your original post said
    "The inputs to this program are:... and the threshold value."

    > for (k=0; k<=npts-1; k++)
    The usual style for a loop such as this is
    for (k=0; k<npts; k++)


    > but I also don’t get any screen asking me to input values
    Mmm, seems to work OK here
    Code:
    $ ./a.out
    Enter number of points for short-window:
    2
    Enter number of points for long-window:
    3
    Doesn't produce any output, but then again, I don't have a representative seismic.dat file.
    That said, I can't see anything immediately wrong on the input side of things.

    Which operating system / compiler are you using?

    Heh, taking a guess here, you're on unix/linux and you called your program 'test'
    test is a standard unix program which does something completely different.

    Try typing
    ./test
    to get the one in your current directory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    HAHA, getting burned by "test" is an important milestone in a programmers career.

    gg

  11. #11
    Registered User
    Join Date
    Nov 2003
    Posts
    26
    hi guys thanks for your help i'm going to try what you suggested. its only my 2nd year of programming and im not the best. im including the seismice.dat file for everyone so they can try it out. would someone please try it and tell me if they get the correct outputs? thank you all HO HO HO actually i ave just realised that i can't send .dat files as attachments. is it okay for me to e-mail it to people or is that against the forum rules? if i can then please let me know if that is okay with the person. thank you

    ps. do i need to have seismic.dat in the same folder as my rub.c program?


    kermit, please explain your problem with the user input, i don't quite understand your comment.

    i am using windows xp. and i am using turbo c++

    no i'm not on unix/linux and my program is called rub, so nothing to do with the test thing mentioned! okay codeplug lol
    thank you
    1+1 = 0
    if you think that's wrong then you shouldn't be here!!!

  12. #12
    Registered User
    Join Date
    Nov 2003
    Posts
    26
    so can anyone help me with my previous post. thank you
    1+1 = 0
    if you think that's wrong then you shouldn't be here!!!

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > ps. do i need to have seismic.dat in the same folder as my rub.c program?
    It needs to be in the same place as your executable

    Which leads me to suspect that it's taking the error route out of the program (which doesn't ask for input)

    Since you seem to be on windows, my guess is the window is opening and closing pretty darn quickly.
    This being the case, check the FAQ


    > i am using windows xp. and i am using turbo c++
    I just don't get it.
    Why oh why does everyone seem to be driving around in ferrari's powered by steam engines?
    Get a better compiler, one for this millenium not the last.

    > actually i ave just realised that i can't send .dat files as attachments
    Well try a ZIP file then
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Nov 2003
    Posts
    26
    hi i have included seismic.dat in a zip file. im not too familiar with zip files so i hope i have done it right, any help is appreciated
    1+1 = 0
    if you think that's wrong then you shouldn't be here!!!

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So did you address any remaining issues?

    For a start, I notice your ZIP file contains SEISMIC.DAT, and your program reads seismic.dat.

    Have you checked with your tutor / whoever as to what good answers to the questions are

    Namely
    Enter number of points for short-window:
    Enter number of points for long-window:

    Basically, if you're not given a complete set of inputs and a specimen answer, its gonna be hard for us to replicate.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM