Eliminating this seg fault

Discussion in 'C Programming' started by Albert, Nov 24, 2009.

  1. Albert

    Albert Guest

    As a response to the following problem,

    Speed Cameras
    Input File: camin.txt
    Output File: camout.txt
    Time Limit: 1 second

    As director of the Safe Driving Ministry, you are faced with a dilemma.
    None of your speed cameras are working, but your department desperately
    needs the extra revenue for their new Super Espresso Coffee Deluxe. With
    determination, you and your co-workers head down to the highway to
    balance the budget.

    You order your officers Bernard and Peter to stand at either end of a
    tunnel running through a hill. Bernard records the order in which cars
    enter the tunnel, and Peter records the order in which cars exit the
    tunnel. There is a strict no-overtaking rule in the tunnel, so given
    this information you can reliably pull over and fine a certain number of
    drivers at your roadblock further down the highway.

    Given Bernard's and Peter's lists, you must write a program that
    determines how many cars you can claim made an illegal overtake with
    certainty.

    Input

    The first line of input will contain a single integer N representing the
    number of cars that drove through the tunnel ( 1 <= N <= 1000).

    Following this will be N lines describing Bernard's list. Each line will
    contain the number plate of a car that entered the tunnel, in order from
    first entry to last entry. Following this will be N lines describing
    Peter's list, each line containing the number plate of a car that exited
    the tunnel in order from first exit to last exit.

    Each number plate consists of at least six and at most eight characters.
    Only capital letters (A-Z) and digits (0-9) will be used. No two cars
    will have the same number plate.

    Output

    Output should consist of a single integer representing how many drivers
    you know with certainty have made an overtake.

    Sample Input 1

    4
    109DLY
    SSH2ANU
    VOLVO76
    REDCAR
    REDCAR
    109DLY
    SSH2ANU
    VOLVO76

    Sample Output 1

    1

    Sample Input 2

    5
    TRS80MCX
    IOI2004
    DEB18N
    REDCAR
    REGINA41
    IOI2004
    REGINA41
    REDCAR
    TRS80MCX
    DEB18N

    Sample Output 2

    3

    Sample Input 3

    5
    REDCAR
    L0LL1P0P
    UQ9396
    109DLY
    B1LKENT
    B1LKENT
    UQ9396
    REDCAR
    L0LL1P0P
    109DLY

    Sample Output 3

    2

    I'm getting a seg fault with Input 2 and

    /* This program reads in two lists of car licence plates and uses this
    to determine how many cars have overtaken *with certainty*. It simulates
    the overtakings starting with the car at the top of out list, modifying
    the in list until the bottom of the out list is reached. */

    #include <stdio.h>
    #include <string.h>

    enum { MAXCARS = 1000 };

    FILE *in, *out;
    int ncars;
    char *inlist[MAXCARS], *outlist[MAXCARS]; /* stores the list of license
    plates before the cars enter the tunnel and after. */

    int find(char *s)
    {
    int i;
    for (i = 1; i <= ncars; i++)
    if (strcmp(inlist, s) == 0)
    return i;
    }

    int main(void)
    {
    in = fopen("speedin.txt", "r"); /* CHECK */
    out = fopen("speedout.txt", "w");

    fscanf(in, "%d", &ncars);
    int i;
    for (i = 1; i <= ncars; i++)
    fscanf(in, "%s", inlist);
    printf("%d\n", find("TRS80MCX"));
    return 0;
    }

    Running it under GDB reveals an issue with strcmp...What is wrong?
     
    Albert, Nov 24, 2009
    #1
    1. Advertising

  2. Albert <> writes:

    >As director of the Safe Driving Ministry, you are faced with a dilemma.


    Who has the dilemma?

    >Running it under GDB reveals an issue with strcmp...What is wrong?


    Array indicies start at 0 in C ?

    --
    Chris.
     
    Chris McDonald, Nov 24, 2009
    #2
    1. Advertising

  3. Albert <> writes:

    > As a response to the following problem,

    <snip problem>
    > #include <stdio.h>
    > #include <string.h>
    >
    > enum { MAXCARS = 1000 };
    >
    > FILE *in, *out;
    > int ncars;
    > char *inlist[MAXCARS], *outlist[MAXCARS]; /* stores the list of
    > license plates before the cars enter the tunnel and after. */

    <snip>
    > int main(void)
    > {
    > in = fopen("speedin.txt", "r"); /* CHECK */
    > out = fopen("speedout.txt", "w");
    >
    > fscanf(in, "%d", &ncars);
    > int i;
    > for (i = 1; i <= ncars; i++)
    > fscanf(in, "%s", inlist);
    > printf("%d\n", find("TRS80MCX"));
    > return 0;
    > }
    >
    > Running it under GDB reveals an issue with strcmp...What is wrong?


    The problem is that there is nowhere to put the strings. inlist is
    NULL so the fscanf has nowhere to put what it reads. Since the
    strings are small and memory seems not to be a constraint in this
    case, you can just make the lists be arrays of char arrays:

    char inlist[MAXCARS][9];

    You can get away with 8 if you are very careful about unterminated
    "strings". I have to put it in quotes because it is not really a
    string if it has no null character at the end.

    --
    Ben.
     
    Ben Bacarisse, Nov 24, 2009
    #3
  4. Albert

    Albert Guest

    Chris McDonald wrote:
    > Albert <> writes:
    >
    >> <snip>
    >> Running it under GDB reveals an issue with strcmp...What is wrong?

    >
    > Array indicies start at 0 in C ?


    I believe all the inputs of strings are written to each element of the
    in array starting at 0 though. My 1 based indexing in the for loops is
    consistent...
     
    Albert, Nov 24, 2009
    #4
  5. Albert

    Albert Guest

    Ben Bacarisse wrote:
    > <snip>
    > The problem is that there is nowhere to put the strings. inlist is
    > NULL so the fscanf has nowhere to put what it reads. Since the
    > strings are small and memory seems not to be a constraint in this
    > case, you can just make the lists be arrays of char arrays:
    >
    > char inlist[MAXCARS][9];
    > <snip>


    Thanks - have a 100% solution now.
     
    Albert, Nov 25, 2009
    #5
  6. Albert

    Phil Carmody Guest

    Albert <> writes:
    > int main(void)
    > {
    > in = fopen("speedin.txt", "r"); /* CHECK */
    > out = fopen("speedout.txt", "w");
    >
    > fscanf(in, "%d", &ncars);
    > int i;
    > for (i = 1; i <= ncars; i++)
    > fscanf(in, "%s", inlist);
    > printf("%d\n", find("TRS80MCX"));
    > return 0;
    > }
    >
    > Running it under GDB reveals an issue with strcmp...What is wrong?


    Your code.

    Quite horrifically.

    Try making your pointers actually point to something before using
    them.

    Phil
    --
    Any true emperor never needs to wear clothes. -- Devany on r.a.s.f1
     
    Phil Carmody, Nov 26, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vinod

    Suprising seg fault!!!

    Vinod, Sep 27, 2003, in forum: C++
    Replies:
    3
    Views:
    489
    Ron Natalie
    Sep 27, 2003
  2. Corrine
    Replies:
    1
    Views:
    444
    Gianni Mariani
    Nov 27, 2003
  3. Manuel Maria Diaz Gomez

    fstream::open & seg fault

    Manuel Maria Diaz Gomez, Jul 20, 2004, in forum: C++
    Replies:
    3
    Views:
    1,308
    John Harrison
    Jul 20, 2004
  4. goktan
    Replies:
    4
    Views:
    2,038
    Howard
    Aug 2, 2004
  5. Matthias Kaeppler

    seg-fault on vector-auto-reserve

    Matthias Kaeppler, Feb 27, 2005, in forum: C++
    Replies:
    2
    Views:
    483
    Victor Bazarov
    Feb 27, 2005
Loading...

Share This Page