sum of 2 int arrays

This is a discussion on sum of 2 int arrays within the C++ Programming forums, part of the General Programming Boards category; if i have 2 int arays what is the best way to sum the two in new array?...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    18

    sum of 2 int arrays

    if i have 2 int arays what is the best way to sum the two in new array?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    A for loop, and use of the + operator
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    void print (int* a, int* b, int arraySize) {
       int* aPtr;
       int* bPtr;
       int i;
    
       cout << "The values:" << endl;
       for (i = 0, aPtr = a, bPtr = b; i < arraySize; 
          cout << "A[" << i << "]: " << *(aPtr++) << endl, 
          cout << "B[" << i << "]: " << *(bPtr++) << endl, i++);
    }
    
    int main() {
       const int arraySize = 10;
       int* a = new int[arraySize];
       int* b = new int[arraySize];
       int* aPtr;
       int* bPtr;
       int i;
    
       // initialise
       for (i = 0, aPtr = a, bPtr = b; i < arraySize; *(aPtr++) = 1, *(bPtr++) = 2, i++);
    
       // print
       print(a, b, arraySize);
    
       // copy
       for (i = 0, aPtr = a, bPtr = b; i < arraySize; *(aPtr++) = *(bPtr++), i++);
    
       // print
       print(a, b, arraySize);
    }

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Maybe:
    Code:
    transform(start of array1, end of array1, start of array 2, start of sum array, plus<int>());
    Assumes array 2 and the sum array are at least as big as array 1. Need to #include the <algorithm> and <functional> headers for that to work. Still a couple details for you to work out on your own there.

    Edit: Probably not the best way... but I like it.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    18
    thanks i will try them

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    for (i = 0, aPtr = a, bPtr = b; i < arraySize; *(aPtr++) = 1, *(bPtr++) = 2, i++);
    You shouldn't cram too much into a for loop (or while loop) header. It can get hard to read.

    BTW, instead of *(aPtr++) you can use *aPtr++.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by dwks
    You shouldn't cram too much into a for loop (or while loop) header. It can get hard to read..
    Amen to that! It also makes debugging a nightmare, and you will hate yourself for doing that in a very short time when you have forgotton what that is supposed to do. and that semicolon at the end of the line will cause many hours of grief if you ever decide to add code that should be executed on every iteration of the loop, and you don't know why it doesn't work.
    Last edited by Ancient Dragon; 10-21-2005 at 03:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

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