2 Replies - 518 Views - Last Post: 04 December 2010 - 06:59 AM Rate Topic: -----

#1 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

End of an Integer Array

Posted 04 December 2010 - 06:33 AM

How will I determine the end of an integer array?

i tried something like this:

int numbers[5];
int i = 0;
while(numbers[i] != '\0')
{

}


can anyone help me pls
Is This A Good Question/Topic? 0
  • +

Replies To: End of an Integer Array

#2 amir_ju  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 82
  • Joined: 20-June 08

Re: End of an Integer Array

Posted 04 December 2010 - 06:52 AM

the notation you've used is totally wrong. Because you can't be sure that the next element in memory is '\0' unless you have set that to zero like the string case. But this doesn't make sense, because in the strings '\0' is a special character not occurring at any position. But in an array,it's not clear that that zero indicates end of the array, or just a zero element. If you want to iterate over the array elements, a for from zero up to sizeof(numbers)/sizeof(int) solves the problem.
int numbers[6];
cout <<(sizeof(numbers)/sizeof(int)) << endl;
for(int i=0; i<sizeof(numbers)/sizeof(int); i++){
}


But of course, the most reasonable way is to store the size of array in a number and use it for iteration.
Was This Post Helpful? 2
  • +
  • -

#3 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: End of an Integer Array

Posted 04 December 2010 - 06:59 AM

View Postissyl, on 04 December 2010 - 10:33 PM, said:

How will I determine the end of an integer array?

That question means nothing as asked.
An array does not have an 'end'.
An array has boundaries.

Since you declare your array as follows
int numbers[5];
then the boundaries are element 0 and element 4.

Try and rethink your question in terms at what you have learned in class.
What is you are actually trying to do?
How does that requirement fit in with what you have been taught?

EDIT
For those interested in the history behind this question have a look here
http://www.dreaminco...ray-of-numbers/

This post has been edited by janotte: 04 December 2010 - 07:26 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1