Numbers into an array

This is a discussion on Numbers into an array within the C++ Programming forums, part of the General Programming Boards category; Could anyone please tell me how I can store any number of values into an array? thanks...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    Numbers into an array

    Could anyone please tell me how I can store any number of values into an array?

    thanks

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    An array, when created, has a fixed size.

    Try using a linked list, or a vector by #include-ing <vector>.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    Yes, but for this assignment the user must be able to enter any number of values into an array, and we haven't covered linked lists or vectors yet.

    Would my teacher really mean to store 128 elements in the array?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    int myarray[128];
    myarray[0] = 1;

    If you're having trouble, post your code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Or post the exact problem requirements. For better or worse, programming involves a precise understanding of the problem. For example, the problem may be to allow the user to enter as many integer numbers as they want up to a total of 128 integers or it may be to allow the user to enter as many integer numbers as they want without an upper limit. You can write code to handle either problem, but the code you use will be heavily dependent on which problem it is.

  6. #6
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    if you want to store an unknown number of values in an array you could dynamically allocate the array.

    Code:
     
    cout << "How big will your array be?\n";
    int x;
    cin >> x;
    
    int * array = new int[x];
    
    //...
    
    delete [] array;
    or just make an array big enough to hold a whole lot of values, so that the user will be unlikely to fill it all the way up.
    I came up with a cool phrase to put down here, but i forgot it...

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    ^ some compilers complain becaue it's not a constant...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    of course its not a constant... thats the whole point behind dynamic memory...

    any compiler that complains about dynamically allocated arrays is not a compiler worth using
    I came up with a cool phrase to put down here, but i forgot it...

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    bloodshed dev c++ V 4.8.9.0 complains... and it's worth using...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    thats curious... because im looking at a fully compiled program written in Dev C++ that involves nothing but a single dynamic array...

    is it possible that youve never used dynamic memory before and you simply dont know that it works and uses standard C++ keywords that work on any compiler?
    I came up with a cool phrase to put down here, but i forgot it...

  11. #11
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    maybe it's just letting you know that you can't use 'x' and be certain it still contains the size of the array you allocated?

    Your warning level might be at the highest setting? Seems like a weird warning to give, like ... said.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    nvm... I just tried it... I must be getting it confused with something else that I needed a const for... I cant' remember what it was now though...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Probably you tried this:
    int array[x];

    That is what will choke your compiler, most likely
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To Find Largest Consecutive Sum Of Numbers In 1D Array
    By chottachatri in forum C Programming
    Replies: 22
    Last Post: 07-10-2011, 02:43 PM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. getting numbers in an array
    By ct26torr in forum C Programming
    Replies: 6
    Last Post: 03-04-2003, 10:31 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

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