Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Eliminating this seg fault

0 views
Skip to first unread message

Albert

unread,
Nov 23, 2009, 9:20:38 PM11/23/09
to
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[i], 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[i]);
printf("%d\n", find("TRS80MCX"));
return 0;
}

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

Chris McDonald

unread,
Nov 23, 2009, 9:31:31 PM11/23/09
to
Albert <albert.xt...@gmail.com> 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.

Ben Bacarisse

unread,
Nov 23, 2009, 9:38:09 PM11/23/09
to
Albert <albert.xt...@gmail.com> 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[i]);
> 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[i] 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.

Albert

unread,
Nov 23, 2009, 9:42:49 PM11/23/09
to
Chris McDonald wrote:
> Albert <albert.xt...@gmail.com> 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...

Richard Heathfield

unread,
Nov 23, 2009, 10:39:38 PM11/23/09
to
In <WpHOm.57057$ze1....@news-server.bigpond.net.au>, Albert wrote:

<snip>

> char *inlist[MAXCARS], *outlist[MAXCARS]; /* stores the list of
> license plates before the cars enter the tunnel and after. */

Nice pointers. Where do they point?

<snip>



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

You have pointers, but they're not pointing anywhere.

Solution: if you want to store X bytes of external data in your
program, consider allocating X bytes of memory in which to store
them.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within

Richard Heathfield

unread,
Nov 23, 2009, 10:41:34 PM11/23/09
to
In <JKHOm.57059$ze1....@news-server.bigpond.net.au>, Albert wrote:

> Chris McDonald wrote:
>> Albert <albert.xt...@gmail.com> 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.

Why do you believe that? You set the index at 1. What makes you think
it can ever be 0?

> My 1 based indexing in the for
> loops is consistent...

...consistently wrong.

Albert

unread,
Nov 25, 2009, 3:27:59 AM11/25/09
to
Ben Bacarisse wrote:
> <snip>

> The problem is that there is nowhere to put the strings. inlist[i] 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.

Phil Carmody

unread,
Nov 25, 2009, 7:20:29 PM11/25/09
to
Albert <albert.xt...@gmail.com> 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[i]);
> 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

0 new messages