Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: sequences help

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default sequences help

    hello.
    im quite new here, and this is my first post- so straigh for the business, i was making a program at eclipse, that is supposed to get an input of an integer, another integer and an array of doubles. the first int is the legth of the sequence the user's going to type, the second int is how many numbers are gonna show up(to continue the sequence) and the array of doubles is the sequence he inputs. (of course in the first ineger's length). the program will print the next numbers of the sequence. for example: input- first int-5, second int-3, array: 1, 2, 4, 7, 11
    the output will be 16, 22, 29.
    that was quite fun to programme, but it is still not completed. i wonder if there are other programmes like mine, but more proffesional. if yes could you show me or link me to those? i would be happy if you do.
    and another thing- could you please check my code, and tell me if it's good, and what can i improve? after all im in 10th class and they're not teaching us stuff near 2 dimentional array
    here's the code. its long so you should copy it to your IDE or something.
    thanks!
    dave

    package pack;
    import java.util.Scanner;
    public class sequence {
         static Scanner in=new Scanner(System.in);
         public static void main(String[] args) {
     
    	int path[] = new int[1];
    	System.out.println("enter how many elements are in the sequence");
    	int num=in.nextInt();
    	System.out.println("enter how many elements you want to show up (up to 10)");
    	int show = in.nextInt();
    	System.out.println("enter the sequence: press enter after each element");
    	double seq[] = new double[num];
                  for (int i=0; i<num; i++){
        	    seq[i] = in.nextDouble();
                  }
     
    	double get[][] = new double[seq.length-1][seq.length-1]; //5
    	get = seqIdentifier(seq, path);
    	ShowContSeq(get, show, path, seq);
         }
     
         public static double[][] seqIdentifier(double seq[], int path[]){
    	boolean flag=false;
    	double check[][] = new double[seq.length-1][seq.length-1]; //5
     
    	//***checking + and - sequence options***
    	//finding the differences between following elements by array check[] variables
    	for (int i=0; i<seq.length-1; i++){ 
    			check[i][0] = seq[i+1]-seq[i];
    	}
    	for (int c=0; c<check.length && check[c][0] == 0; c++){
    		if (c == check.length){flag=true; path[0]=1;}
    	}
    	/*  the code is supposed to search the differences between the elements.
    	*  it works like this: it runs the loop and checks if the differences are equal.
    	*  if they are, the loop is ended. if they're not, the loop checks the same with 
    	*  the DEFFERENCE BETWEEN THE DIFFERENCES. that means, a sequence like this:
    	*  1-2-4-7-11-16-22-29-37.... (each time it grows in x+1 when x is the last number)
    	*  will run a loop. the differences aren't equal (1,2,3,4) BUT the differences
    	*  between the differences are equal so it will run 2 times and mark 2nd dimension, and will print 0.
    	*/
     
    	// switches dimensions of check array, when differences aren't equal
    	for (int index=1; index<check.length && flag == false; index++){ // 5
    		//runs for [index] dimension of the check array. 
     
    		for (int i=0; i<check.length-1; i++){   
    			check[i][index] = check[i+1][index-1]-check[i][index-1];
    		}
    		for (int c=0; c<check.length && check[c][index] == 0; c++){
    			if (c == check.length-(index+1)){flag = true; path[0] = 1;}
    		}
    	}
     
    	// checking * and / options
    	if (path[0] == 0){
    		flag=false;
    		for (int i=0; i<seq.length-1; i++){ 
    			check[i][0] = seq[i+1]/seq[i];
    	              }
    	              for (int c=0; c<check.length && check[c][0] == 1; c++){
    		    if (c == check.length-2){flag = true; path[0] = 2;}
    	              }
     
    	              for (int index=1; index<check.length; index++){
    	    	              for (int i=0; i<check.length-1; i++){
    	    		              check[i][index] = check[i+1][index-1]/check[i][index-1];
    	    	              }
    	    	              for (int c=0; c<check.length && check[c][index] == 1; c++){
    	 		              if (c == check.length-(index+1)){flag = true; path[0] = 2;}
    	                            }
    	              }
    	}
    	System.out.println(flag+", "+path[0]);
     
    	return check;
        }
     
        public static void ShowContSeq(double get[][], int show, int path[], double original[]){
        double seq[][] = new double[show+2][show+get.length+1];
     
        for (int i=0; i<get.length; i++){  // 4
    	System.out.println("");
    	for (int i2=0; i2<get.length-i; i2++){
    		System.out.print(get[i2][i]+" ");
    	}
        }
        System.out.println("\n");
     
        seq[0][0] = original[original.length-1];
        for (int i=0; i<get.length; i++){     
                  // setting the seq array so it will have the ends of the get array
    	seq[0][i+1] = get[get.length-i-1][i];
        }
     
        if (path[0] == 2){
    	for (int i=0; i<seq.length; i++){    
                                // if it found dividing or multiplying sequence it will make all initialized be 1
    		for (int i2=0; i2<seq.length; i2++){
    			if(seq[i2][i] == 0){seq[i2][i] = 1;}
    		}
    	}
        }
     
        System.out.println(seq[0].length);  //making the new array
        if (path[0] == 1 || path[0] == 2){
              for (int index=1; index<=show+1; index++){
        	  for (int i=1; i<seq.length; i++){
        		  if(path[0] == 1){seq[index][seq.length-i-1] = seq[index-1][seq.length-i-1]+seq[index][seq.length-i];}
        		  if(path[0] == 2){seq[index][seq.length-i-1] = seq[index-1][seq.length-i]*seq[index-1][seq.length-i-1];}
        	  }
              }
        }
     
        for (int i=0; i<seq.length; i++){  // 4
    	System.out.println("");
    	for (int i2=0; i2<seq.length; i2++){
    		System.out.print(seq[i2][i]+" ");
    	}
        }
        System.out.println("\n"+"\n");
        for (int i=0; i<show; i++){
    	int num = (int)seq[i+1][0]; 
    	if (num == seq[i+1][0]){
    	         if (i == (show-1)){System.out.print(num+".");}
    	         else 
                                    System.out.print(num+", ");
    	         }
    	else
    	         System.out.print(seq[i+1][0]+", ");
    }
    }
    }
    Last edited by david37370; January 1st, 2013 at 02:42 PM. Reason: added code tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: sequences help

    The code needs to be properly formatted. Nested statements should be indented 3-4 spaces.
    Too many statements start in the first column which makes it hard to read and understand the program's logic.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sequences help

    i dont understand what do you mean "too many statements start in the first column". you meant i should space it more?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: sequences help

    i dont understand what do you mean
    In the class's definition only the first and last statement should start in the first column. All others should be indented. For example:
    import java.util.Scanner;
     
    public class Sequence {
       static Scanner in=new Scanner(System.in);
       public static void main(String[] args) {

    http://www.oracle.com/technetwork/ja...oc-136057.html
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sequences help

    i understand. i do it the same but just without the class brackets, because im used to doing so but i can move them all 4 spaces. does it really matter?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: sequences help

    No, 3 vs 4 spaces doesn't make any difference. No spaces does make a difference.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sequences help

    ok thanks you. i didnt explain myself good enough i guess: i wanted to know if i did stupid things or wasted efficiency or if i could make it better, shorter, etc. if i could somehow improve the syntax. and then maybe i'll try to catch some new kinds of sequences like fibonachi or such things.
    but still thanks for the comment i'll remember that.
    also im curious if anyone else took the time to make a sequence identifying program, but in a high level. i would like to see such a thing because now i understand how complex this is.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: sequences help

    It's hard to read and understand unformatted code. When the formatting is done properly, more people could try to read it. As it is now, many will wait until it is properly formatted before trying to read it.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jan 2013
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sequences help

    you're right i'll edit it now
    is it better now?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: sequences help

    Not quite. There should never be three (or 2) }s in a column as at the end of the program. Each } should be inline below the statement with the matching {
    	if (path[0] == 0){
    		flag=false;
    		for (int i=0; i<seq.length-1; i++){ 
    			check[i][0] = seq[i+1]/seq[i];
    	    }
    The {}s are not aligned above. The last } should be below the for:
    	if (path[0] == 0){
    		flag=false;
    		for (int i=0; i<seq.length-1; i++){ 
    			check[i][0] = seq[i+1]/seq[i];
    	        }
    	if (i == (show-1)){System.out.print(num+".");}
    	         else System.out.print(num+", ");
    The else should align with the if
    	if (i == (show-1)){System.out.print(num+".");}
    	else System.out.print(num+", ");
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jan 2013
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sequences help

    thats wierd because i always do that, and when i look at eclipse now its like you said, but when i copy paste it it gets unorganized, as you explained
    maybe if you copy it back to an IDE, it will be like before
    and in the eclipse the else is totally in the same column, really

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: sequences help

    My editor doesn't fix it. Maybe if you use spaces not tabs in the source.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Jan 2013
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sequences help

    maybe i'll send it to you as a java file?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: sequences help

    Sorry, I'd rather you post it so everyone can see it.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Jan 2013
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: sequences help

    ok, i'll just do it manually

Similar Threads

  1. What's wrong with my code? (generate sequences of numbers using a stack)
    By mescoff in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 18th, 2012, 09:12 AM
  2. Question to Pattern.matches and escape sequences
    By steppenwolf in forum Java Theory & Questions
    Replies: 2
    Last Post: May 17th, 2012, 04:29 PM
  3. Generate all possible sequences from Event Tree
    By masterblaster in forum Algorithms & Recursion
    Replies: 0
    Last Post: November 22nd, 2011, 02:57 AM