You are not logged in.

#1 2007-08-29 17:36:35

jinn
Member
From: Gothenburg
Registered: 2005-12-10
Posts: 506

C challenge, find 5 errors!

Find 5 errors within this c code! errors in the same row is counted as one, and ignore any logical errors in the algorithm of function find.
Give the solution to the error as well.

 
#include <stdio.h>
#include <stdlib.h>

struct Elem {int key; int value;};

//-performs binary search.
//- REturns -1 if key not found in array.
int find(Elem *array, int size, inte key)
{
   //-start is the first element in the search space
   //- stop is the last
   int start = 0, stop = size -1;
   int pos;

   for(;;)
   {
      if (stop < start) return -1;

      pos = start + (stop - start)/ 2;
      if (array[pos]->key == key) return array[pos]->value;

      //- Split the search space
      if(key < array[pos].key) stop = pos -1;
      else start = pos +1;
   }
}


int main(int argc, char *argv[])
{
    struct Elem *array[] =
    {
        { .key =0, .value =10 },
        { .key =4, .value =14 },
        { .key =7, .value =17 },
        { .key =11,.value =21 }
    };

    int value = find(array, 4, atio(argv[1]));
    printf("%s\n", value);

    return 0;
}

The ultimate Archlinux release name: "I am your father"

Offline

#2 2007-08-29 17:52:42

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: C challenge, find 5 errors!

What are the rules ? gcc detects all these errors.


pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

#3 2007-08-29 17:53:36

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: C challenge, find 5 errors!

int find(Elem *array, int size, inte key)

I know of no datatype called inte.  big_smile  Also, "Elem" should be "struct Elem"

if (array[pos]->key == key) return array[pos]->value;

array[pos]->x should be array[pos].x for all x.

struct Elem *array[]

bogus syntax - get rid of the *. -edit- I guess I should clarify that, in general, it's not bogus syntax per se, but for this specific example it is. -/edit-

int value = find(array, 4, atio(argv[1]));

atio is undefined.

    printf("%s\n", value);

Should be %d, not %s

-edit- Shining makes a good point.  I didn't use gcc to find these, but I suppose anyone with a bit of knowledge could use it to find 'em.  Although, I think the error for the third one I posted would be a bit more vague... -/edit-

Last edited by Cerebral (2007-08-29 18:28:25)

Offline

#4 2007-08-29 18:03:19

jinn
Member
From: Gothenburg
Registered: 2005-12-10
Posts: 506

Re: C challenge, find 5 errors!

Yeah.. the rules should be not using gcc.. just reading the code and finding out.. Sorry I thought it was obvious not to use gcc sine it would be no challenge..

Nice cerebral, about the datatype inte is just my mispell..

Last edited by jinn (2007-08-29 18:05:38)


The ultimate Archlinux release name: "I am your father"

Offline

#5 2007-08-29 18:45:39

jinn
Member
From: Gothenburg
Registered: 2005-12-10
Posts: 506

Re: C challenge, find 5 errors!

damn.. I missed this..

segfault at atoi after fixing the code up.. Cerebral can you answer this also big_smile


The ultimate Archlinux release name: "I am your father"

Offline

#6 2007-08-29 19:03:05

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: C challenge, find 5 errors!

Er.  Pass in a parameter?

Offline

#7 2007-08-29 19:20:07

jinn
Member
From: Gothenburg
Registered: 2005-12-10
Posts: 506

Re: C challenge, find 5 errors!

nope.. Actually I have no idea why it segfaults..

[21:16 root programming]# ./a.out 4
zsh: segmentation fault  ./a.out 4
[21:17 root programming]#


The ultimate Archlinux release name: "I am your father"

Offline

#8 2007-08-29 19:23:07

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: C challenge, find 5 errors!

then compile with -g and run it through gdb.

$ gcc test.c -g
$ gdb a.out

When it crashes, type 'bt' to get a backtrace - it'll tell you where it's crashing, and you can print out the contents of variables to figure out what's going on.

Offline

#9 2007-08-29 19:33:17

jinn
Member
From: Gothenburg
Registered: 2005-12-10
Posts: 506

Re: C challenge, find 5 errors!

yeah, tried that.. but cant get anything useful from this..

(gdb) run 0
Starting program: /root/programming/a.out 0

Program received signal SIGSEGV, Segmentation fault.
0xb7e7d6cb in strlen () from /lib/libc.so.6
(gdb) bt
#0  0xb7e7d6cb in strlen () from /lib/libc.so.6
#1  0xb7e50795 in vfprintf () from /lib/libc.so.6
#2  0x08048590 in _IO_stdin_used ()
#3  0x00000000 in ?? ()
(gdb)

The ultimate Archlinux release name: "I am your father"

Offline

#10 2007-08-29 19:37:23

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: C challenge, find 5 errors!

check your printf line.

Offline

#11 2007-08-29 19:39:56

jinn
Member
From: Gothenburg
Registered: 2005-12-10
Posts: 506

Re: C challenge, find 5 errors!

damn.. I am blind.. forgot about the error that I created.. %s..


The ultimate Archlinux release name: "I am your father"

Offline

Board footer

Powered by FluxBB