If i have an array like that
int x[100]={1,0,3};

so only i have 3 elements and the rest of the array is zero, how can i count the number of elements in a larger one ..

Recommended Answers

All 18 Replies

Do you mean to check how many elements there are in an array?

I want to know how many elements in the array, not the size of the array...
E.g:

int x[1000]={1,22,3,54,7,89,1,0,0,1,2}
Number of elements = 11

is there any possible way to that.

int array[10]={1,5,6,2,4,5,7,8,5};

    int n=0;
    int p;
    while (p!= '\0')
    {
                n++;
                p = array[n];
    }    

    cout << "There are "<<n-1 << " elements in the array.";

You shorten the code but its basically how you do it.

This isn't working when i have for example the following array:

int x[100]={1,2,3,0,3,5,4,1};
The actual number of elements is 8,
but he will display 4

Edited the code, try again,

Edited the code, try again,

It will still fail. You are using P, but it's not initialized to anything.
Also 'n' will start at element 1, not 0
Also '\0' is the termination-character for CHAR- arrays, not INT.

@OP:
You could initialize your entire array to -1 if you only need positive numbers and then check when the first value is < 0, that'll give you the number of ints.
Or you could use a counter to keep track of the number of ints.
Or you could use a vector<int>

Could you tell us why you need this functionality?

>>Edited the code, try again,
Sorry, OP is right. x[100]={1,2,3,0,3,5,4,1}; will print 2 elements

To the OP:
You cannot !. Yes you cannot. However surprise it may bring to you, the truth is you really you cannot.
Thats why most of functions dealing with int array often asks you to enter the length of the array as one of the argument.
You will have to track the length of the array yourself.

Edit: Niek, I don't know why I couldn't see you editing, but the fact is that my "viewing list" at the bottom of page told me that no one was replying to the thread while I was.

Edit: Niek, I don't know why I couldn't see you editing, but the fact is that my "viewing list" at the bottom of page told me that no one was replying to the thread while I was.

I've set my profile to 'invisible', so no-one can see what I'm doing here ;). I've done this because I got a lot of "helpz me pleaaasse!!!11oneone!!" PM's when people saw that I was looking at a thread. I didn't like getting all those PM's begging for help, so I selected 'invisible mode' and it helped quite a bit.

If i am using a vector instead of an array , is there a way to count elements in it?

yes:

std::vector<int> my_vec;
my_vec.push_back(1);
my_vec.push_back(2312);
std::cout << "size of vector is : " << my_vec.size();

Hi there!
Actually, I don not know much about C++...However,i know that there is a good operator "count($array)" in PHP which can do that, maybe it exists in C too...:)

Hi there!
Actually, I don not know much about C++...However,i know that there is a good operator "count($array)" in PHP which can do that, maybe it exists in C too...:)

First: C++ and C or two different languages
Second: No, such a function does not exist for every kind of array. In the case of char-array's , you could use strlen() which is kind of the same function as count() in PHP. For other kinds of arrays you'll have to come up with an alternative solution as previously mentioned

And what if you consider the following ?

int array[5]={1,23,45,7,8};
int element_count = sizeof(array)/sizeof(int); /* now this variable holds the value '5' ... */

Edit:: This won't work with multidimensional arrays :) ...

>>And what if you consider the following ?
Tell me if this work:

int array[5]={1,23};
//this var will still tell you 5 while the OP wants 2
int element_count = sizeof(array)/sizeof(int);

>>And what if you consider the following ?
Tell me if this work:

int array[5]={1,23};
//this var will still tell you 5 while the OP wants 2
int element_count = sizeof(array)/sizeof(int);

Oh, he meant it like that, he only wants to count the elements which aren't zero I assume then ?

Edit:: You can create a simple object which uses methods to add and remove data from the array and which keeps a counter of elements in the array, and If I'm not wrong you can also use the STL vector container ...

>>Oh, he meant it like that, he only wants to count the elements which aren't zero I assume then ?
No Tux, He wants to magically find out how many elements he changed in the array.
(Read the whole thread, I know it is not easy though. This is the worst part of Usenet ;))

Regrettably the OP question (and all the thread ;)) is a product of a misunderstanding of an array semantics in C++ (and C). If we have an array which is defined as int x[100] = {1,0,3}; then we have an array of 100 (one hundred) elements. The leading three elements have initial values {1, 0, 3} and other 97 elements have values 0 (zero).

Please, feel the difference between array element and array element value.

So the last phrase of OP was started from the false conclusion:

so only i have 3 elements...

Nope, you have 100 elements in this array and no need to count them in run-time. An array in C++ is a compound object of a fixed size defined at the moment of this object allocation.

Incidentally, PHP arrays have absolutely different semantics: they are chimeras of C++ STL std::vector and std::map (that's about count($...) in PHP)...

commented: Exactly +5

Rep++ for ArkM :P !!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.