Results 1 to 2 of 2

Thread: PrefixCalc

  1. #1

    Thread Starter
    Addicted Member MethadoneBoy's Avatar
    Join Date
    Oct 2001
    Location
    Preferably somewhere between Keira Knightley and Diane Kruger but I'm not fussy
    Posts
    180

    PrefixCalc

    Does anyone know how to modify the prefixCalc method here so that it won't print out "Infinity..." when run?

    class Prefix {

    static float a[] = {2,8,2,8,5,11};

    static void prefixCalc(float Arr[]) {

    try {

    Arr = a;

    for (int i = 0; i < Arr.length; i ++) {

    for (int j = 0; j <= i; j ++) {

    Arr[i] += Arr[j];
    Arr[i] /= i;

    }

    }

    } catch (Exception e) {}

    }

    public static void main( String args[] ) {

    prefixCalc(a);

    System.out.print("prefix averages =");

    for (int i = 0; i < a.length; i++)

    System.out.print(" " + a[i]);

    System.out.println();

    }

    }




    Thanks

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I ran your code and changed it alittle so i could see what was going on. It seems that you are dividing a floating point number by 0 which results in Positive Infinity.

    Code:
    class Prefix { 
    
    static float a[] = {2,8,2,5}; 
    
    static void prefixCalc(float Arr[]) { 
    
    try { 
    
    Arr = a; 
    
    for (int i = 0; i < Arr.length; i ++) { 
    
    for (int j = 0; j <= i; j ++) { 
    
    Arr[i] += Arr[j];
    System.out.println(Arr[i] + " + "  + Arr[j]);
    Arr[i] /= i; 
    System.out.println(Arr[i] + " / "  + i); 
    } 
    
    } 
    
    } catch (Exception e) {} 
    
    } 
    
    public static void main( String args[] ) { 
    
    prefixCalc(a); 
    /*
    System.out.print("prefix averages ="); 
    
    for (int i = 0; i < a.length; i++) 
    
    System.out.print(" " + a[i]); 
    
    System.out.println(); 
    */
    } 
    
    }

Posting Permissions

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



Click Here to Expand Forum to Full Width