The first row contains only one entry and value is 1; the second row contains tw
ID: 3623049 • Letter: T
Question
The first row contains only one entry and value is 1; the second row contains two entries that are all ones. From the third row of the arrays, the first and last entries in the row are always ones, other entry's value is the sum of the entries on its top and top-left.Suppose the size of the arrays is n, n>2, in row i and column j, where i >= 3, j > 1 and j<i, the value of the entry is the sum of the entries in row i-1, column j-1 and row i-1, column j.
For example, when a user input 5, the arrays are printed as following:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Implement a Java program
i will rate life saver thx :)
Explanation / Answer
import java.util.Scanner;
/**
*
* @author ashok
*/
public class Main {
public static void main(String[] args) {
Scanner sin = new Scanner(System.in);
int n;
System.out.println("Please enter user input:");
n=sin.nextInt();
int array[]=new int[n];
int temp[]=new int[n];
System.out.println("1");
if(n>1)
{
System.out.println("1 1");
array[0]=1;array[1]=1;
for(int i=2;i
{
System.out.print("1 ");
temp[0]=1;
for(int j=2;j<=i;j++)
{
System.out.print(array[j-2]+array[j-1]+" ");
temp[j-1]=array[j-2]+array[j-1];
}
temp[i]=1;
System.arraycopy(temp, 0, array, 0, temp.length);
System.out.print("1");
System.out.println();
}
}
}
}
-----------------------------------------------
Sample execution:
Please enter user input:
7
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.