Newb question: Modifying Arrays

This is a discussion on Newb question: Modifying Arrays within the C++ Programming forums, part of the General Programming Boards category; I have somewhat of a puzzle. Knowing C++ for a grand total of 3 days, I'm trying to make a ...

  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
    21,711
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 07:24 PM
  3. basic question to Arrays
    By doneirik in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2005, 01: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

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