Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

An array question.

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
howdy,

-I am declaring an array with 100 index.->Data[100];
-Then I initialize all to zero.
-like below.
for(int count;count<100;count++)
Data[count]=0;

-Next Im going to use only 20 index space in the array Data[100]; And the rest of the value would be zero sinc I intitialize to zero.

***My question to you experts is, is there a function or
an easy way to have C++ count how many index are use.
For example we know I only use 20 index space.So it there
is a way then C++ will say,&quot;Oh you use 20 index space&quot;.
I hope you can understand my question.And thanks in advance.
 
Can't think of a predefined function, but there's an easier way to initialize.
Check out ZeroMemory, and FillMemory .

I would just use a member variable to hold the current position.

A little more overhead:
You could use one of the CArray type classes, then you have all the methods associated with them, like:
CArray GetSize(), GetUpperBound().
 
memset would be another option to set the memory. as for checking and finding the first index that is zero, a simple function adapted from bsearch could be used.

int getLastIndex(int* ptr, int numberOfElements)
{
int done = 0;
int index = numberOfElements/2;
while(!done)
{
if( *(ptr +index) )
index += (numberOfElements-index)/2;
else
index -= (numberOfElements-index)/2;

// double check my logic above

if(!index) return 0;

if(!(*ptr) && *(ptr-1))
return index;
}
}

The idea is there, tho the logic is not yet tested. Hope it helps

Matt
 
An exotic approach that just occured to me would be scanning the contents of the array with a 4 byte pointer and perform logical and's on the memory values found.
Zyrenthian's code above could be slightly changed to take this approach.
[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top