(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
    23,796
    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
    33,292
    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, 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