(newb) Summing half of an array

This is a discussion on (newb) Summing half of an array within the C++ Programming forums, part of the General Programming Boards category; I understand how to sum all the numbers in an integer array but how can I sum up just half ...

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

    (newb) Summing half of an array

    I understand how to sum all the numbers in an integer array but how can I sum up just half of the numbers in the array like just the top half or just the bottom half? Any suggestions would help, Thank you!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Well... how do you sum all the numbers an integer array? Show your code snippet.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    32
    something like this:

    Code:
    int SumofAll (int Array[], int size)
    {
    int total = 0;
    for (int i = 0; i < size; i++)
    total+= Array [i];
    return total;
    }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    There is no difference between

    total = SumofAll( myArray, 10 );
    and
    total = SumofAll( &myArray[0], 10 );

    Can you adjust the index and count now?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Replies: 7
    Last Post: 11-25-2008, 12:50 AM
  3. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 06:14 PM
  4. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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