summing an array's elements

This is a discussion on summing an array's elements within the C++ Programming forums, part of the General Programming Boards category; Hi, I am using a function and passing in the array as a parameter. I would like to then sum ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    1

    summing an array's elements

    Hi,

    I am using a function and passing in the array as a parameter. I would like to then sum the array's elements. Can anyone help me with that?

    Thanks! Mindi

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    Show us what you have so far.

    gg

  3. #3
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    If you want to pass an array and sum its elements, you have to pass in the size of the array as a separate argument too. This is because you can't find the size of an array within a function.

    A lame example involving an array of ints:

    Code:
    #include <iostream>
    #include <cstdlib>
    
    //
    // Calculates the sum of the elements in an array.
    //
    int sumOfElements( int array[], int sizeOfArray )
    {
      int total = 0 ;
      for( int i=0; i<sizeOfArray; i++ )
        total += array[i] ;
      return total ;
    }
    
    int main(int argc, char *argv[])
    {
      int intArray[] = { 1, 2, 3, 4, 5, 6, 7 } ;
      int sizeOfArray = sizeof(intArray)/sizeof(intArray[0]) ;
      
      std::cout << "The sum of the elements in the array is " ;
      std::cout << sumOfElements( intArray, sizeOfArray ) ;
      
      std::cin.get() ;
      return 0;
    }

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Edit: Nevermind what I said...

    You can also pass it this way:
    Code:
    int somefunc ( int* array, int size)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    >> Not sure if this is correct, but I think there is a way to determine the number of elements in the array...Maybe something like:

    It would not work if the array was passed into the function, and if you're trying that within the function:

    The following code does not work (as intended).

    Code:
    #include <iostream>
    #include <cstdlib>
    
    //
    // Calculates the sum of the elements in an array.
    //
    int sumOfElements( int array[] )
    {
      int total = 0 ;
      for( int i=0; i<sizeof(array)/sizeof(array[0]); i++ )
        total += array[i] ;
      return total ;
    }
    
    int main(int argc, char *argv[])
    {
      int intArray[] = { 1, 2, 3, 4, 5, 6, 7 } ;
      int sizeOfArray = sizeof(intArray)/sizeof(intArray[0]) ;
      
      std::cout << "The sum of the elements in the array is " ;
      std::cout << sumOfElements( intArray ) ;
      
      std::cin.get() ;
      return 0;
    }

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Yeah...After I posted I thought about it and realized that it probably wouldn't work, and then I reread your post...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    man i had a prog with the same exact question. ill c if i can find it.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    As others have mentioned there is no way to tell the size of an array from a pointer. The normal C convention is to pass the pointer and a size, though you should probably be using size_t rather than int.

    int accumulate(const int *array, size_t size);

    I recommend you adapt the C++ convention of iterator pairs, one to the start and "one past the end".

    Code:
    int accumulate(const int *first, const int *last);
    ...
    int main() {
        int arr[] = {1,3,4,63,2,4,56};
        int total = accumulate(arr,&arr[sizeof(arr)/sizeof(arr[0])]);
    }
    This is much more frendly to generic code and allows you to write.

    Code:
    template<class ForwardIterator>
    int accumulate(ForwardIterator first, ForwardIterator last) {
        return std::accumulate(first,last,0);
    }
    That not only works with arrays but also lists, vectors, sets, and any container class you write that is willing to yarf up a forward iterator.

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    You can also consider using a sentinel value to indicate the end of the array.

  10. #10
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    You could also consider using vectors and save yourself a great deal of headache.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  11. #11
    Neo
    Neo is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    8
    Considering his post he is probably new to C++ and I didn't learn about Vectors until my third CS class so Vectors might not be an option for him right now Vectors are a lot nicer though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-04-2008, 08:27 PM
  2. Comparing elements of character pointer arrays
    By axe in forum C Programming
    Replies: 2
    Last Post: 11-13-2007, 11:20 PM
  3. calculating elements of arrays
    By Kristin in forum C Programming
    Replies: 2
    Last Post: 11-02-2003, 09:38 AM
  4. Pointer of arrays - how do I access their elements?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-11-2001, 11:28 PM
  5. elements of arrays; functions
    By sballew in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 01:48 AM

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