Results 1 to 2 of 2
  1. #1
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default Adding all elements of an Array together, not adding up correctly

    Java Code:
    import java.text.DecimalFormat;
    import java.util.Scanner;
    public class Lab11
    {
       public static void main(String[] args)
       {
          final int arraySize = 30;
          final double SENT = -999.99;
    
          
          Scanner scan = new Scanner(System.in);
          DecimalFormat fmt = new DecimalFormat("0.00");
          double overallSum = 0;
          int overallNum = 0;
          double sum, average, overallAver = 0;
          int num;
    
    
          
          double[] dArr = new double[arraySize];
    
    
          
          while(scan.hasNext())
          {
    
    
             
             num = readData(scan, dArr, SENT);
    
             
             sum = sumNumbers(dArr, num);
    System.out.println(sum + "     " + num);
             overallSum += sum;
             overallNum += num;
    
    
             
             System.out.println("The average of the group is "
                                + fmt.format(sum/num));
    
          } 
    
          
          System.out.println("The average overall inputs is "
                             +fmt.format(overallSum/overallNum));
    
       } 
    
    
       public static int readData(Scanner scan, double[] dArr, double sent)
       {
         
          int n;
              n = 0;
          double dB = scan.nextDouble();
    
          
          while(dB != sent)
          {
             
             if(n >= dArr.length)
             {
                System.out.println("Error: Not enough room in array;"
                                   + "Quitting Program ");
                System.exit(0);
             }
    
             
             dArr[n] = dB;
             n++;
             dB = scan.nextDouble();
    
          }  
    
    
          
          return n;
    
       } 
    
       public static double sumNumbers(double[] dArr, int num)
       {
          double sum = 0;
          
          for(int i = 0; i < dArr.length; i++)
          {
             sum = sum + dArr[i];
          }
          
          return sum;
       }
    
    }
    The input is

    Java Code:
    0.7497790548868464
    0.6462076498775827
    0.32616335534841256
    0.37582682849958304
    -999.99
    0.41621372481399
    0.6463706270883212
    0.7655177221930171
    0.2650562905607654
    0.14762994129539841
    0.9627229328823885
    -999.99
    0.08133929860959621
    0.9560335264804165
    0.8155875415237163
    -999.99
    0.019074104888079813
    0.8270564200270186
    -999.99
    From the group beginning with "0.08133929860959621", and the following group beginning with "0.019074104888079813", they do not add correctly. I'm not really sure why.

  2. #2
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    Figured it out, instead of
    Java Code:
    for(int i = 0; i < dArr.length; i++)
    I used
    Java Code:
    for(int i = 0; i < num; i++)
    and it worked like a charm.

Similar Threads

  1. Adding elements to an array
    By slitka in forum New To Java
    Replies: 9
    Last Post: 04-01-2011, 04:01 AM
  2. Adding elements before and after array
    By liQuorice in forum New To Java
    Replies: 4
    Last Post: 03-04-2011, 12:52 AM
  3. Adding the Elements of a 2D Array
    By RMcLuckie45 in forum New To Java
    Replies: 0
    Last Post: 11-07-2010, 11:04 PM
  4. Adding elements in array list and vectors using threads
    By hina.yousuf@seecs.edu.pk in forum Advanced Java
    Replies: 2
    Last Post: 10-10-2010, 03:07 AM
  5. Adding elements to an Object Array
    By aneesahamedaa in forum New To Java
    Replies: 4
    Last Post: 09-07-2008, 03:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •