### Best practise for finding length of elements in an int array

Hi,

I need to calculate the length of an array by its elements, for example my int array would be something like below.

3245232523500000000000000

The array has a defined like this below.

char compared_p[2002] = {} ;

As above the numbers in the array can change, also the amount of numbers, but there it's always likely that 0's will be at the end of the array.

I have been looking at something like this below, not sure i fully understand it.

int size_of_a = sizeof( compared_p ) / sizeof( compared_p[ 0 ] );

As I am aware it takes into account the size of the full array "2002" which is not what i would like. All i need to do is find out how many numbers are in my array that are not 0.

Any help would be greatly appreciated
Best practice? Use std::vector.
The following statement
`int size_of_a = sizeof( compared_p ) / sizeof( compared_p[ 0 ] );`
Takes the total size of the array in bytes (2002) and divides it by the size of one element (char = 1 byte) = 2002.

 ``1234567891011`` ``````char compared_p[2002] = {}; strcpy( compared_p, "3245232523500000000000000" ); int numNonZero = 0; // The following loop ends when it finds a null, which strcpy will add at the end of the string for( int i=0; compared_p[i]; i++ ) if( compared_p[i] != '0' ) numNonZero++; printf( "numNonZero = %d\n", numNonZero );``````
@keskkiverto Sorry im not using vector at this moment and its a bit late to start.

@binarybob250
Ahh that looks like a good idea bob, very simple. I got wrapped up into the other complicated methods and got lost thank you for pulling me back.
If your integer array is not allowed to contain zero as an actual element then the size of the array is calculated the following way

int a[2002] = {};

int size = std::distance( std::begin( a ), std::find( std::begin( a ), std:;end( a ), 0 ) );
My overall goal is to get an average of length / by a number that is calculated by my function so say i get an a string of 2412324000000

Im comparing that string with another, everytime an index is different to the the index on the other string it ++1 for the editdistance so i need to then take that value editdistance and divide by the length of the string which is compared_p

@vald
I looked at that line before but could not get it to work before, will try again, maybe my syntax was wrong which is more likley the case, im tinkering at the moment, here is the snippet of code i am working on at the moment.

 ``1234567891011`` `````` double length = 0; for( int l=0; l < MAX_PROG_SIZE; l++ )// max prog overflow is value 2002. { if( compared_p[i] != 0 ) { length++; } } editdistance = (editdistance / length);``````

I keep getting this value when i export it to spread sheet.

-2147483648 for all the of them.

ps. I have to nip out to shops quickly.
Last edited on
I have understood nothing. On the one hand you are saying about an integer array. On the other hand you are saying about some strings. Can you use the human language that it would be clear what you want?!
Sorry the array is a char array made up of 1,2,3,4,5,6,7 my bad i should not of used string in any shape or form, I was in rush before i left for the shop.

char compared_p[MAX_PROG_SIZE] = {} ;

so that compared_p gets filled with numbers like so

535234120000000000000 to end 00 up to MAX_PROG_OVERFLOW "2002" length

I take 2 of these arrays that are different and compare the index's if the index is a number and is different to the other index in the other array it is +1 else if it is the same it is 0 and on to the next index untill all index's are compared.

Once i have the difference between "editdistance" the arrays, i then need to calculate the length of one of them which would be this bit here

"53523412" not the 0's

then i need to divide the difference of the 2 arrays between the length of the array compared_p something like below.

editdistance = (editdistance / length);

Hope that was a better explanation sorry for confusion

I will try your previous post vlad now that I am here.
Last edited on

What includes do you use for this

int size = std::distance( std::begin( a ), std::find( std::begin( a ), std:;end( a ), 0 ) );

having slight trouble getting it working.

+ is that suppose to be semi colon ;

std:;end?
I have understood nothing again except that you are dealing with two character arrays. Do you need to count how many elements of the two arrays are equal each other?
Yes I need to count the elements in one of the arrays, my function compares both the arrays but it makes them the same length by adding in extra index's so do not worry about that. This part of the function works fine.

I am just having problems trying to calculate the length of the one of the arrays's seen as they are now both the same length. I just want the length of one of the arrays, and then the i divide it by a value i get from comparing both of the arrays later.

Last edited on
Topic archived. No new replies allowed.