Results 1 to 3 of 3

Thread: [RESOLVED] Ok I need help

Hybrid View

  1. #1
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Ok I need help and I'm willing to pay $25 via Paypal

    Keep your money...it was fun

    Code:
    import java.io.*;
    
    public abstract class App {
      public static void main(String[] args) {
        int ar[] = new int[50];
        ReadFile("C:\\Temp.txt", ar);
        sort(ar);
        System.out.println("Minimum= " + ar[0]);
        System.out.println("Maximum=" + ar[ar.length - 1]);
        System.out.println("Mean= " + mean(ar));
        System.out.println("Mode= " + modal(ar));
        System.out.println("Median= " + median(ar));
        System.out.println("Std Dev= " + stdDev(ar));
      }
    
      public static double mean(int ar[]) {
        int sum = 0;
        for (int i = 0; i < ar.length; i++) {
          sum += ar[i];
        }
        return (sum / ar.length);
      }
    
      public static int modal(int a[]) {
        int n;
        int freq = 0;
        int Mfreq = 0;
        int Mn = a[0];
        for (int i = 0; i < a.length; i++) {
          n = a[i];
          for (int j = 0; j < a.length; j++) {
            if (a[j] == n) {
              freq++;
            }
          }
          if (freq > Mfreq) {
            Mn = n;
          }
        }
        return Mn;
      }
    
      public static int median(int a[]) {
        if (a.length % 2 == 0) {
          int x = a[a.length / 2];
          int y = a[ (a.length + 2) / 2];
          return (x + y) / 2;
        }
        else {
          return a[ (a.length + 1) / 2];
        }
      }
    
      public static double stdDev(int a[]) {
        int s2 = 0;
        int s = 0;
        for (int i = 0; i < a.length; i++) {
          s2 += (a[i] * a[i]);
          s += a[i];
        }
        return Math.sqrt( (s2 - (s * 2) / a.length) / (a.length - 1));
      }
    
      public static void sort(int a[]) {
        int tmp;
        for (int i = 0; i < a.length; i++) {
          for (int j = 0; j < a.length; j++) {
            if (a[j] > a[j + 1]) {
              tmp = a[j];
              a[j] = a[j + 1];
              a[j + 1] = tmp;
            }
          }
        }
      }
    
      public static int ReadFile(String filename, int ar[]) {
        File infile = new File(filename);
        int value, n = 0;
        try {
          BufferedReader reader = new BufferedReader(new FileReader(infile));
          String line = null;
    
          while ( (line = reader.readLine()) != null) {
            value = Integer.parseInt(line);
            ar[n++] = value;
          }
          return n;
        }
        catch (IOException e) {
          System.err.println("Problem reading input file!");
        }
        return n;
      }
    }
    Last edited by ComputerJy; Feb 1st, 2006 at 07:32 PM.
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

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