Results 1 to 7 of 7

Thread: limit of array

  1. #1

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919

    limit of array

    Code:
    	int m[];
    	for(int i=0;i<=m[i];i++);
    	
    	int i=0;
    	while(m[i] != '\n'){
    		...
    		i++;
    	}
    which one is correct to scan till the ubound of hte array? i know VB has UBound, but i cant seem to figure out how to do it in C++

    thanks
    -nabeel
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  2. #2
    Lively Member
    Join Date
    Jan 2000
    Posts
    123
    Why not just keep track of the elements in the array via some counter... then the loop is not neccessary...

  3. #3

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    but i dont wanna have uneccessary variables.
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  4. #4

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    and after you use 2 or 3 arrays, the counters get kinda hard and messy to keep track of
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  5. #5
    Zaei
    Guest
    Neither is correct. To first will loop until it finds a value in the array that is greater then or equal to i, while the other just loops until it finds (int)'\n' in the array. You have to understand that C++ arrays are not VB arrays. If you create an array like:
    Code:
    int x[10]
    you will never have more or less then 10 elements in that array.

    If you need a dynamic array, use an STL vector (#include <vector>, std::vector<int> x.

    Z.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Arrays are C++ weak point (sometimes strong point) as they contain no information about their bounds, which undermines object orientation pretty much. Zaei's suggesting the vector as a implementation of a dynamical array, in VB dynamical arrays are declared without size, while in C++ you need your own implementation for the sake of flexibility.

    You can obtain the size of (only) dynamically allocated memory with _msize() and so you could retrieve the size for any dynamical array with:
    template<class T>int DynArraySize(T* a){return _msize(a)/sizeof( T);};
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    thanks

    any tuts on using vectors?
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width