coping elements not in array

This is a discussion on coping elements not in array within the C++ Programming forums, part of the General Programming Boards category; If the user is given an option of inputing a certain amount of int elements (MAX being 9) and then ...

  1. #1
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61

    coping elements not in array

    If the user is given an option of inputing a certain amount of int elements (MAX being 9) and then inputs those elements, how can I copy the elements he did NOT put into the array into another array?

    For example,
    He/She enters 3 then inputs 1, 2, 3

    4, 5, 6, 7, 8, 9 is copied into another array.

    or

    they enter 2 then input 1, 3

    2, 4, 5, 6, 7, 8, 9 is copied into another array.


    I've tried to use two for loops to accomplish this but I can't figure out the logic. Because depending the users input it seems the logic (the indexes) have to change...

    any ideas?

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    I'm confused by your question: are you saying that you have an array of 9 integers in your program, and you want a user to be able to select which values are not copied from the first array to a second array?

    Code:
    Array 1
    
       0    1    2   3   4   5    6    7    8
    -------------------------------------------
    | 13 | 27 | 16 | 2 | 9 | 7 | 12 | 66 | 42 |
    -------------------------------------------
    
    User selects values 0, 4 and 5
    
    Array 2 becomes
    
       0    1   2    3    4    5
    ------------------------------
    | 27 | 16 | 2 | 12 | 66 | 42 |
    ------------------------------
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    If Richie is on the right track, then if you have an array of all the possible items, first, copy as much as you can from the array until you match an excluded item. Then, adjust your indices to skip that element in the array your copying from, but don't advance your position in the array you're copying to.

    Keep going until you've passed every excluded item.

    Then just copy the rest.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. way to check 3 elements of array and set 4th
    By Syneris in forum C++ Programming
    Replies: 3
    Last Post: 01-09-2006, 10:30 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 10:37 PM

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