954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to count the elements of an array?

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 ..

super.mina
Newbie Poster
15 posts since Apr 2009
Reputation Points: 6
Solved Threads: 0
 

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

Dannyo329
Junior Poster in Training
79 posts since Apr 2008
Reputation Points: 20
Solved Threads: 8
 

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.

super.mina
Newbie Poster
15 posts since Apr 2009
Reputation Points: 6
Solved Threads: 0
 
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.

Dannyo329
Junior Poster in Training
79 posts since Apr 2008
Reputation Points: 20
Solved Threads: 8
 

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

super.mina
Newbie Poster
15 posts since Apr 2009
Reputation Points: 6
Solved Threads: 0
 

Edited the code, try again,

Dannyo329
Junior Poster in Training
79 posts since Apr 2008
Reputation Points: 20
Solved Threads: 8
 
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

Could you tell us why you need this functionality?

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

>>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.

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 
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.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

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

super.mina
Newbie Poster
15 posts since Apr 2009
Reputation Points: 6
Solved Threads: 0
 

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();
Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

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...:)

Acute
Junior Poster
129 posts since Mar 2009
Reputation Points: 8
Solved Threads: 3
 
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

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

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 :) ...

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

>>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);
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

>>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 ...

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

>>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 ;))

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

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 of100 (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 have100 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)...

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

Rep++ for ArkM :P !!

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You