Thread: Accessing an array dynamically....

  1. #1
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68

    Accessing an array dynamically....

    I want to access the elements of my array dynamically. So far I've only figured out how to do this manually. if I tried it like this my code would work but there should be a better way right?

    Sorry if I appear green, I just have holes due to being self taught. Thanks all who help.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What do you mean by "access the elements of my array dynamically" and "manually"?
    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

  3. #3
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68
    This is how I would do it manually.

    int XA [4];
    XA[2] = 3;
    int Z = XA[2];
    I need to learn how to access the array space 1 by 1 automatically.

    int
    UPDATE! As of 10/6/2014

    https://www.dropbox.com/s/2sj6qwpfbb...t%201.zip?dl=0
    Just find the application file and double click it. Controls are (Arrow keys) (Z) (Z + arrow key) (Spacebar)
    Don't play this crappy update. Wait for the newest one which is far more impressive.

    Official Sonic character poll hosted by some guy at Sega..... Sega of America. Vote for blaze because she OP.
    http://blogs.sega.com/2015/08/28/the...down-heats-up/

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah. Have you learned how to use loops?
    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

  5. #5
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68
    I only know how to use 3 for while and do while.
    UPDATE! As of 10/6/2014

    https://www.dropbox.com/s/2sj6qwpfbb...t%201.zip?dl=0
    Just find the application file and double click it. Controls are (Arrow keys) (Z) (Z + arrow key) (Spacebar)
    Don't play this crappy update. Wait for the newest one which is far more impressive.

    Official Sonic character poll hosted by some guy at Sega..... Sega of America. Vote for blaze because she OP.
    http://blogs.sega.com/2015/08/28/the...down-heats-up/

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Any of them will work, though a for loop would often be most appropriate when working with an array since it is common to loop from index 0 to the last valid index of the array.
    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

  7. #7
    Registered User immy's Avatar
    Join Date
    Apr 2014
    Posts
    33
    If you want any further help on this topic you can add me on skype if you like.

    skype: imohamme5

  8. #8
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68
    Could I get a small example how this would be done?
    UPDATE! As of 10/6/2014

    https://www.dropbox.com/s/2sj6qwpfbb...t%201.zip?dl=0
    Just find the application file and double click it. Controls are (Arrow keys) (Z) (Z + arrow key) (Spacebar)
    Don't play this crappy update. Wait for the newest one which is far more impressive.

    Official Sonic character poll hosted by some guy at Sega..... Sega of America. Vote for blaze because she OP.
    http://blogs.sega.com/2015/08/28/the...down-heats-up/

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by immy
    If you want any further help on this topic you can add me on skype if you like.
    You are certainly free to do so, but I note that assistance provided via this forum is subject to peer review, hence mistakes or omissions in help provided are more likely to be corrected.

    Quote Originally Posted by LAURENT*
    Could I get a small example how this would be done?
    Code:
    #include <iostream>
    
    int main()
    {
        int numbers[] = {1, 1, 2, 3, 5, 8};
        const std::size_t numbers_size = sizeof(numbers) / sizeof(numbers[0]);
        for (std::size_t i = 0; i < numbers_size; ++i)
        {
            std::cout << numbers[i] << std::endl;
        }
    }
    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

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I also note that in C++11, you can also do:
    Code:
    #include <iostream>
     
    int main()
    {
        int numbers[] = {1, 1, 2, 3, 5, 8};
        for (auto && e : numbers)
            std::cout << e << std::endl;
    }
    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.

  11. #11
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68
    Wow..... I feel dumb for not thinking of doing that. Alright thanks I'll ask more questions as I see fix.
    UPDATE! As of 10/6/2014

    https://www.dropbox.com/s/2sj6qwpfbb...t%201.zip?dl=0
    Just find the application file and double click it. Controls are (Arrow keys) (Z) (Z + arrow key) (Spacebar)
    Don't play this crappy update. Wait for the newest one which is far more impressive.

    Official Sonic character poll hosted by some guy at Sega..... Sega of America. Vote for blaze because she OP.
    http://blogs.sega.com/2015/08/28/the...down-heats-up/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamically allocating an array
    By Lina_inverse in forum C Programming
    Replies: 6
    Last Post: 10-07-2012, 06:06 AM
  2. dynamically allocating a 2d array
    By ali.franco95 in forum C Programming
    Replies: 3
    Last Post: 10-18-2011, 09:28 AM
  3. Dynamically Allocated Array
    By chris4 in forum C Programming
    Replies: 9
    Last Post: 05-06-2011, 10:01 AM
  4. Dynamically allocating 3D array
    By ssharish2005 in forum C Programming
    Replies: 8
    Last Post: 02-26-2008, 04:07 PM
  5. Dynamically Allocating a 2D Array.
    By LightsOut06 in forum C Programming
    Replies: 17
    Last Post: 10-09-2005, 12:55 AM