Thread: arrays

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    18

    arrays

    How do I turn a two dimensional array into a one dimensional array?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You don't have to. Hint: cast.
    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
    Registered User
    Join Date
    Nov 2008
    Posts
    18
    Here is what I have I dont know what to put in the for loops
    int val[height][width]
    int n,m;

    int main()
    {

    for( )
    for( )

    {val[]=(n+1)*(m+1);

    }
    return 0;
    {

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What are you trying to compute, here? I see no reason why you would expect, or for that matter want, val as a one-dimensional array.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    18
    Its just a problem for school I need to find out how to take a two dimensional array and turn it in to a one dimensional array

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Here is what I have I dont know what to put in the for loops

    You put valid C statements in them and then check the output, for starters. But if you're too lazy to pick up a book and actually learn how to do that, well...
    Last edited by Sebastiani; 11-30-2008 at 08:15 PM. Reason: tone
    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;
    }

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    18
    Here is what the two dimensional array code looks like, I need to find out what goes in the for loops in the above code to turn it in to a one dimensional array.
    int val[height][width]
    int n,m;

    int main()
    {
    for(n=0; n<height;n++)
    for(m=0;m<width;m++)

    {val[n][m]=(n+1)*(m+1);
    }

    return 0;
    }
    Last edited by john5754; 11-30-2008 at 08:23 PM.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Ok, the array contents are *guaranteed* to be contiguous, so a simple cast is *really* all that is needed (ie: ** -> * ). Can you manage that?
    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
    Registered User
    Join Date
    Nov 2008
    Posts
    18
    what do you mean by cast

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    eg:

    Code:
    double d = 2.5;
    int i = int( d ); // c-style
    // - or -
    int j = static_cast< int >( d ); // c++-style
    It works with pointers, too, of course.
    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;
    }

  11. #11
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    Quote Originally Posted by Sebastiani View Post
    Ok, the array contents are *guaranteed* to be contiguous, so a simple cast is *really* all that is needed (ie: ** -> * ). Can you manage that?
    Is that true everywhere? For example, dynamically allocated arrays, are they also guaranteed to be contiguous?

    My solution would have looked something like this

    Code:
    int old2darray[width][height];
    int new1darray[width * height];
    
    for (blah)
    {
       for(blah)
       {
          move data from oldarray to new array
       }
    }
    However, casting would be a much more efficient way to move the data. (Ok, you aren't really moving data)

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Cogman View Post
    Is that true everywhere? For example, dynamically allocated arrays, are they also guaranteed to be contiguous?
    Dynamic arrays are not guaranteed to be contiguous. (They almost always aren't, I would guess, if malloc/calloc writes extra bits to the allocated memory.)

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tabstop
    Dynamic arrays are not guaranteed to be contiguous.
    Just in case there is confusion, note that what tabstop means is that consecutively (or otherwise) allocated dynamic arrays are not guaranteed to be contiguous. However, like fixed size arrays, the contents of each dynamic array are contiguous.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by tabstop View Post
    Dynamic arrays are not guaranteed to be contiguous. (They almost always aren't, I would guess, if malloc/calloc writes extra bits to the allocated memory.)
    That's new/delete for you.
    But anyone in their right mind wouldn't try to make dynamically allocated arrays using new and delete in C... if they can avoid it!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Nov 2008
    Posts
    18
    Here is what I got, It seems to work when I run it. See any problems?
    # define width 5
    # define height 3

    int jimmy[Height * Width]
    int n,m;
    int main()
    {
    for(n=0; n<height ; n++)
    for(m=0; m<width; m++)
    { jimmy[n]=(n+1)*(m+1);
    }
    return 0;
    {

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM