| | |
How to count the elements of an array?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
C++ Syntax (Toggle Plain Text)
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.";
Last edited by Dannyo329; Apr 24th, 2009 at 5:05 am.
C programmers will get smashed by C++ programmers.
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?
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.
>>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.
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
(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
•
•
•
•
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 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. ![]() |
Similar Threads
- Finding No. of elements in an Array (C)
- count elements in array type (blah) (C++)
- C++ Array help needed (C++)
- bubble sort function w/ array (C++)
- Searches only returning results for certain strings (PHP)
- array from StringTokenizer (Java)
Other Threads in the C++ Forum
- Previous Thread: Unhandled Exception error
- Next Thread: Search Function
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference simple string strings studio system temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






