Thread: how to scan an int dinamic 2d array..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    how to scan an int dinamic 2d array..

    i dont know when each row ends

    its not a string
    that i could say stop when you see \0

    can i find the length of row 0 by

    sizeof(arr[0])/sizeof(int)


    ??

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> can i find the length of row 0 by

    Not if it's a dynamically allocated array. In that case, you'll just need to store the size somewhere.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how to find the length of a column 0
    this is for row
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    void main()
    {
    	int arr[10][10]={0};
    	int t=sizeof(arr[0])/sizeof(int);
    	printf("%d",t);
    }

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    can i find the length of row 0 by

    sizeof(arr[0])/sizeof(int)
    That only works if your array is declared with a static size like:
    Code:
    int a[5][5];
    It will not work if you have used pointers to dynamically allocate the size of the rows.

    EDIT: I guess I should have refreshed the page before posting, Sebastiani beat me to it.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The column is
    Code:
    sizeof(arr) / (t * sizeof(arr[0][0]);
    As stated above, this doesn't at all work for dynamic arrays, since the sizeof only works for variables that are known size at the time of compilation. Dynamic arrays are specifically used when you do NOT know the size at time of compilation.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ahh so how to do this with dinamic allocation
    ??

    (suppose i am not given the size and i need to find it)

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It also doesn't work if you're passing the array to a function.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> how to find the length of a column 0

    Hint: if sizeof(arr[0]) gives you the size of a single row and sizeof(a) gives you the size of the entire data structure, then the column length would be the result of some arithmetic operation between the two.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> ahh so how to do this with dinamic allocation

    Wasn't that already answered?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    no it havent been
    it just been said that my method doesnt work on dinamic array

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    ahh so how to do this with dinamic allocation
    ??

    (suppose i am not given the size and i need to find it)
    You CAN NOT find the size of a dynamic allocation in any generic/portable/reliable way. Of course, you can have markers to mark the end, but that's not generic, because some particular value (such as zero, -1, -32768 or some other value) is then "invalid" for the normal array values.

    So, no, you can't do that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Here is how to do it for dynamic arrays:
    Code:
    int** a;
    a = malloc(COLUMN_SIZE * sizeof(int*));
    for(i = 0; i < COLUMN_SIZE; i++)
        a[i] = malloc(ROW_SIZE * sizeof(int));
    
    printf("column size is: %d\n", COLUMN_SIZE);
    printf("row size is: %d\n", ROW_SIZE);
    See what I did there?

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i need to find some number in a cell of dinamic 2d array

    how to do that
    if i dont know where ends each row
    and how many row are there
    ?

  14. #14
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by transgalactic2 View Post
    i need to find some number in a cell of dinamic 2d array

    how to do that
    if i dont know where ends each row
    and how many row are there
    ?
    Now you're just trolling us... right?

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> no it havent been.
    >> it just been said that my method doesnt work on dinamic array

    Quote Originally Posted by Sebastiani View Post
    >> can i find the length of row 0 by

    Not if it's a dynamically allocated array. In that case, you'll just need to store the size somewhere.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM