# sequences help

• January 1st, 2013, 12:39 PM
david37370
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 :D
here's the code. its long so you should copy it to your IDE or something.
thanks!
dave

Code :

```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]+", "); } } }```
• January 1st, 2013, 12:50 PM
Norm
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.
• January 1st, 2013, 12:54 PM
david37370
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?
• January 1st, 2013, 12:59 PM
Norm
Re: sequences help
Quote:

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:
Code :

```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
• January 1st, 2013, 01:02 PM
david37370
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?
• January 1st, 2013, 01:03 PM
Norm
Re: sequences help
No, 3 vs 4 spaces doesn't make any difference. No spaces does make a difference.
• January 1st, 2013, 01:08 PM
david37370
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.
• January 1st, 2013, 01:11 PM
Norm
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.
• January 1st, 2013, 01:13 PM
david37370
Re: sequences help
you're right i'll edit it now
is it better now?
• January 1st, 2013, 01:23 PM
Norm
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 {
Code :

``` 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:
Code :

``` if (path[0] == 0){ flag=false; for (int i=0; i<seq.length-1; i++){ check[i][0] = seq[i+1]/seq[i]; }```
Code :

``` if (i == (show-1)){System.out.print(num+".");} else System.out.print(num+", ");```
The else should align with the if
Code :

``` if (i == (show-1)){System.out.print(num+".");} else System.out.print(num+", ");```
• January 1st, 2013, 01:27 PM
david37370
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
• January 1st, 2013, 01:29 PM
Norm
Re: sequences help
My editor doesn't fix it. Maybe if you use spaces not tabs in the source.
• January 1st, 2013, 01:32 PM
david37370
Re: sequences help
maybe i'll send it to you as a java file?
• January 1st, 2013, 01:33 PM
Norm
Re: sequences help
Sorry, I'd rather you post it so everyone can see it.
• January 1st, 2013, 01:35 PM
david37370
Re: sequences help
ok, i'll just do it manually