How to count the elements of an array?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 12
Reputation: super.mina has a little shameless behaviour in the past 
Solved Threads: 0
super.mina super.mina is offline Offline
Newbie Poster

How to count the elements of an array?

 
0
  #1
Apr 24th, 2009
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 ..
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 59
Reputation: Dannyo329 is an unknown quantity at this point 
Solved Threads: 6
Dannyo329's Avatar
Dannyo329 Dannyo329 is offline Offline
Junior Poster in Training

Re: How to count the elements of an array?

 
0
  #2
Apr 24th, 2009
Do you mean to check how many elements there are in an array?
C programmers will get smashed by C++ programmers.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 12
Reputation: super.mina has a little shameless behaviour in the past 
Solved Threads: 0
super.mina super.mina is offline Offline
Newbie Poster

Re: How to count the elements of an array?

 
0
  #3
Apr 24th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 59
Reputation: Dannyo329 is an unknown quantity at this point 
Solved Threads: 6
Dannyo329's Avatar
Dannyo329 Dannyo329 is offline Offline
Junior Poster in Training

Re: How to count the elements of an array?

 
0
  #4
Apr 24th, 2009
  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.
C programmers will get smashed by C++ programmers.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 12
Reputation: super.mina has a little shameless behaviour in the past 
Solved Threads: 0
super.mina super.mina is offline Offline
Newbie Poster

Re: How to count the elements of an array?

 
0
  #5
Apr 24th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 59
Reputation: Dannyo329 is an unknown quantity at this point 
Solved Threads: 6
Dannyo329's Avatar
Dannyo329 Dannyo329 is offline Offline
Junior Poster in Training

Re: How to count the elements of an array?

 
0
  #6
Apr 24th, 2009
Edited the code, try again,
C programmers will get smashed by C++ programmers.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,958
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 306
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Cenosillicaphobiac

Re: How to count the elements of an array?

 
0
  #7
Apr 24th, 2009
Originally Posted by Dannyo329 View Post
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 niek_e; Apr 24th, 2009 at 6:02 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: How to count the elements of an array?

 
0
  #8
Apr 24th, 2009
>>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.
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,958
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 306
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Cenosillicaphobiac

Re: How to count the elements of an array?

 
0
  #9
Apr 24th, 2009
Originally Posted by siddhant3s View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 12
Reputation: super.mina has a little shameless behaviour in the past 
Solved Threads: 0
super.mina super.mina is offline Offline
Newbie Poster

Re: How to count the elements of an array?

 
0
  #10
Apr 24th, 2009
If i am using a vector instead of an array , is there a way to count elements in it?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC