Monitoring variables with regard to time

This is a discussion on Monitoring variables with regard to time within the C Programming forums, part of the General Programming Boards category; Hi there, I'm writing a C program to simulate a software system that would be used to monitor the movement ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    17

    Monitoring variables with regard to time

    Hi there,

    I'm writing a C program to simulate a software system that would be used to monitor the movement of shipping.

    I am required to write a program that simulates a passage of time and calculates whether or not a "risk of collision" exists between any ships and whether or not any ships actually collide. If ships do collide, I will assume that they sink without a trace and need to have features to record that in my data structures. Once a ship has sunk, it cannot collide with anything again.

    Currently, my program prompts the user for the name of a file which will contain the shipping data for the area I am concerned with; it then opens the file, reads in all of the data and stores it in a couple of data structures.

    My program then prompts the user for a "duration", that being the length of time in hours that my program should then attempt to simulate. It then prompts the user for a "time step", that being the length of time in minutes that my program should move forward at each step of its simulation.

    It should then run, with time moving forward, and at each step, should evaluate the new position of all of the ships and whether or not any significant events occur, i.e.

    a risk of collision
    an actual collision
    a ship sails out of the area I'm monitoring
    a ship sails into the area I'm monitoring

    Appropriate output should be sent to "standard output" to show the current state of simulation and any significant events.


    I have got as far as reading all of the data in from the text file specified by the user, and prompting the user for a "duration" and "time step", but I'm not sure how to then "run it", with time moving forward, and evaluate the new position of all of the ships at each step, and whether or not any significant events occur.

    This is what I have so far:

    Code:
    /* 
     * File:   main.c
     * Author: eef8
     *
     * Created on 06 December 2011, 15:53
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "structs.h"
    #include "navigation.h"
    
    /*
     * 
     */
    extern int errno;
    
    /* Here, I need to create the linked list, so that the data for each ship
       is stored in a new instance of the array. */
    typedef struct shipInfo item;
    
    int main(int argc, char** argv) {
        
        char text[500]; /* Create a character array that will store all of the text in the file */
        char line[100]; /* Create a character array to store each line individually */
    
        dateTime *dates = (dateTime *) malloc (6 * sizeof (int));
        
        
        FILE *file; /* Create a pointer to the file which will be loaded, to allow access to it */
        char fileName[30]; /* Create a character array to store the name of the file the user want to load */
            
        
        printf("Enter the name of the file containing ship information: ");
        scanf("%s", fileName);
        
        /*Try to open the file specified by the user. Use error handling if file cannot be found*/
        file = fopen(fileName, "r"); /* Open the file specified by the user in 'read' mode*/
        char lineBuffer[100];
        
        if(file == NULL){
            perror("The following error occurred: ");
            printf("Value of errno: %d\n", errno);
        }
        else {
            
            
            /* Read in the date and time of the ship information */
            dates = calloc(1, sizeof(dateTime));
            fscanf(file, "%d %d %d %d %d %d", &dates->day, &dates->month, &dates->year, &dates->hour, &dates->minute, &dates->second);
                   
            printf("File loaded for %d/%d/%d\n", dates->day, dates->month, dates->year); 
                                 /* Display a message to let the user know 
                                  * that the file has been loaded properly. 
                                  * Need to use fscanf to read the information in from the file */
            
            item *current, *head;
            int i;
            
            head = NULL;
            while(!feof(file)){    /* Will need to give i some proper value (not 10)
                                     * that will be correct depending of the number of 
                                     * ships in a file- maybe read how many lines there are
                                     * in the file, to find out how many ships there will be */           
                
                current = (item *)malloc(sizeof(item));
                
                fscanf(file, "%s %f %f %f %f\n", &current->AISID, &current->latitude, &current->longitude, &current->direction, &current->speed);
                
                current->nextShip = head;
                head = current;
            }
            
          current = head;
            
          /* Print out the contents of the array for each ship, to check that the
             data is being stored correctly */
            while(current!=NULL){
                printf("%s\n%f\n%f\n%f\n%f\n", current->AISID, current->latitude, current->longitude, current->direction, current->speed);
                current = current->nextShip;
            }
          
          /*Now ask the user to provide a duration, for which the program should simulate*/
          float duration, timeStep;
          printf("Please enter the duration in hours of time for which you would like to simulate: ");
          scanf("%f", &duration);
          
          printf("Please enter the 'time step' in minutes that the program should move forward in each step of the simulation: ");
          scanf("%f", &timeStep);
            
            fclose(file);
     //   return 0;
        }
        
        
        free(dates);
        dates = NULL;
        
        /*free(ships);
        ships = NULL;*/
        
        return (EXIT_SUCCESS);
    }
    /* Here, use the 'great_circle' method in navigation.h to calculate the distance between each of
     *  the ships in the file. If any of the ships are within 2 miles of each other AND are on courses
     *  that overlap at anytime in the future, a 'risk of collision' is deemed to exist*/
    int riskOfCollision(location ship1, location ship2){
        if((great_circle(ship1, ship2)<=2) &&(/*Need code here to determine whether or not ships' 
                                               * courses overlap at all here*/)){
            
        }
        
    }
    I'm currently trying to implement a method to determine whether or not a 'risk of collision' occurs- this would happen if any two ships' courses overlap at all, but I'm not sure how I would work that out...

    If someone could point me in the right direction, I would be very grateful.

    Thanks in advance.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by someone2088 View Post
    I have got as far as reading all of the data in from the text file specified by the user, and prompting the user for a "duration" and "time step", but I'm not sure how to then "run it", with time moving forward, and evaluate the new position of all of the ships at each step, and whether or not any significant events occur.
    You move it forward in time by calculating the locations of each ship in a loop... the loop keeps running for the specified duration, but not in real time... in computer time where loop iterations depicting minutes or seconds go by in a flash...


    I'm currently trying to implement a method to determine whether or not a 'risk of collision' occurs- this would happen if any two ships' courses overlap at all, but I'm not sure how I would work that out...

    If someone could point me in the right direction, I would be very grateful.

    Thanks in advance.
    It's not enough that the courses overlap... that should be no problem as long as 2 ships don't end up in the same place at the same time. So your calculations need to take bearing and speed into account, calculate the ship's locations at given moments... if two of them wind up in the same place at the same time, you've got a problem...

    Looking at your current code, you've already got the problem that your ship data is all going into one struct... you're going to need to keep data for each ship separately so you'll need an array of structs...
    Last edited by CommonTater; 12-13-2011 at 04:14 PM.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    17
    It's not enough that the courses overlap... that should be no problem as long as 2 ships don't end up in the same place at the same time. So your calculations need to take bearing and speed into account, calculate the ship's locations at given moments... if two of them wind up in the same place at the same time, you've got a problem...
    Sorry, I forgot to mention in my first post- there have been some simplifications made for the purposes of this assignment- these simplifications are:

    • All ships are considered to be of size zero
    • A "risk of collision" will be deemed to exist if two ships are within 2 miles of each other and are on courses that will overlap at any time in the future
    • Ships will be considered to have collided if the distance between them is less than 0.25 of a nautical mile

    Looking at your current code, you've already got the problem that your ship data is all going into one struct... you're going to need to keep data for each ship separately so you'll need an array of structs...
    As I understand, I have already declared an array of 'shipInfo' structs above my main method in the line:

    Code:
    typedef struct shipInfo item;
    and then created pointers to the array of structs with the line:

    Code:
    item *current, *head;
    You move it forward in time by calculating the locations of each ship in a loop... the loop keeps running for the specified duration, but not in real time... in computer time where loop iterations depicting minutes or seconds go by in a flash...
    So would this be similar to where I have printed out the contents of the array for each ship? i.e.

    Code:
    while(current!=NULL){             printf("%s\n%f\n%f\n%f\n%f\n", current->AISID, current->latitude, current->longitude, current->direction, current->speed);             current = current->nextShip;         }
    I have each ship there, in a separate structure- I'm just not sure what to do with each one to simulate their movement...

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by someone2088 View Post
    Sorry, I forgot to mention in my first post- there have been some simplifications made for the purposes of this assignment- these simplifications are:

    • All ships are considered to be of size zero
    • A "risk of collision" will be deemed to exist if two ships are within 2 miles of each other and are on courses that will overlap at any time in the future
    • Ships will be considered to have collided if the distance between them is less than 0.25 of a nautical mile



    As I understand, I have already declared an array of 'shipInfo' structs above my main method in the line:

    Code:
    typedef struct shipInfo item;
    and then created pointers to the array of structs with the line:

    Code:
    item *current, *head;


    So would this be similar to where I have printed out the contents of the array for each ship? i.e.

    Code:
    while(current!=NULL){             printf("%s\n%f\n%f\n%f\n%f\n", current->AISID, current->latitude, current->longitude, current->direction, current->speed);             current = current->nextShip;         }
    I have each ship there, in a separate structure- I'm just not sure what to do with each one to simulate their movement...
    Have you verified that all data is loading and being correctly assigned in your linked list?

    If so, then your next step is to calculate locations based on the speed and direction data for each ship...
    1) Start with everything where it is at time index 0,
    2) Move the index forward by 1 step... lets say a minute...
    3) Recalculate the location of each ship
    4) Test for collissions
    5) test if you've reached the end of the interval and exit if true
    5) loop to step 2

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    17
    Sorry, maybe I should I have given an example of the format of text file that the programming will be working with- it will always be in the format described below:

    13 11 2011 13 04 00
    GW1927 52.408 -4.117 1.000 0.000
    GS452 51.750 -4.300 5.000 10.000
    EI597 52.100 -6.000 90.000 12.000
    EI600 52.000 -5.900 10.000 15.000
    As far as I can tell, all data is loading and being correctly assigned in my linked list... for example, if I comment out the 'riskOfCollision' method that I've started writing beneath the main, to get the code to compile, and then run it, it asks me for the name of the file containing the ship information. When I've entered that, it tells me that the file has been loaded for the date that is given in the first line of the file I specified.

    It then procedes to print out all of the data for each ship in the text file, in the format:

    ship ID
    latitude
    longitude
    direction
    speed

    After that, it asks me for the duration I would like the simulation to run, and the time step that the program should move forward in each step of the simulation... after I've entered numbers for these two variables, the program exits.

    What I'm not sure about, is how get these two variables to apply to the simulation.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I'm not going to do the math for you and I'm not going to write the code...
    I have layed out for you the process your loops should take, so you should be able to work it from there.

    The problem is recalculating the location of each ship, making sure none of them get too close.

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    17
    Sorry- I don't mean to ask you to do the math or write the code... I'm just trying to get my head around using C again, as this is the first time I have used it in roughly two years...

    Thanks for all of your help so far. I'm fairly sure I understand the logic, it's just how to use C to do it that I think I'm unsure about. I'll try writing out my solution in pseudo code, to see if I can figure out how to do it in C then. Thanks again.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by someone2088 View Post
    Sorry- I don't mean to ask you to do the math or write the code... I'm just trying to get my head around using C again, as this is the first time I have used it in roughly two years...

    Thanks for all of your help so far. I'm fairly sure I understand the logic, it's just how to use C to do it that I think I'm unsure about. I'll try writing out my solution in pseudo code, to see if I can figure out how to do it in C then. Thanks again.
    If it's been a while and you're finding it's mostly escaped your grasp (hey, it happens) I would suggest a brief side trip into one of the many tutorials available online, as a bit of a refresher course. Also, get the library documentation for your compiler, if you don't already have it, and start looking stuff up... there's no shame in not remembering. IMO, the greater skill is to know how to read the docs than trying to memorize hundreds and hundreds of keywords and library calls... Nobody can hope to do that.

    I get away from this stuff for more than a few days and it starts to fade a bit... so I can certainly empathise.

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    17
    Ok, so I've written some code, which I think should theoretically work, but when I run it, I end up in an infinite loop...

    This is the code which is giving me the infinite loop:

    Code:
    while(current!=EOF){
              //  printf("%s\n%f\n%f\n%f\n%f\n", current->AISID, current->latitude, current->longitude, current->direction, current->speed);
              //  current = current->nextShip;
                
                /* Use the two lines above as a 'template for how to iterate through shipInfo-
                 * get each ship's lat and long, and use the great_circle method and location struct
                 * to find out the distance between two ships.
                 */
                location ship1, ship2;
                ship1.lat = current->latitude;
                ship1.lng = current->longitude;
                
                item *next = current->nextShip;
                ship2.lat = next->latitude;
                ship2.lng = next->longitude;
                
                double distanceBetweenShip1Ship2;
                distanceBetweenShip1Ship2= great_circle(ship1, ship2);
                printf("The distance between ship1 and ship2 is %d", distanceBetweenShip1Ship2);
                
                
                
            }
    The problem seems to occur after I have found the distance between the first two ships- instead of moving on to compare the distance between the first and third ship, it is just comparing the distance between the first and second again...

    The loop doesn't exit until it reaches End Of File, so since it never moves on to the next ship, I'm stuck in an infinate loop- it's just printing out the distance between the first and second ship over and over again....

    Also, rather than printing the distance as a double, which it should do, it seems to be printing a memory address or something, i.e. 1075589536.

    If someone could point out where I'm going wrong here, that would be great.

    Cheers!

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Is leaving this commented out intentional?
    Code:
              //  current = current->nextShip;
    There are (at least) two ways to exit a while() loop. The first is to satisfy the condition. The other is to use break.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Dec 2011
    Posts
    17
    Although that line is commented out in line 3, I have actually used it in line 13.

    I think what I need to do, is after I have displayed the distance between ship1 and ship2, I then need to set ships2 to point to the next ship in the list. (i.e. it's called ship2 because it stands for the second ship being compared, as opposed to being the second ship in the list)

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by someone2088 View Post
    Although that line is commented out in line 3, I have actually used it in line 13.
    If you mean this line:
    Code:
                item *next = current->nextShip;
    You are wrong about them being equivalent.

    That loop, as you've posted it, will run infinitely because current never changes. Therefore, current->nextShip never changes, therefore, next never changes. This is why it is always the same two ships.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terrible confusion with time variables
    By LowlyIntern in forum C++ Programming
    Replies: 12
    Last Post: 08-01-2008, 08:23 AM
  2. How many times can time variables be delcared?
    By Qui in forum C++ Programming
    Replies: 2
    Last Post: 04-17-2004, 05:24 PM
  3. Monitoring LAN
    By pasha in forum C Programming
    Replies: 4
    Last Post: 01-14-2003, 04:27 AM
  4. CPU monitoring
    By fenedara in forum Windows Programming
    Replies: 1
    Last Post: 01-08-2003, 09:14 AM
  5. lan monitoring
    By scott27349 in forum C++ Programming
    Replies: 3
    Last Post: 05-09-2002, 11:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21