Thread: HELP with Program!

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    7

    HELP with Program!

    This is what I have to do:

    A bus line called the World's Longest Street Bus Line, wants you to write a program that produces a billing report based on passenger trips and cost of trips. The company has a large fleet of buses that run between towns, on different parts of Yonge Street from Toronto to Barrie. Yonge Street is over 700 miles long and is the longest street in the world. The buses can stop at many towns between Toronto and Barrie.



    These buses are equipped with the latest satellite technology that can tell where all buses are located. In order for a passenger to use the bus they need a credit card. When the passenger gets on the bus they slide their credit card into a machine. The machine reads the credit card number and sends a message containing the credit card number and the location where the passenger got onto the bus to the satellite. The satellite then sends the message to the head office of the bus line. When the passenger gets off the bus, they insert their credit card again and the same process occurs.



    The program will read two files: prices.dat containing data about prices and cities, and trips.dat containing all the trips taken in a single day. The program will process these files and produce a file, billing.rpt containing billing information, and also will print summary information on the screen.



    The prices.dat file contains one record (line) for each town with a town ID and a town price. The towns are listed, starting at Toronto. An example of 5 towns is:



    7 0.00

    15 5.00

    22 10.00

    3 15.50

    40 25.00



    The first town (Toronto) has an ID of 7 and a price of $0.00. The second town has an ID of 15 with a price of $5.00, etc. The cost of a trip from the 2nd town to the 4th town is $15.50 - $5.00 = $10.50.

    The cost of a trip from the 5th town to the 2nd town is $25.00 - $5.00 = $20.00.



    The trips.dat file contains one record for each passenger trip on a single day with an entry town ID, exit town ID and credit card number. A credit card number consists of 11 characters. An example of 6 trips is:



    7 15 MC123456789

    3 15 AE111222333

    3 99 AB432344655

    40 40 VZ333222111

    40 7 VI010101010

    7 40 VI010101010



    Your program must write one record to the billing.rpt file for each trip containing the credit card number, entry town ID, exit town ID and trip cost. If either the entry or exit town ID is not found in the prices file, or if the entry town equals the exit town, then the record must indicate an error and show the town IDs. The data must be neatly aligned. An example, based on the above files is:



    --- BILLING REPORT ---

    MC123456789 7 15 $ 5.00

    AE111222333 3 15 $10.50

    ** INVALID TOWN DATA: 3 99 *

    ** INVALID TOWN DATA: 40 40 *

    VI010101010 40 7 $25.00

    VI010101010 7 40 $25.00

    --- END OF BILLING REPORT ---



    Summary lines are to be printed on the screen showing the number of valid and invalid trips, and the total cost of all trips. For example:



    Number of valid trips processed: 4

    Number of invalid trips processed: 2

    Total trip revenue: $65.50



    Because your program must search the price data for each trip, it is efficient to first load the town and price data into arrays. However, because of the possible size of the trip file you should not load the trip data into an array.



    For each trip your program must search for the trip’s entry town and exit town, so you must code and use this function:



    int search( int data[ ], double value[ ], int size, int key, double *pkeyValue)



    that will sequentially search the ‘data’ array (containing ‘size’ number of items) for the value of the parameter ‘key’. If the ‘key is found then set the last parameter to the value of the corresponding item from the ‘value’ array, and return 1 (true). Return 0 (false) if the value is not found.

    If someone could help me out that would be great.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You failed to include what you've written so far.

    Help with homework is one thing, but simply posting your assignment without showing that you've attempted to work on it is another. Start writing the program, and when you have specific problems, ask about them.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    7
    This is what I have so far, I'm not quite sure how to do the search function?

    Code:
    #include <string.h>
    #include <stdio.h>
    
    
    main()
    {
      int i=0;
      int town[7];
      int key;
      int key2;
      double value[7];
      int size=7;
      double pkeyValue;
      int entry;
      int exit;
      char credit[11];
      int found;
      FILE *fp;
      FILE *fp2;
      FILE *fpout;
    
      fp = fopen("prices.dat","r");
      fp2 = fopen("trips.dat","r");
      fpout = fopen("billing.rpt","w");
    
    if(fp == NULL)
    
      printf("Error: Could not open file!!! \n");
    
    else
    
    {
    fprintf(fpout, "---    BILLING REPORT    ---\n");
    while(!feof(fp))
    {
      fscanf(fp,"&#37;d %lf\n", &town[i], &value[i]);
      printf("%d %.2lf\n", town[i], value[i]);
      i++;
    }
    for(i=0;i<10;i++){
     fscanf(fp2,"%d  %d %[^\n]\n", &entry, &exit, &credit);
    printf("%d %d %s\n", entry, exit, credit);
      key = entry;
      key2 = exit;
    found= search(town, value, size, key, key2, &pkeyValue);
     if ( found == 1 ){
      fprintf(fpout, "%s %d %d\n", credit, entry, exit);
    }
     if ( found == 0 ){
       fprintf(fpout, "** INVALID TOWN DATA: %d %d\n", entry, exit);
    }
    }
       fprintf(fpout, "--- END OF BILLING REPORT ---\n");
    }
     fclose(fpout);
     fclose(fp2);
     fclose(fp);
    }
    
    int search(int data[], double value[], int size, int key, int key2, double *pkey
    Value)
     {
    int i;
    if (key == data[size] && key2 == data[size]){
    *pkeyValue = value[i];
    return 1;
    }
    else{
    return 0;
    }
    }

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    7
    Can someone at least give me a few idea's of what to do? I'm kinda in crunch time here and some help would be greatly appreciated. Thanks.

  5. #5
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    A sequential search will probably we adequate in this case. Start at the beginning of the array you want to search and keep going until you find whatever you're looking for.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    7
    Basically I want to know how to compare "entry" and "exit" to "town", in the search function.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    7
    I still need more help, I don't know what I'm doing. If you could be more specific, with your help because I'm kinda new to programing.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    - create two struct's, one for prices and another for trips
    - create two arrays, one for prices (using the previous struct as the type of the array), and once for trips (likewise)
    - create a function to read the prices.dat file into the array of prices
    - ditto for the trips
    - create a function to display the prices in the prices array
    - ditto for trips

    Code:
    int main ( ) {
      priceType priceArray[MAX];
      numRead = readPrices( "prices.dat", priceArray, MAX );
      printPrices( priceArray, numRead );
    }
    What this proves is that you can read the files properly and generate a simple report.
    It's also a great foundation for the next step, generating billing information.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    while(!feof(fp)) - do not use feof to control loop, read FAQ http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    1
    Did you ever finish the program? and what does the program look like? I have a similar assignment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM