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
Define a class pin code and store the given pin codes in a single dimensional array. Sort these pin codes in ascending order using the Selection Sort technique only. Display the sorted array.
110061, 110001, 110029, 110023, 110055, 110006, 110019, 110033
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)
Define a class to search for a value input by the user from the list of values given below. If it is found display the message "Search successful", otherwise display the message "Search element not found" using Binary search technique.
5.6, 11.5, 20.8, 35.4, 43.1, 52.4, 66.6, 78.9, 80.0, 95.5.
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]?