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]+", "); } } }