0

I have an array of 10 elements. This array stocks some informations but last information is ended by an symbol "END" (which is -1).

for exemple:

array[0] = 1;
array[1] = 1;
array[2] = 1;
array[3] = 1;
array[4] = 1;
array[5] = 1;
array[6] = END;

So in this case the length is 7 instead of 10.

Here is my algorithm:

for (i = 0; (i < 10) 
             && (array[i] != END); i++)
 {
  ;
 }

at the end i contains the length.

Is it correct or there is an other methode?

physics
  • 161
  • 9
  • 1
    Hardcoding `10` like this isn't a very good idea. – Bartek Banachewicz May 30 '14 at 14:12
  • The number of elements of an array (eg `arr`) is given by `sizeof arr / sizeof *arr`. If an array is passed to a function, inside the function it is no longer an array (it is a pointer to the array first element) and the expression no longer represents the number of elements. – pmg May 30 '14 at 20:00

5 Answers5

4

No doubt there are [at least a zillion] other methods, but this one looks fine. Code style hints at making that magic '10' a named constant though.

Quentin
  • 62,093
  • 7
  • 131
  • 191
3

(i < 10) && (array[i] != END); will be very fragile. Remember it's undefined behaviour to access an array element outside the bounds of the array.

If you know that the end of the array is always END, then you can drop the (i < 10) altogether.

It would be better though to pass the array and its length into any function that consumes the array.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • for the first remark, the index i is protected by the first condition( i < 10), i think that the case of the bounds of the array will never happen – physics May 30 '14 at 14:51
  • It will never happen **for now**. When a colleague starts working on your code base and you're far too senior to get your hands dirty in what by then to you will be trivialities, that situation might be a little different! – Bathsheba May 30 '14 at 15:24
0

My "DRY" perspective is that the length of the array should imply how many data elements are in it. Consider sizing the array to match the number of elements beforehand, or use another data type like ArrayList or a List that allows resizing.

EDIT: It looks like you are in C (I misread). Resizing can be hairier.

Community
  • 1
  • 1
DenverJT
  • 125
  • 7
0

You should create a struct to represent your collection.

struct MyCollection {
    int *items;
    int count;
};

MyCollection* MyCollection_Create(int capacity) {
   MyCollection *ret = malloc(sizeof(MyCollection));
   ret->items = calloc(sizeof(int) * capacity);
   ret->count = 0;
   return ret;
}

MyCollection_Destroy(MyCollection *col) {
  free(col->items);
  free(col);
}

MyCollection_AddItem(MyCollection *col, int item) {
  col->items[col->count++] = item;
}

int MyCollection_GetCount(MyCollection *col) {
   return col->count;
}
CuriousGeorge
  • 7,120
  • 6
  • 42
  • 74
0

C strings are arrays of char that are terminated with '\0'.

The question code is doing a very similar thing, only terminating with a (-1) instead of a (0).

The question algorithm is not that different from C's strlen(); which counts the number of elements in the array/string until it encounters the '\0' terminator.

Given the success of C strings, the algorithm is sound.

Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28