Don't overwrite this space!!!!

This is a discussion on Don't overwrite this space!!!! within the C Programming forums, part of the General Programming Boards category; I have a question about loading values into an array. Let's say that I have an initialized array of length ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    43

    Don't overwrite this space!!!!

    I have a question about loading values into an array. Let's say that I have an initialized array of length n, and that I want to read in new values. For example lets say that the array is initialized to all zeros
    Code:
     ary[n] = {0}
    . Now I want to read in - n numbers or characters,but after I do that if an element contains the same element that want to read in I have to prevent it from occurring. For example:

    let's say that the array is of length five.

    Code:
    int i;
    char ary[5]= {0}
    
    for(i= 0;i<3;i++){
      
       ary[i] = 'M';
    
    }
    I did not fill the whole array with 'M', now let's say that this same array is called again and I want to fill the rest of the array with 'M',but If an 'M' is already in an index position I wan't to prevent an 'M' from overwriting a previous 'M' and just look for a place with no 'M' and put one there. How could I do this?
    Last edited by mesmer; 10-27-2008 at 02:16 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Could you please edit your post and move the "[/code]" to BEFORE your question. Then we can read it without scrolling three miles sideways.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You would want to use an if statement.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    "if an element contains the same element that want to read in I have to prevent it from occurring"

    There is no way that this could possibly matter, and it certianly won't save computation, since you have to do a check -- in other words, if it's already an M, it might as well be again.

    However, you could throw this into your loop:

    Code:
    if (ary[i]=='M') continue;
    else ary[i]='M';
    Again, computationally this is a waste of time and effort.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably also need a loop that walks from one end towards the position that is either set or not set [depending on whether you think it will be "mostly empty" or "mostly full" to reduce the number of iterations you need to do].

    Two other possibilities:
    * If it's always a char array that always has at least one zero at the end, use strlen to see how much has been filled in.
    *Use a second variable to hold the "last used" or "first free" position in some way (either a pointer or an index).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Out of space when compiling kernel
    By NuNn in forum Linux Programming
    Replies: 3
    Last Post: 04-01-2009, 03:43 PM
  2. disk space mysteriously gone
    By evader in forum C++ Programming
    Replies: 4
    Last Post: 01-21-2004, 01:30 PM
  3. Replies: 12
    Last Post: 05-17-2003, 06:58 AM
  4. someone who is good at finding and fixing bugs?
    By elfjuice in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2002, 04:59 PM

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