943,866 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4498
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 24th, 2009
0

How to count the elements of an array?

Expand Post »
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 ..
Similar Threads
Reputation Points: 6
Solved Threads: 0
Newbie Poster
super.mina is offline Offline
15 posts
since Apr 2009
Apr 24th, 2009
0

Re: How to count the elements of an array?

Do you mean to check how many elements there are in an array?
Reputation Points: 20
Solved Threads: 8
Junior Poster in Training
Dannyo329 is offline Offline
79 posts
since Apr 2008
Apr 24th, 2009
0

Re: How to count the elements of 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.
Reputation Points: 6
Solved Threads: 0
Newbie Poster
super.mina is offline Offline
15 posts
since Apr 2009
Apr 24th, 2009
0

Re: How to count the elements of an array?

C++ Syntax (Toggle Plain Text)
  1. int array[10]={1,5,6,2,4,5,7,8,5};
  2.  
  3. int n=0;
  4. int p;
  5. while (p!= '\0')
  6. {
  7. n++;
  8. p = array[n];
  9. }
  10.  
  11. cout << "There are "<<n-1 << " elements in the array.";
You shorten the code but its basically how you do it.
Last edited by Dannyo329; Apr 24th, 2009 at 5:05 am.
Reputation Points: 20
Solved Threads: 8
Junior Poster in Training
Dannyo329 is offline Offline
79 posts
since Apr 2008
Apr 24th, 2009
0

Re: How to count the elements of an array?

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

Re: How to count the elements of an array?

Edited the code, try again,
Reputation Points: 20
Solved Threads: 8
Junior Poster in Training
Dannyo329 is offline Offline
79 posts
since Apr 2008
Apr 24th, 2009
0

Re: How to count the elements of an array?

Click to Expand / Collapse  Quote originally posted by Dannyo329 ...
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?
Last edited by Nick Evan; Apr 24th, 2009 at 6:02 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Apr 24th, 2009
0

Re: How to count the elements of an array?

>>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.
Last edited by siddhant3s; Apr 24th, 2009 at 6:14 am.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Apr 24th, 2009
0

Re: How to count the elements of an array?

Click to Expand / Collapse  Quote originally posted by siddhant3s ...
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.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Apr 24th, 2009
0

Re: How to count the elements of an array?

If i am using a vector instead of an array , is there a way to count elements in it?
Reputation Points: 6
Solved Threads: 0
Newbie Poster
super.mina is offline Offline
15 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Unhandled Exception error
Next Thread in C++ Forum Timeline: Search Function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC