1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C Programming: Arrays and pointers

  1. Nov 8, 2015 #1
    1. The problem statement, all variables and given/known data
    So I created two arrays one called departure_time and the other arrival_time and populate each array as follows. I've tried both putting 8 and defining N as 8

    int departure_times[N]={480, 538, 679, 767, 840, 945, 1140, 1305};
    int arrival_times[N]={616, 712, 811, 900, 968, 1075, 1280, 1438};
    I get two errors.
    The first is that it says "excess elements in array initializer"
    The second is that when I print out the element
    printf("arrival time in minutes past midnight: %d", arrival_times[7]);

    it returns either a 0 or a number from the other array

    So am I missing something?
     
  2. jcsd
  3. Nov 8, 2015 #2

    Mark44

    Staff: Mentor

    Please show us the rest of your code. I don't think you've given us enough information to answer your question.
     
  4. Nov 8, 2015 #3
    Write a program that asks the user to enter a time (expressed in hours and minutes, using the 24-hour clock). The program then displays the departure and arrival times for the flight whose departure time is closest to that entered by the user, using standard time.
    Specifications:
    The program should prompt the user to enter a time and pattern match against the ‘:’ character. After reading in the hours and minutes, calculate the time in minutes past midnight, and pass the time into a function that finds the flight whose departure time is closest to the desired_time (expressed in minutes since midnight). The function will store the departure and arrival times of the flight (also expressed in minutes since midnight) in variables pointed to by departure_time and arrival_time, respectively. Use the following function prototype: void find_closest_flight(int desired_time, int *departure_time, int *arrival_time); Inside the function, define two constant integer arrays called departures and
    arrivals that store the departure times and arrival times of the airplane in minutes since midnight. Use a global constant to help convert the departure and arrival times to minutes past midnight. Use a while() loop and if-else statement to determine which element of the array contains the departure time closest to the desired_time. Then assign the appropriate departure and arrival times to the variables pointed to by the pointers departure_time
    and arrival_time, and exit the function. Once the find_closest_flight() function returns back to the main function, use the information stored in the variables that were point ed to by
    departure_time and arrival_time in the function to back out the actual departure and arrival times. Since the times are in minutes past midnight, you must pull out the hour and minutes, convert from 24 time to standard time, and print out the appropriate results – with the appropriate number of digits.For example, 13:15 is 13 x 60 + 15 = 795 minutes since midnight, which is closer to 12:47 pm (767 minutes since midnight) than to any of the other departure times.

    TLDR: So what the code is supposed to do is ask for the user to enter they're desired departure time in 24 hour format. Then it must be converted to minutes past midnight. Then it's supposed take their desired_time and compare it against a table of of departure times and the corresponding arrival time and print out the flight departure time and it corresponding arrival time that is closest to their desired time in 12 hour format. Finding the closest departure time must be found in a separate function. The pointers departure_time and arrival_time must be used to look back at the 2 constant arrays.


    I was having trouble with a loop that would check the departure_time and arrival_time because it would print values from the opposing array if the time entered is greater than 19:00.

    Departure Time Arrival Time
    8:00 am 10:16 am
    9:43 am 11:52 am
    11:19 am 1:31 pm
    12:47 pm 3:00 pm
    2:00 pm 4:08 pm
    3:45 pm 5:55 pm
    7:00 pm 9:20 pm
    9:45 pm 11:58 pm
    Mod note: Added code tags
    Code (C):

    //preprocessor directives
    #include <stdio.h>
    #include <stdlib.h>

    //declare prototypes
    void find_closest_flight(int desired_time);
    //external variables
    int arrival_hours, arrival_minutes, departure_hours, departure_minutes, hours, minutes,minutes_past_mid;
    //call main function
    int main(void)
    {
      int departures[8]={480, 583, 679, 767, 840, 945, 1140, 1305};
      int arrivals[8]={616, 712, 811, 900, 968, 1075, 1280, 1438};
      //*departure_time = &departure[];
      // *arrival_time = &arrivals[];
      //ask for time in hours:
      printf("Enter a 24-hour time: ");
      scanf("%d: %d", &hours, &minutes);
     
      minutes_past_mid = (hours*60) + minutes;
     
      find_closest_flight(minutes_past_mid);
     
      exit(EXIT_SUCCESS);
    }
    void find_closest_flight(int desired_time)
    {
      int departures[7]={480, 583, 679, 767, 840, 945, 1140, 1305};
      int arrivals[7]={616, 712, 811, 900, 968, 1075, 1280, 1438};
      int i=0;
     
      if(desired_time>departures[0])
      {
      while( desired_time > departures[ i])
      {  
      arrivals[ i];
      i++;
      }
      departures[ i] = departures[i-1];
      arrivals[ i] = arrivals[i-1];
      }
      else
      {
      while( desired_time < departures[ i])
      {  
      arrivals[ i];
      i--;
      }
      departures[I] = departures[i+1];
      arrivals[ i] = arrivals[i+1];
      }
      printf("Closest Departure/ Arrival Times\n\n");
     
      departure_hours = departures[ i]/60;
      departure_minutes = departures[ i]%60;
      arrival_hours = arrivals[ i]/60;
      arrival_minutes = arrivals[ i]%60;
     
      if(departure_hours>12)
      departure_hours= departure_hours-12;
      if(arrival_hours>12)
      arrival_hours=arrival_hours-12;
      if(departure_hours == 0)
      departure_hours = 12;
      if(arrival_hours == 0)
      arrival_hours = 12;
      if(departures[I]>=720)
      {
      printf("Departure Time: \t\t %d:%.2d pm\n", departure_hours, departure_minutes);
      if( arrivals[ i] >= 720 )
      {
      printf("Arrival Time1: \t\t %d:%.2d pm", arrival_hours, arrival_minutes);
      }
      else
      {
      printf("Arrival Time2: \t\t %d:%.2d pm", arrival_hours, arrival_minutes);
      }
      }
      if(departures[ i]<720)
      {
      printf("Departure Time: \t\t %d:%.2d am\n", departure_hours, departure_minutes);
      if( arrivals[ i] >= 720 )
      {
     
      printf("Arrival Time3: \t\t %d:%.2d pm", arrival_hours, arrival_minutes);
      }
      else
      {
      printf("Arrival Time4: \t\t %d:%.2d am", arrival_hours, arrival_minutes);
      }
      }
    }
     
    Last edited by a moderator: Nov 8, 2015
  5. Nov 8, 2015 #4

    Mark44

    Staff: Mentor

    In the code below, you have too many initializers. Could that be the problem you saw?
    Also, it doesn't seem like a good idea to me to have these array variables as locals. You should probably pass them as parameters when you call the function.
    Code (C):
    void find_closest_flight(int desired_time)
    {
    int departures[7]={480, 583, 679, 767, 840, 945, 1140, 1305};
    int arrivals[7]={616, 712, 811, 900, 968, 1075, 1280, 1438};
    int i=0;
     
     
  6. Nov 8, 2015 #5

    Mark44

    Staff: Mentor

    Your find_closest_flight() function doesn't match the required prototype -- yours has only the first one, the desired time.

    The header and first couple of lines of code in the body should look like this:
    Code (C):
    void find_closest_flight(int desired_time, int * departure_time, int * arrival_time)
    {
       int departures[ ]={480, 583, 679, 767, 840, 945, 1140, 1305};
       int arrivals[ ]={616, 712, 811, 900, 968, 1075, 1280, 1438};
    When the function above returns, the memory pointed to by departure_time and arrival_time should be set with the appropriate times.
     
  7. Nov 8, 2015 #6
    I don't understand how I have too many initializers? I mean maybe I'm remembering incorrectly but doesn't the amount of elements equal the number inside the brackets plus 1. So an array b[6] has seven element because it starts with element 0?
     
    Last edited: Nov 8, 2015
  8. Nov 8, 2015 #7

    DrClaude

    User Avatar

    Staff: Mentor

    No. Declaring an array b[6] means that it will have 6 elements, numbered 0 to 5.
     
  9. Nov 8, 2015 #8

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    Your array has 7 places, but you try to fill in 8. That leads to the error, not the code you quoted in post 1.

    In general, having the same numbers twice in the code is something you should never use. It will lead to inconsistencies at some point, and a lot of extra work before because you have to update two sets of numbers.
     
  10. Nov 10, 2015 #9
    Thank you I haven't used an array in a while and it's a good thing you cleared that up my next exam will be focusing on arrays.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted