Number of Arrays

This is a discussion on Number of Arrays within the C++ Programming forums, part of the General Programming Boards category; How would you go about finding the size of an array? \...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    1

    Post Number of Arrays

    How would you go about finding the size of an array?

    \

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,411
    Assuming this is an adhoc question a try-catch block will do it.

    Code:
    size_t array_size = 0;
    try {
        while(true, myarray[array_size++]);
    }
    catch(const std::runtime_error &err) {
        ; // do nothing
    }
    Of course. This is purely for fun. An array dimension should be a const object (or a non const one if it is a dynamic array) and should be in scope wherever the array is used.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by haoli12345
    How would you go about finding the size of an array?

    \
    Use std::vector instead.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you are in the same scope as the array was created and it is a static array, you can divide the sizeof the array by the sizeof one element in the array.

    Otherwise you should keep track of it yourself. Of course, in C++, you should generally be using vector or some other container that keeps track of its own size.

    Mario F.'s fun attempt won't actually work reliably, since you can read past the bounds of the array without generating an error in some cases. That behavior is undefined.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,411
    You are right Daved.

    It is completely wrong. My mind is somewhere else. an overflow doesn't throw an exception. Just consider the last paragraph to be true. Anything before that is... ridiculous.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Furthermore, I think you had your comma beckwords:
    "while(true, myarray[array_size++]);".
    The way it is the true comma is useless. It would effectively be a tool for finding the lenght of a null pointing string. Perhalps you wanted this infinate loop:
    "while(myarray[array_size++],true);".
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    BTW, the C++ equivalent of a statically sized array is std::tr1::array. Boost should have an implementation, as does Dinkumware I think.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,411
    Quote Originally Posted by King Mir
    Furthermore, I think you had your comma beckwords:
    "while(true, myarray[array_size++]);".
    The way it is the true comma is useless. It would effectively be a tool for finding the lenght of a null pointing string. Perhalps you wanted this infinate loop:
    "while(myarray[array_size++],true);".
    Nah. The code was correct. The idea was to do an infinite loop. The exception would be generated by the overflow.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    King Mir's point was that the result of the comma expression is the rightmost statement.

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,411
    Duh... i'm just too tired. It's 8am and I had no sleep tonight. Sorry about the whole posting on this thread
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Furthermore, I think you had your comma beckwords:
    Once I talked to an English guy (in a multiplayer game), who told that he can't spell the word backwards (he wrote it like "backwurds" or "buckwerds").
    Is this word really so difficult?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Quote Originally Posted by maxorator
    Once I talked to an English guy (in a multiplayer game), who told that he can't spell the word backwards (he wrote it like "backwurds" or "buckwerds").
    Is this word really so difficult?
    If it were just backwords I'd be fine, but you probably want me to spell everything right.

    I'm horrible at spelling, epecially when I'm typing fast. I'm sure this post is a tesiment to that.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    about finding the size of an array... I agree with Mario F., an array dimesion should be a const int, but if you had a situation where you were creating an array on the fly and filling it with information as you do so, it would make sense to me to use a counter variable to keep track of the size as you go. What are you trying to do?

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    This thread is 4 days old, Miles.

    What are you trying to do?
    What is who trying to do? (Hey... it rhymes!)
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. Number Guessing
    By blacknapalm in forum C Programming
    Replies: 2
    Last Post: 10-01-2008, 01:48 AM
  3. Stone Age Rumble
    By KONI in forum Contests Board
    Replies: 30
    Last Post: 04-02-2007, 09:53 PM
  4. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  5. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21