C Programming: Arrays and pointers

In summary, the program should prompt the user to enter a time and then check to see if the time entered is greater than 19:00. If it is, the program should print out the corresponding departure and arrival times for the flight whose departure time is closest to that entered by the user.
  • #1
SnakeDoc
27
1

Homework Statement


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?
 
Physics news on Phys.org
  • #2
SnakeDoc said:

Homework Statement


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?
Please show us the rest of your code. I don't think you've given us enough information to answer your question.
 
  • #3
Mark44 said:
Please show us the rest of your code. I don't think you've given us enough information to answer your question.

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
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:
  • #4
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.
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;
 
  • #5
SnakeDoc said:
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.

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:
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.
 
  • #6
Mark44 said:
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.
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;
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:
  • #7
SnakeDoc said:
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?
No. Declaring an array b[6] means that it will have 6 elements, numbered 0 to 5.
 
  • #8
int departures[7]={480, 583, 679, 767, 840, 945, 1140, 1305};
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.
 
  • Like
Likes SnakeDoc
  • #9
DrClaude said:
No. Declaring an array b[6] means that it will have 6 elements, numbered 0 to 5.
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.
 

1. What is an array in C programming?

An array in C programming is a data structure that can hold a fixed number of values of the same data type. These values are stored in consecutive memory locations and can be accessed and manipulated using their index number.

2. How do you declare an array in C programming?

To declare an array in C programming, you need to specify the data type of the array elements followed by the array name and the size of the array in square brackets. For example, int numbers[5] will declare an array named 'numbers' that can hold 5 integer values.

3. What is a pointer in C programming?

A pointer in C programming is a variable that stores the memory address of another variable. It allows you to indirectly access and manipulate the value stored in that specific memory location.

4. How do you declare a pointer in C programming?

To declare a pointer in C programming, you need to specify the data type of the variable it will be pointing to, followed by an asterisk (*), and then the name of the pointer variable. For example, int *ptr will declare a pointer named 'ptr' that will point to an integer value.

5. How do you access array elements using pointers in C programming?

To access array elements using pointers in C programming, you can use the array name followed by the index number in square brackets or you can use the pointer variable and the dereference operator (*) to access the value stored at that memory location. For example, numbers[2] or *(ptr + 2) will both access the third element in the 'numbers' array.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
918
  • Engineering and Comp Sci Homework Help
Replies
8
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
Back
Top