Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 15th, 2006, 12:14 PM   #1
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3 kruptof is on a distinguished road
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:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Nov 15th, 2006, 2:49 PM   #2
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 244
Rep Power: 3 Seif is on a distinguished road
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
Seif is offline   Reply With Quote
Old Nov 15th, 2006, 3:04 PM   #3
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>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.
Narue is offline   Reply With Quote
Old Nov 15th, 2006, 4:29 PM   #4
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3 kruptof is on a distinguished road
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:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:56 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC