Thread: (newb) Summing half of an array

  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
    28,413
    Well... how do you sum all the numbers an integer array? Show your code snippet.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

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, 01:50 AM
  3. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  4. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM