Thread: Newb question: Modifying Arrays

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    4

    Newb question: Modifying Arrays

    I have somewhat of a puzzle. Knowing C++ for a grand total of 3 days, I'm trying to make a console program that'll calculate the value of prime integers from 1 to 100. Unfortunately, I'm curious as to if you can add a value to an array when a program is running.

    For example: loop here has produced a result

    Code:
    {Loop here}
    -Finished result of previous loop-
    However, while the program is running, is it possible to move the product of the loop into an array, such as the following?

    Code:
    -Finished result-  *goes into* -Array one-
    *print contents of Array 1*
    Sorry for my lack of formal code. I'm mostly wondering whether the logic I presented is possible.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    An array's size is fixed at compile time. However, you can create an array that's large enough for your needs, and then only use a portion of it, with another variable to keep track of how many elements are in use. At run time, you can then expand this used portion of the array by incrementing this second variable, thus giving the effect of adding an element to the array.

    Alternatively, you can use dynamically allocated arrays, but that may be beyond your scope of learning at the moment. A generally even better option than dynamically allocated arrays is to use a container such as std::vector.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about arrays
    By TomButcher in forum C++ Programming
    Replies: 3
    Last Post: 09-02-2005, 09:27 AM
  2. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 08:24 PM
  3. basic question to Arrays
    By doneirik in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2005, 02:57 AM
  4. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  5. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM