Jump to content
  • Advertisement
Sign in to follow this  
chumchees

Can anyone help me with C programming please

This topic is 3789 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Write a C program for the following problem: A file contains the services available along Highway 401. Each entry details the exit number followed by a numerical value detailing the services. The exit number corresponds to the distance in kilometers from the Michigan border in Windsor. The services numerical code can be decoded using the following key: 1 = gas available 2 = food available 4 = lodging available 8 = hospital available For example, entries in the file such as 90 3 164 11 232 2 mean that exit 90 has gas and food (2+1), that exit 164 has a hospital, food and gas (8+2+1)and that exit 232 has food only (2). Note that for highway 401, the highest exit number is 825 near the Quebec border. Your first task is to read the entire highway file into an array, the subscript (offset, cell number)will indicate the exit number and the cell content the services code. Your program must contain a menu that offers the following options to the user: 1. An option that displays the list of all exit numbers that have hospitals. 2. An option that asks the user for a location in kilometers (from the Michigan border) and gives the two closest gas stations (1 eastbound, 1 westbound) with the direction clearly indicated and the actual distance to drive. 3. An option that asks the user for a location in kilometers (from the Michigan border) and gives the two exits (1 eastbound, 1 westbound) that have gas, food and lodging with the direction clearly indicated and the actual distance to drive. I finsihed the program with some help. I am a beginner with C and when i compiled the program it said ISO C89 forbids variable size arrays 'exits' Can someone help me fix the program so it will run. #include <stdio.h> #include <stdlib.h> const int MAX_EXIT = 826; int main() { FILE *input; int EXIT_FOUND = 0; int choice; int index = 0; int kilometers = 0; int east = 0, west =0; int exits[MAX_EXIT]; int exit_number = 0; int temp = 0; int i; input = fopen ("test.txt", "r"); for (i = 0; i < MAX_EXIT; i++) { exits = 0; } while(fscanf(input,"%d %d", &exit_number, &temp) != EOF) exits[exit_number] = temp; fclose(input); printf("Enter 1 to print all exits with a hospital\n"); printf("Enter 2 to print the closest exits with gas stations\n"); printf("Enter 3 to print the closest exits with gas, food, and lodging\n"); scanf("%d",&choice); if(choice == 1) { for(index = 0; index < MAX_EXIT; index++) { if(exits[index] >= 8) { printf("%d \n", index); } } } if(choice == 2) { printf("Enter your location in kilometers\n"); scanf("%d",&kilometers); index = kilometers; if(exits[index]% 2 != 0) { printf("Exit %d has gas\n", index); } index--; while(!EXIT_FOUND) { if(exits[index]% 2 != 0) { printf("Exit %d has gas in %d kilometers\n", index, (kilometers - index)); EXIT_FOUND = 1; index = kilometers + 1; } else { index--; } } EXIT_FOUND = 0; while(!EXIT_FOUND) { if(exits[index]% 2 != 0) { printf("Exit %d has gas in %d kilometers\n", index, (index - kilometers)); EXIT_FOUND = 1; } else { index++; } } } if(choice == 3) { printf("Enter your location in kilometers\n"); scanf("%d",&kilometers); index = kilometers; if(exits[index]== 7) { printf("Exit %d has gas, food and lodging\n", index); } index--; while(!EXIT_FOUND) { if(exits[index]== 7) { printf("Exit %d has gas, food and lodging in %d kilometers\n", index, (kilometers - index)); EXIT_FOUND = 1; index = kilometers + 1; } else { index--; } } EXIT_FOUND = 0; while(!EXIT_FOUND) { if(exits[index] == 7) { printf("Exit %d has gas, food and lodging in %d kilometers\n", index, (index - kilometers)); EXIT_FOUND = 1; } else { index++; } } } } please help me

Share this post


Link to post
Share on other sites
Advertisement
This is one of the reasons I prefer C++ [smile]

Does it work if you change:
const int MAX_EXIT = 826;
to:
#define MAX_EXIT 826
?

I can't remember how well C does with constants.

Share this post


Link to post
Share on other sites

This topic is 3789 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!