Thread: C program to delay flights

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    15

    C program to delay flights

    Hey I am trying to make a C program which delays or sets airplane landing times. I have a problem with delaying the times. Here is the function which I am trying to do it :

    Code:
    int canGoDown()
    {
        int time[24]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};
        for(int i=0; i<28; i++)
        {
            for(int j=0; j<24;j++)
            {
                if(time[j] == 0)
                {
                     plane[i].landingTime += 1;
                }
                else
                {
                    plane[i].landedTime = plane[i].landingTime;
                    time[j] = 0;
                }
            }
        }
    }


    I am basically trying to set the landing times. If the landing time is 0 it will delay the flight by one hour and try again. If it is anything else it will set the time to that and set the time to 0 which represents occupation.

    My input file tells me when will the flights land for example a plane will land at 14:00 if the time is occupied it should try to land it on 15:00 if that is occupied it should try to land at 16:00 etc. This function currently returns all zeroes. For example if one plane is set to land at 14:00 no other plane should be able to land at time. So all the other planes which will land at 14:00 should be delayed until they find an empty hour to land. Currently there is 28 planes and I am trying to make it to work for only one day. So 4 of the planes will never land. I tried so many things since last night but I couldn't get it to work. Here is the excel file of the landing times of the planes and when should they land : https://flic.kr/p/2mQmaSa

    I think it is a great logic but it doesn't work. I think I have a problem on setting the loops. If i can get any help i'd be so happy.
    My input.txt is below.
    Attached Files Attached Files
    Last edited by fleafy; 12-13-2021 at 06:20 AM. Reason: wrong code format

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    How can you fit 28 planes into 24 landing times?

    Anyway, your logic is in fact wrong. Presumably you mean to test if time[plane[i].landingTime] is 0, not time[j]. In fact, looping through the times doesn't make sense. You just need the outer loop through the planes. Also, what happens if the time is 24 and you add 1?

    Also, after translating the turkish I notice that the first value on each line in input.txt is a "priority". How is that used?
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Oct 2021
    Posts
    15
    The priority will come later. I need to set-up the landing times by priority of the planes. For example: 1.Emergency Plane, 2.War Plane, 3.Passanger Plane, 4.Cargo Plane. As seen on the photo which I linked on the question five of the planes will not land because if a plane cant land after 3 times they will be automatically redirected to another airport those ones are marked with X LAND. I can make the check of it later for now I just need to land every plane then I'll focus on other problems.

  4. #4
    Registered User
    Join Date
    Oct 2021
    Posts
    15
    If the time is 24 and we add 1 more I want to redirect the plane to another airport.

  5. #5
    Registered User
    Join Date
    Oct 2021
    Posts
    15
    Screenshot 2021-12-13 164115 | Berk Sunduri | Flickr Here is the code I reworked on based on your writings. Screenshot 2021-12-13 164005 | Berk Sunduri | Flickr Here is the output of that code. e0a199ab-5781-426b-89a1-b3c6c396a61f | Berk Sunduri | Flickr and here is how the output should look like just until the second landing time.

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Your j loop doesn't make sense. Get rid of it. Instead, make a loop to increment the landing time until you find a spot or until you've incremented it 3 times or until it goes above 24.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  7. #7
    Registered User
    Join Date
    Oct 2021
    Posts
    15
    How can I see what spot is occupied or not? Without this if-else statements I can't think of any other way. I made a loop which increments values only 3 times, some of the landing times were correct this time but there is still a huge portion which is wrong and repeats.

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    I didn't say don't use if statements, just that the j loop doesn't make sense. Post your current code (not a picture of it).
    A little inaccuracy saves tons of explanation. - H.H. Munro

  9. #9
    Registered User
    Join Date
    Oct 2021
    Posts
    15
    Code:
     
    int canGoDown()
    {
        int time[24]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};
        for(int i=0; i<28; i++)
        {
            for(int j=0; j<3;j++)
            {
    
    
                if(time[plane[i].landingTime] == 0)
                {
                    plane[i].landingTime += 1;
                }
                if(plane[i].landingTime>24)
                {
                    plane[i].landingTime=-1;// for marking the ones which goes above the 24h time limit
                }
                else
                {
                    plane[i].landedTime= plane[i].landingTime;
                    time[j] = 0;
                }
            }
        }
    }


    In my int main() I just read the data from file and sort it according to priority of the planes. And I call the function and then print the contents of the struct. Sorry I am a newbie to programming.
    Last edited by fleafy; 12-13-2021 at 08:28 AM.

  10. #10
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Maybe something like this (untested) :
    Code:
    void adjustLandingTimes()
    {
        int time[25] = {0}; // not using element [0]
     
        for (int i = 0; i < 28; i++)
        {
            int found = 0;
            for (int attempts = 0; attempts <= 3; attempts++)
            {
                if (time[plane[i].landingTime] != 0)
                {
                    if (++plane[i].landingTime > 24)
                    {
                        plane[i].landingTime = -1;// for marking the ones which goes above the 24h time limit
                        break;
                    }
                }
                else
                {
                    found = 1;
                    break;
                }
            }
     
            if (found)
            {
                plane[i].landedTime = plane[i].landingTime;
                time[plane[i].landingTime] = 1;
            }
        }
    }
    It would be better if you passed the plane array in along with the number of planes instead of using a global variable and hardcoded constant.

    Note that you need to sort by priority as the primary field and id as the secondary field. If you are using a stable sort to sort by priority then since the ids are already in order they will stay ordered within the priority categories. But that assumes the input will always be in id order.
    Last edited by john.c; 12-13-2021 at 09:09 AM. Reason: changed attempts < 3 to attempts <= 3
    A little inaccuracy saves tons of explanation. - H.H. Munro

  11. #11
    Registered User
    Join Date
    Oct 2021
    Posts
    15
    Sir, thank you for your time. The program you wrote works fantastic. Now I just need to include priority stuff into the code as I mentioned above. Just a quick question can I find how many times a plane delayed like this:

    plane[i].delayed=plane[i].landedTime-plane[i].landingTime;

    I am trying this but it doesn't work. Just shows zeroes.

  12. #12
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    I think you've already handled the priorities by sorting.

    The reason you get zeroes is because you are changing the landingTimes. You should use a separate variable so you don't change the original. Something like the following. I also changed it to take the planes array as a parameter, but you can change it back to use a global if you want.
    Code:
    void adjustLandingTimes(Plane *planes, int num_planes)
    {
        int times[25] = {0}; // not using element [0]
        for (int i = 0; i < num_planes; i++)
        {
            int landingTime = planes[i].landingTime;
            for (int attempts = 0; attempts <= MAX_ATTEMPTS; attempts++)
            {
                if (times[landingTime] == 0)
                {
                    planes[i].landedTime = landingTime;
                    times[landingTime] = 1;
                    break;
                }
                if (++landingTime > 24)
                {
                    planes[i].landedTime = -1;
                    break;
                }
            }
        }
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  13. #13
    Registered User
    Join Date
    Oct 2021
    Posts
    15
    Yes I found my own mistake sorry for not correcting the reply! I am very thankful for your time you spend on my problem sir. Have a nice day/night!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in delay program.
    By Ajinkya Yalmar in forum C Programming
    Replies: 17
    Last Post: 10-16-2014, 12:18 PM
  2. Makeing a delay in program.
    By apacz in forum C++ Programming
    Replies: 7
    Last Post: 05-28-2007, 03:08 PM
  3. making my program sleep / wait / delay...
    By bobthebullet990 in forum C++ Programming
    Replies: 4
    Last Post: 08-13-2006, 10:14 AM
  4. Flights Of Fantasy
    By kawk in forum Game Programming
    Replies: 4
    Last Post: 04-02-2005, 02:29 PM
  5. program delay (sleep)
    By rxg00u in forum C++ Programming
    Replies: 5
    Last Post: 04-16-2002, 12:54 PM

Tags for this Thread