Question

in java Given n >= 0, create an array with length n*n with following the pattern,...

in java

Given n >= 0, create an array with length n*n with following the pattern, shown for n=3 { 0, 1, 2, 1, 0, 3, 2, 3, 0} (Spaces have been added to show the grouping of elements of the array more "obvious.") Note: The values in the array are the sum of the "row" and "column" values, except along the diagonal, where it is zero.


diagonal(3) ? [0, 1, 2, 1, 0, 3, 2, 3, 0]
diagonal(2) ? [0, 1, 1, 0]
diagonal(4) ? [0, 1, 2, 3, 1, 0, 3, 4, 2, 3, 0, 5, 3, 4, 5, 0]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class Sample{
  
public static void main(String args[]){
  
int n, i, j;
Scanner obj = new Scanner(System.in);
n = obj.nextInt();
int a[][] = new int[n][n];
for (i = 0; i < n; i++){
for(j = 0; j < n; j++){
if(i == j){
a[i][j] = 0;
}else{
a[i][j] = i + j;
}
}
}
int b[] = new int[n*n];

int k=0;
for (i = 0; i < n; i++){
for(j = 0; j < n; j++){
b[k++] = a[i][j];

}
}
}
}

Add a comment
Know the answer?
Add Answer to:
in java Given n >= 0, create an array with length n*n with following the pattern,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a full C++program to do the following processes on a two dimension array of integers....

    Write a full C++program to do the following processes on a two dimension array of integers. Assume the size is 4x4 . 1- Fill the array with the numbers like this pattern: 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 Note: Do not accept the values as input, or hard code it. Find the math to generate these numbers and fill the array with them.    2- Reset the main diagonal elements...

  • This is for Java. Create ONE method/function that will return an array containing the row and...

    This is for Java. Create ONE method/function that will return an array containing the row and column of the largest integer i the 2D array. If the largest number is located on row 2, column 1, the method needs to return row 2 and column one. Do not use more than one method. Use the code below as the main. Please comment any changes. in java Given the main, create a method that RETURNS the largest number found in the...

  • write a java program Accept a positive integer n from keyboard and then create an array...

    write a java program Accept a positive integer n from keyboard and then create an array or arraylist containing n random elements within the range [-n,n). Print out the random array or arraylist, and then find out and print out the number of inversions and the maximum subarray (index range of the maximum subarray along with the maximum subarray sum) in the array or arraylist using divide and conquer, respectively. For example, suppose we accept integer 6 from keyboard, then...

  • I need to create a Java program that, given an array of size N whose entries...

    I need to create a Java program that, given an array of size N whose entries are numbers between 0 and 10^6, finds the largest repeating subarray. Doesn't matter how many times the array is repeated. For example, an array [0 1 2 1 4 1 2 1 0 5] should return 3 for the subarray [1 2 1]. I need this to be quite time efficient. I was recomended Suffix array but no idea how to implement.

  • Write a Java program that does the following. a. Declare an integer 2D array with 5...

    Write a Java program that does the following. a. Declare an integer 2D array with 5 rows and 5 columns. b. Initialize the array's elements to random integers between 1 and 10 (inclusive). c. Display all the elements in the 2D array as a table of rows and columns. d. Display the row index and column index of all the even integers in the 2D array. e. Display the sum of first row's elements.

  • please answer in Java..all excercises. /** * YOUR NAME GOES HERE * 4/7/2017 */ public class...

    please answer in Java..all excercises. /** * YOUR NAME GOES HERE * 4/7/2017 */ public class Chapter9a_FillInTheCode { public static void main(String[] args) { String [][] geo = {{"MD", "NY", "NJ", "MA", "CA", "MI", "OR"}, {"Detroit", "Newark", "Boston", "Seattle"}}; // ------> exercise 9a1 // this code prints the element at row at index 1 and column at index 2 // your code goes here System.out.println("Done exercise 9a1.\n"); // ------> exercise 9a2 // this code prints all the states in the...

  • Java arrays Create method named repeatedN that takes two integer parameters named range and n and...

    Java arrays Create method named repeatedN that takes two integer parameters named range and n and returns an integer array Method should return an integer array that has numbers 0 - range repeated in order n times If range is less than or equal to 0; return an array that has 0 repeated n times Create a second method named printArray that takes an integer array as a parameter. The method should print out every element on the same line...

  • This is for JAVA programming. Create a test class that contains two arrays and two methods:...

    This is for JAVA programming. Create a test class that contains two arrays and two methods: - The first array has 3 rows and 3 columns and is initialized with type double data - The second array has 4 rows and 4 columns and is also initialized with type double data - The first method ArraysRows will receive an array of type double as an argument and then print out the sum and average of all elements in each ROW....

  • This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static...

    This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static void main(String args[]) throws IOException { Scanner inFile = new Scanner(new File(args[0])); Scanner keyboard = new Scanner(System.in);    TwoDArray array = new TwoDArray(inFile); inFile.close(); int numRows = array.getNumRows(); int numCols = array.getNumCols(); int choice;    do { System.out.println(); System.out.println("\t1. Find the number of rows in the 2D array"); System.out.println("\t2. Find the number of columns in the 2D array"); System.out.println("\t3. Find the sum of elements...

  • //please help I can’t figure out how to print and can’t get the random numbers to...

    //please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks import java.util.*; public class Test { /** Main method */ public static void main(String[] args) { double[][] matrix = getMatrix(); // Display the sum of each column for (int col = 0; col < matrix[0].length; col++) { System.out.println( "Sum " + col + " is " + sumColumn(matrix, col)); } } /** getMatrix initializes an...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT