array question

This is a discussion on array question within the C Programming forums, part of the General Programming Boards category; Hi, I got a question. If i have an array of five cells, each storing a single digit number, and ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    2

    array question

    Hi, I got a question. If i have an array of five cells, each storing a single digit number, and another array of the same, how can i add both arrays so that the sum is stored in an array of six cells? If there is no carry, then sum[0] must hold a zero.

    eg.
    integer1={4,5,0,7,1}
    integer2={9,2,9,8,7}
    sum={1,3,8,0,5,8}

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    Code:
    integer1[6]={0,4,5,0,7,1} 
    integer2[6]={0,9,2,9,8,7} 
    sum[6]={1,3,8,0,5,8}
    Consider what integer1[i] + intege2[i] is

    Then consider what it means if that result is >= 10

    Then think about a for loop

  3. #3
    Unregistered
    Guest
    If you just want to sum 2 numbers, an array is an awkward way to do it. You would have to walk through both arrays in reverse order, and keep a carry. Let us know if you need more info.

  4. #4
    Intranasal Heroin User Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,035
    Code:
    #include "stdio.h"
    int main()
    {
    int var=0;
    int integer1[6]={0,4,5,0,7,1}; //make element 5, arrays start with 0
    int integer2[6]={0,9,2,9,8,7};
    int sum[6] = {0,0,0,0,0,0};
    printf("The sum array is: ");
    while(var<6)
    {
         sum[var] = integer1[var]+integer2[var];
         printf("%d ",sum[var]);
         var++;
    }
    
    return 0;
    }
    Last edited by Xterria; 02-18-2002 at 05:31 PM.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    2
    Thanks for all the help guys, i got it.
    Last edited by KAchE; 02-18-2002 at 06:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 06:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 05:27 PM

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