Array

Status
Not open for further replies.

Mohan Giri

In Runtime
Messages
144
Hi.
I have one doubt. We are initiaising the array withoust specifying the size.
ie

int array[]={1,2,5,4};

Now we want to identify how many elements are in the array. Whats is the procedure?
 
Oh, I thought it was just me, and that I should've known what language, I'm a newbie, but I've read quite a bit.

-SkyHi
 
In attempt to save the time for you...

In java it would be .length(), and I believe it is the same in c++ (do not know for sure).
 
I dont want to use any function. Manually I want to find using loop structure. Is it possible?
 
Find what using the loop structure?

Before you wanted to make an array, find the size, now make a size with an array?

What are you talking about?

If you are trying to access (whether it is store, delete, or read) values in a specific index of the array, then you probably should use a loop.



for (int i = 0; i < array.length(); i++)
{
< code goes here>;
valueAtIndex = array;
< code goes here>;
}

That way each time the array runs, you can do whatever it is you want with the value at the index of 'i' in the array.

If you are just trying to get ONE value, then you can just put the index you want and assign it to another variable.
 
If you want to find the length you'll need to scan through the entire array

Code:
int arraylength(int * x[]){
   int i;
   int len = 0;
   for (i = 0; array[i] != 0; i++)
      len++;
   return len;
}
 
Status
Not open for further replies.
Back
Top Bottom