help with arrays & methods

I need some methods that use arrays to sort

Page 1 of 1

3 Replies - 660 Views - Last Post: 06 November 2010 - 08:35 PM Rate Topic: -----

#1 poulin76  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-November 10

help with arrays & methods

Posted 06 November 2010 - 02:08 PM

Hello,
Well Im stuck on this problem for my class, I mean i'm drawing a complete blank here. heres the crux of the problem:

This program has a console-based user interface. After printing a brief introduction to the user, your program will prompt for a file name. Your program will examine the data and find the longest sequence of decreasing numbers. The program will display the numbers in that sequence, how many there are, and the position (starting from 0) where the sequence starts. The program should repeat this process, allowing the user to analyze other files, until the user signals they are done (using an exit sentinel of some sort).

Heres one of the methods I can't quite get:

public static double[] getDecSeq(double[] data) -- returns the longest decreasing sequence found in the data. If more than one decreasing sequence is present with the "longest" length, return the one that appears closest to the beginning of the array. Therefore, if the entire set of data is non-decreasing, return an array of capacity one holding the first element. The returned array should be completely filled (i.e. its length should match the length of the sequence).

A push in the right direction would be great. I keep trying to use a for loop to go through the array and find the decending sequence but I'm not sure how to recognize it and how to store it so I have propper access to it later. heres what I have so far.

import java.util.*;
import java.io.*;


public class Sequence{
  
  public static void main(String[] args) throws FileNotFoundException {      
   
   Scanner console = new Scanner(System.in);                                 
   System.out.print("Please enter the name of the file to be processed: ");  
   File f = new File(console.nextLine());                                    
   System.out.println(Arrays.toString(getDecSeq((getData(f)))));
   
   }
  
  public static double[] getData(File source) throws FileNotFoundException{
    Scanner input = new Scanner(source);
    double[] listA = new double [70];
    int count = 0;
    
    while (input.hasNextDouble()){
      listA [count] = input.nextDouble();
      count++;
    } 
    double[] listB = new double[count];
    for (int i = 0; i < listB.length; i++)
      listB[i] = listA[i];
    return listB;
    
  }
  
  //public static int findSequence(double[] data, double[] seq){
    
  //}
  
  public static double[] getDecSeq(double[] data){
    //System.out.println("hello" + Arrays.toString(data));
    double[] seq = new double [data.length];
    
    double temp = 0;
    int j = 0;
    for (int i = 1; i < data.length; i++){  //This is where I start going
      if (data[i] < data[i-1])               //off the rails!!
            seq[i] = data[i];
    }
    
    return seq;
  }
  
  //public static String toString(double[] data){
    
  //}
  
}



Is This A Good Question/Topic? 0
  • +

Replies To: help with arrays & methods

#2 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: help with arrays & methods

Posted 06 November 2010 - 02:58 PM

sequenceLength = 1;
for ( int i = 0; i < n; ++i )
{
  if ( data[i] > data[i+1] )
    sequenceLength++;
  else
  {
    if ( sequenceLength > maxSequenceLength )
      maxSequenceLength = sequenceLength;
    sequenceLength = 1;
  }
}



Wrote it from the top of my head and didn't test, but maybe it'll give you a sense of direction. Lemme know if it works or not.

This post has been edited by sparkart: 06 November 2010 - 02:59 PM

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10492
  • View blog
  • Posts: 38,876
  • Joined: 27-December 08

Re: help with arrays & methods

Posted 06 November 2010 - 06:21 PM

Just make sure your loop goes from 0 through length-2 rather than length-1, otherwise you'll get an ArrayIndexOutOfBoundsException when you try to access the i+1 element at the end of the loop. :)
Was This Post Helpful? 0
  • +
  • -

#4 poulin76  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-November 10

Re: help with arrays & methods

Posted 06 November 2010 - 08:35 PM

How would I go about storing the actual max decending array and tehn calling it back later?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1