Computer Science
Write a program to declare a single-dimensional array a[] and a square matrix b[][] of size N, where N > 2 and N < 10. Allow the user to input positive integers into the single dimensional array.
Perform the following tasks on the matrix:
- Sort the elements of the single-dimensional array in ascending order using any standard sorting technique and display the sorted elements.
- Fill the square matrix b[][] in the following format:
If the array a[] = {5, 2, 8, 1} then, after sorting a[] = {1, 2, 5, 8}
Then, the matrix b[][] would fill as below:
1 2 5 8
1 2 5 1
1 2 1 2
1 1 2 5
- Display the filled matrix in the above format.
Test your program for the following data and some random data:
Example 1
INPUT:
N = 3
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 3 1 7
OUTPUT:
SORTED ARRAY: 1 3 7
FILLED MATRIX
1 3 7
1 3 1
1 1 3
Example 2
INPUT:
N = 13
OUTPUT:
MATRIX SIZE OUT OF RANGE
Example 3
INPUT:
N = 5
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 10 2 5 23 6
OUTPUT:
SORTED ARRAY: 2 5 6 10 23
FILLED MATRIX
2 5 6 10 23
2 5 6 10 2
2 5 6 2 5
2 5 2 5 6
2 2 5 6 10
Java
Java Arrays
ICSE Prac 2019
11 Likes
Answer
import java.util.Scanner;
public class Array
{
public static void sortArray(int arr[]) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}
}
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("ENTER VALUE OF N: ");
int n = in.nextInt();
if (n <= 2 || n >= 10) {
System.out.println("MATRIX SIZE OUT OF RANGE");
return;
}
int a[] = new int[n];
int b[][] = new int[n][n];
System.out.println("ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY:");
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
sortArray(a);
System.out.println("SORTED ARRAY:");
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
for (int i = n - 1, r = 0; i >= 0; i--, r++) {
for (int j = 0; j <= i; j++) {
b[r][j] = a[j];
}
for (int k = n - 1; k > i; k--) {
b[r][k] = a[k - i - 1];
}
}
System.out.println();
System.out.println("FILLED MATRIX:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(b[i][j] + " ");
}
System.out.println();
}
}
}Output
![BlueJ output of Write a program to declare a single-dimensional array a[] and a square matrix b[][] of size N, where N > 2 and N 10. Allow the user to input positive integers into the single dimensional array. Perform the following tasks on the matrix: (a) Sort the elements of the single-dimensional array in ascending order using any standard sorting technique and display the sorted elements. (b) Fill the square matrix b[][] in the following format: If the array a[] = {5, 2, 8, 1} then, after sorting a[] = {1, 2, 5, 8} Then, the matrix b[][] would fill as below: 1 2 5 8 1 2 5 1 1 2 1 2 1 1 2 5 (c) Display the filled matrix in the above format. Test your program for the following data and some random data: Example 1 INPUT: N = 3 ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 3 1 7 OUTPUT: SORTED ARRAY: 1 3 7 FILLED MATRIX 1 3 7 1 3 1 1 1 3 Example 2 INPUT: N = 13 OUTPUT: MATRIX SIZE OUT OF RANGE Example 3 INPUT: N = 5 ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 10 2 5 23 6 OUTPUT: SORTED ARRAY: 2 5 6 10 23 FILLED MATRIX 2 5 6 10 23 2 5 6 10 2 2 5 6 2 5 2 5 2 5 6 2 2 5 6 10 BlueJ output of Write a program to declare a single-dimensional array a[] and a square matrix b[][] of size N, where N > 2 and N 10. Allow the user to input positive integers into the single dimensional array. Perform the following tasks on the matrix: (a) Sort the elements of the single-dimensional array in ascending order using any standard sorting technique and display the sorted elements. (b) Fill the square matrix b[][] in the following format: If the array a[] = {5, 2, 8, 1} then, after sorting a[] = {1, 2, 5, 8} Then, the matrix b[][] would fill as below: 1 2 5 8 1 2 5 1 1 2 1 2 1 1 2 5 (c) Display the filled matrix in the above format. Test your program for the following data and some random data: Example 1 INPUT: N = 3 ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 3 1 7 OUTPUT: SORTED ARRAY: 1 3 7 FILLED MATRIX 1 3 7 1 3 1 1 1 3 Example 2 INPUT: N = 13 OUTPUT: MATRIX SIZE OUT OF RANGE Example 3 INPUT: N = 5 ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 10 2 5 23 6 OUTPUT: SORTED ARRAY: 2 5 6 10 23 FILLED MATRIX 2 5 6 10 23 2 5 6 10 2 2 5 6 2 5 2 5 2 5 6 2 2 5 6 10](https://cdn1.knowledgeboat.com/img/abp12/1/2019-40-p2-1.jpg)
![BlueJ output of Write a program to declare a single-dimensional array a[] and a square matrix b[][] of size N, where N > 2 and N 10. Allow the user to input positive integers into the single dimensional array. Perform the following tasks on the matrix: (a) Sort the elements of the single-dimensional array in ascending order using any standard sorting technique and display the sorted elements. (b) Fill the square matrix b[][] in the following format: If the array a[] = {5, 2, 8, 1} then, after sorting a[] = {1, 2, 5, 8} Then, the matrix b[][] would fill as below: 1 2 5 8 1 2 5 1 1 2 1 2 1 1 2 5 (c) Display the filled matrix in the above format. Test your program for the following data and some random data: Example 1 INPUT: N = 3 ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 3 1 7 OUTPUT: SORTED ARRAY: 1 3 7 FILLED MATRIX 1 3 7 1 3 1 1 1 3 Example 2 INPUT: N = 13 OUTPUT: MATRIX SIZE OUT OF RANGE Example 3 INPUT: N = 5 ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 10 2 5 23 6 OUTPUT: SORTED ARRAY: 2 5 6 10 23 FILLED MATRIX 2 5 6 10 23 2 5 6 10 2 2 5 6 2 5 2 5 2 5 6 2 2 5 6 10 BlueJ output of Write a program to declare a single-dimensional array a[] and a square matrix b[][] of size N, where N > 2 and N 10. Allow the user to input positive integers into the single dimensional array. Perform the following tasks on the matrix: (a) Sort the elements of the single-dimensional array in ascending order using any standard sorting technique and display the sorted elements. (b) Fill the square matrix b[][] in the following format: If the array a[] = {5, 2, 8, 1} then, after sorting a[] = {1, 2, 5, 8} Then, the matrix b[][] would fill as below: 1 2 5 8 1 2 5 1 1 2 1 2 1 1 2 5 (c) Display the filled matrix in the above format. Test your program for the following data and some random data: Example 1 INPUT: N = 3 ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 3 1 7 OUTPUT: SORTED ARRAY: 1 3 7 FILLED MATRIX 1 3 7 1 3 1 1 1 3 Example 2 INPUT: N = 13 OUTPUT: MATRIX SIZE OUT OF RANGE Example 3 INPUT: N = 5 ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 10 2 5 23 6 OUTPUT: SORTED ARRAY: 2 5 6 10 23 FILLED MATRIX 2 5 6 10 23 2 5 6 10 2 2 5 6 2 5 2 5 2 5 6 2 2 5 6 10](https://cdn1.knowledgeboat.com/img/abp12/1/2019-40-p2-2.jpg)
![BlueJ output of Write a program to declare a single-dimensional array a[] and a square matrix b[][] of size N, where N > 2 and N 10. Allow the user to input positive integers into the single dimensional array. Perform the following tasks on the matrix: (a) Sort the elements of the single-dimensional array in ascending order using any standard sorting technique and display the sorted elements. (b) Fill the square matrix b[][] in the following format: If the array a[] = {5, 2, 8, 1} then, after sorting a[] = {1, 2, 5, 8} Then, the matrix b[][] would fill as below: 1 2 5 8 1 2 5 1 1 2 1 2 1 1 2 5 (c) Display the filled matrix in the above format. Test your program for the following data and some random data: Example 1 INPUT: N = 3 ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 3 1 7 OUTPUT: SORTED ARRAY: 1 3 7 FILLED MATRIX 1 3 7 1 3 1 1 1 3 Example 2 INPUT: N = 13 OUTPUT: MATRIX SIZE OUT OF RANGE Example 3 INPUT: N = 5 ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 10 2 5 23 6 OUTPUT: SORTED ARRAY: 2 5 6 10 23 FILLED MATRIX 2 5 6 10 23 2 5 6 10 2 2 5 6 2 5 2 5 2 5 6 2 2 5 6 10 BlueJ output of Write a program to declare a single-dimensional array a[] and a square matrix b[][] of size N, where N > 2 and N 10. Allow the user to input positive integers into the single dimensional array. Perform the following tasks on the matrix: (a) Sort the elements of the single-dimensional array in ascending order using any standard sorting technique and display the sorted elements. (b) Fill the square matrix b[][] in the following format: If the array a[] = {5, 2, 8, 1} then, after sorting a[] = {1, 2, 5, 8} Then, the matrix b[][] would fill as below: 1 2 5 8 1 2 5 1 1 2 1 2 1 1 2 5 (c) Display the filled matrix in the above format. Test your program for the following data and some random data: Example 1 INPUT: N = 3 ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 3 1 7 OUTPUT: SORTED ARRAY: 1 3 7 FILLED MATRIX 1 3 7 1 3 1 1 1 3 Example 2 INPUT: N = 13 OUTPUT: MATRIX SIZE OUT OF RANGE Example 3 INPUT: N = 5 ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 10 2 5 23 6 OUTPUT: SORTED ARRAY: 2 5 6 10 23 FILLED MATRIX 2 5 6 10 23 2 5 6 10 2 2 5 6 2 5 2 5 2 5 6 2 2 5 6 10](https://cdn1.knowledgeboat.com/img/abp12/1/2019-40-p2-3.jpg)
Answered By
7 Likes
Related Questions
The statement used to find the total number of Strings present in the string array String s[] is:
- s.length
- s.length()
- length(s)
- len(s)
Consider the following two-dimensional array and answer the questions given below:
int x[ ][ ] = {{4,3,2}, {7,8,2}, {8, 3,10}, {1, 2, 9}};(a) What is the order of the array?
(b) What is the value of x[0][0]+x[2][2]?
What is the output of the Java code given below?
String color[] = {"Blue", "Red", "Violet"}; System.out.println(color[2].length());- 6
- 5
- 3
- 2
Define a class to accept values into an integer array of order 4 x 4 and check whether it is a DIAGONAL array or not. An array is DIAGONAL if the sum of the left diagonal elements equals the sum of the right diagonal elements. Print the appropriate message.
Example:
3 4 2 5
2 5 2 3
5 3 2 7
1 3 7 1Sum of the left diagonal element = 3 + 5 + 2 + 1 = 11
Sum of the right diagonal element = 5 + 2 + 3 + 1 = 11