![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3
![]() |
Finding n element of array
is it possible to know how many elements in an array by just having access to the first elemnt of the array. For example the following code sorts through an array (int) into order, but at the current moment the sorting function relys on the caller passing it the number of elements, so i was just wondering if this can be done by just having access to the first element. thanks
void sort_array(int* backup,int n)
{
int * point=backup;
for(int i=0;i<n;i++)
{
point=backup;
for(int j=i;j<n;j++)
{
if(*backup >= *point)
{
int temp = *backup;
*backup = *point;
*point=temp;
}
point++;
}
backup++;
}
}
void main(void)
{
int lst[11]={7,6,3,4,9,18,1,5,2,2};
int array_size=(sizeof(lst)/4)-1;
sort_array(lst,array_size);
for(int i=0;i<10;i++)
{
cout<<lst[i]<<endl;
}
}
__________________
Quote:
|
|
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 244
Rep Power: 3
![]() |
Not really, If i were you i'd either use a vector instead of an array. However if you wanted to be dirty about it, you can use exception handling to catch when the end of an array is over reached
|
|
|
|
|
|
#3 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>is it possible to know how many elements in an array by just having access to the first elemnt of the array.
No, not in a generic way. You need some method of finding the end of the array, whether it be with an item count or a sentinel, or some other trick. >but at the current moment the sorting function relys on the caller passing it the number of elements What's wrong with that? The caller is in a much better position to know how many elements are in the array than your function. It's not like the argument has a prohibitive overhead either. >you can use exception handling to catch when the end of an array is over reached And what standard exception does an out of bounds access throw in C++?
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#4 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3
![]() |
there is nothing with the caller providing the sentinel but i was just wonedring if it was really possible.........well atleast i know the answer know...thanks
__________________
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| acessing array element | edemkay | Assembly | 1 | Apr 5th, 2006 7:05 AM |
| acessing array element | edemkay | Assembly | 0 | Apr 5th, 2006 2:24 AM |
| changing size of an array | Eric the Red | Java | 3 | Apr 3rd, 2006 9:19 PM |
| finding primes in an array | victor_shade | Java | 4 | Oct 27th, 2005 6:48 PM |
| Installing IPB 2.03 | bh4575 | Other Web Development Languages | 0 | Apr 23rd, 2005 3:36 AM |