Thread: minimum numbers to be entered into an array

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    minimum numbers to be entered into an array

    Is it possible to accept a minimum of ten numbers to be entered into an array? I know it's possible to accept a maximum of ten numbers to be entered into an array.

    Is this where vectors come in?

  2. #2
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    dont let the loop leave until ten are taken

    Code:
    int somearray[10];
    
    for (int i = 0; i < 10; i++)
    {
    cout << "Enter int: ";
    cin >> somearray[i];
    }

  3. #3
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Originally posted by RoD
    dont let the loop leave until ten are taken

    Code:
    int somearray[10];
    
    for (int i = 0; i < 10; i++)
    {
    cout << "Enter int: ";
    cin >> somearray[i];
    }
    That code doesn't look like it scans for a minimum of ten numbers; it looks like it's scanning for exactly ten numbers. What if I want to enter eleven numbers?

    I guess what I want is impossible to do with arrays...

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    for(int i=0; i<10; i++)
    {
       cin >> Array[i];
    }
    
    int Temp;
    int j = 10;
    do
    {
       cin >> temp;
       if(Temp != -1)
       {
          Array[j] = Temp;
          j++;
       }
    }while(Temp != -1)
    Assuming that the exit condition is the user enters -1.


    EDIT:

    Or even better:
    Code:
    int Temp;
    int i = 0;
    
    do
    {
       cin >> Temp;
       if(Temp != -1)
       {
          Array[i] = Temp;
          i++;
       }
    }while((Temp != -1) || (i < 10))
    Last edited by Magos; 04-05-2003 at 05:29 PM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    well, if you want to change the size, vectors come into play. you can use resize() if you need to.

    or you could also use a linked list and dynamically allocate memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing a pair of numbers to an array
    By wankel in forum C Programming
    Replies: 53
    Last Post: 06-22-2009, 05:27 PM
  2. Replies: 2
    Last Post: 02-08-2009, 09:26 PM
  3. question: reading numbers into an array
    By Lince in forum C Programming
    Replies: 15
    Last Post: 11-15-2006, 03:41 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM