Thread: Change array length

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    6

    Change array length

    On beginning I define two arrays

    a[5];
    b[5];

    after this I need next array C

    c[5];

    but when I have c[5] = a[5] + b[5]

    I have overflow of array C.

    how can I change length of array C in loop, I don't change C if I don't have overflow

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    It would be c[4] = a[4] + b[4].

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Remember that arrays in C begin with index 0, so array[5] gives you indeces 0-4 only.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    I know that a[0] is first element of array

    I have need for something like this:
    Code:
    a[5] = {9,9,9,9,9,9}
    +
    b[5] = {8,8,8,8,8,8}
    =
    c[5+1] = {1,8,8,8,8,8,7}
    that mean c[i+1] = a[i] + b[i]
    but 1 in [i+1] is conditioned by add, it not always present
    Code:
    a[5] = {4,4,4,4,4,4}
    +
    b[5] = {4,4,4,4,4,4};
    =
    c[5] = {8,8,8,8,8,8};
    in this case I don't have need for +1 in c[i]

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Set the length of array c to be 6 and, in cases where the length of the result is 5, make the first element zero.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    xian china
    Posts
    31
    define array c longer length.
    Code:
    int c[255] = {0};
    then when you do " a[5] = {4,4,4,4,4,4}+b[5] = {4,4,4,4,4,4};"
    the result put into c can be begin from 0,
    like c[0] = a[4]+b[4],c[1] = a[3]+b[3]...
    Last edited by Jesius; 11-28-2010 at 03:34 AM.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by miroslavgojic View Post
    On beginning I define two arrays

    a[5];
    b[5];

    after this I need next array C

    c[5];

    but when I have c[5] = a[5] + b[5]

    I have overflow of array C.

    how can I change length of array C in loop, I don't change C if I don't have overflow
    Are you sure you're not tripping over that old C problem where the number of elements does not equal the numbers of the elements?

    Array elements always number from 0, not from 1, so a[5] has elements numbered 0 ,1 ,2, 3, 4

    There is no element #5... but there is The 5th Element

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. length of dynamic array
    By budala in forum C Programming
    Replies: 12
    Last Post: 02-12-2011, 02:25 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. array length question
    By cs_newbie in forum C Programming
    Replies: 24
    Last Post: 10-12-2006, 09:10 AM
  5. array length
    By Wick in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2003, 04:53 PM