Thread: How do I skip over zero elements in an array?

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    How do I skip over zero elements in an array?

    I'm initializing an array with a value of [9]. The user can either enter all 10 values or enter a negative number to stop the array. Then I want to average all of the elements in the array, but if the user enters a negative number to stop the array, then it makes the rest of the values in the array zero. How would I code it to make sure that the zero values do not get averaged?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by Roon Hapoon View Post
    I'm initializing an array with a value of [9]. The user can either enter all 10 values or enter a negative number to stop the array. Then I want to average all of the elements in the array, but if the user enters a negative number to stop the array, then it makes the rest of the values in the array zero. How would I code it to make sure that the zero values do not get averaged?
    First of all, if you are creating an array with [9] indexes, then a user can only enter 9 values -- not 10.

    Why not store -1 in the index, and then when you average the numbers, don't include any index with a -1.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    Quote Originally Posted by bithub View Post
    Why not store -1 in the index, and then when you average the numbers, don't include any index with a -1.
    But how would I do that? With an if statement?

    for (x = 0; x < 10; x++)
    {
    avg = (array[x] + avg) / x;
    if (array[x] == -1)

    but then what? That's the part I just don't get :\

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, loop until the index reaches 10, or the current element is negative.
    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

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by Roon Hapoon View Post
    But how would I do that? With an if statement?

    for (x = 0; x < 10; x++)
    {
    avg = (array[x] + avg) / x;
    if (array[x] == -1)

    but then what? That's the part I just don't get :\
    Use the break statement to exit your loop when you reach a -1.
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Math Note: Average is the sum of the elements divided by the number of elements.

    I have no idea what your code is doing.

    Tim S.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Roon Hapoon View Post
    But how would I do that? With an if statement?

    for (x = 0; x < 10; x++)
    {
    avg = (array[x] + avg) / x;
    if (array[x] == -1)

    but then what? That's the part I just don't get :\
    I would try something like...
    Code:
    int array[10];
    int avg ;
    
    // user enters values
    
    avg = 0;
    for (int x = 0; x < 10; x++)
      { if (array[x] == -1)  
            { avg /= x;
              break; }
         avg += array[x]; }

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I wouldn't calculate a "running" average. Unless you specifically need to keep a running average, don't do it - it's just extra calculation for nothing.

    taking your code:
    Code:
    for (x = 0; x < 10; x++)
    {
    avg = (array[x] + avg) / x;
    if (array[x] == -1)
    
    and removing the running average and adding a sum:
    for(x = 0, sum = 0; x < 10; x++) 
    {
      if(array[x] > -1) 
        sum = sum + array[x];
      else
        break;
    }
    //now calculate the sum, just once:
    average = sum/x;
    The standard idiom or practice in C, with arithmetic like: sum = sum + someNumber, is to use the shorthand: sum += someNumber. Don't be surprised if you see C code like that - it's very common. That applies to subtraction, multiplication, and division, as well as addition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. coping elements not in array
    By lord in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2008, 07:53 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 10:37 PM