Results 1 to 5 of 5
  1. #1
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Array Simulation help?? Getting wrong output.

    I'm supposed to write a completely executable program and review the execution of the method with arrays passed as its parameter, and write the value it would return. The element values of the array, list can be directly assigned to the parameter prior to calling the method.

    My problem is that I don't get the right output. My professor told us what the output is (shown in the comments), but I get 0, 0, 2, 1, 3. I don't even know where the numbers are coming from... I need help asap as this is due by 11am, I repeat, 11am.

    Java Code:
    public class Mystery {
      
    public static void main (String[] args) { 
    
    // figure out the average TA IQ
      
    int[] iq = {126, 84, 149, 167, 95};
     
    double avg = average(iq);
     
    System.out.println("Average IQ = " + avg);
      
    int[] a1 = {5}; 
    System.out.println(arrayMystery(a1));  // 0
    
    int[] a2 = {3, 12};
    System.out.println(arrayMystery(a2));  // 9
     
    int[] a3 = {4, 2, 10, 8}; 
    System.out.println(arrayMystery(a3));  // 6
     
    int[] a4 = {1, 9, 3, 5, 7};
    System.out.println(arrayMystery(a4));  // 8
     
    int[] a5 = {8, 2, 10, 4, 10, 9}; 
    System.out.println(arrayMystery(a5));  // 2
    
    }
    
    public static int arrayMystery(int[] array) { 
      
     int x = 0; 
     
     for (int i = 0; i < array.length - 1; i++) { 
       
     if (array[i] > array[i + 1]) { 
       
     x++; 
     
     } 
     
     } 
     
     return x; 
     
    }  
    
    public static double average(int[] numbers) {
    
    int sum = 0;
    
    for (int i = 0; i < numbers.length; i++) {
      
    sum += numbers[i];
    
      }
      
      return (double) sum / numbers.length;
      
      }
      
      public static int mystery1(int[] list) {
      
        int x = 0;
        
        for (int i = 1; i < list.length; i++) {
          
            int y = list[i] - list[0];
            
            if (y > x) {
              
                x = y;
                
            }
            
        }
        
        return x;
      
      }
      
      }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: Array Simulation help?? Getting wrong output.

    I for one can't help as I haven't the faintest idea what your requirements are.
    Clearly the code isn't correct as the arrayMystery method is returning (as you say) 0,0,2,1,3...but what the algorithm in that method is actually supposed to do to return the values in the comments? Who knows.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: Array Simulation help?? Getting wrong output.

    Java Code:
    int[] a5 = {8, 2, 10, 4, 10, 9}; 
    System.out.println(arrayMystery(a5));  // 2
    Is your assignment to write a method that returns a 2 given the array: {8, 2, 10, 4, 10, 9}?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: Array Simulation help?? Getting wrong output.

    Yes, Norm.... I wrote the prompt... The prompt is:

    Simulate the followng method by writing a completely executable program and review the execution of the method with each of the following arrays passed as its parameter, and write the value it would return. The element values of the array, list can be directly assigned to the parameter prior to calling the method.

    She gives us input, the a1 - a5 lines, and the "value returned" is in the comments next to them.
    Last edited by manibby93; 10-21-2013 at 03:32 PM.

  5. #5
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: Array Simulation help?? Getting wrong output.

    I got it. I switched "arrayMystery" to "mystery1" and got the right answers. Thanks anyways...

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 02:37 AM
  2. Getting wrong output. Please Help!!
    By FAkamo in forum New To Java
    Replies: 2
    Last Post: 02-24-2012, 12:45 AM
  3. Car Simulation (Array)
    By slacoul in forum New To Java
    Replies: 1
    Last Post: 09-29-2011, 11:04 AM
  4. output not as expected.What's wrong?
    By abdullahansari in forum New To Java
    Replies: 1
    Last Post: 12-03-2010, 09:01 PM
  5. Whats Wrong I m nt Getting output -please please help
    By divakantdinesh in forum New To Java
    Replies: 3
    Last Post: 10-16-2010, 10:21 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
  •