Untitled
unknown
plain_text
17 days ago
1.7 kB
4
Indexable
Never
package assignment3; import java.util.Scanner; import java.util.Random; public class SymmetricalImmage { public static void main(String[] args) { Scanner scan = new Scanner(System.in); Random random = new Random(); System.out.println("Enter the number of rows (n): "); int n = scan.nextInt(); System.out.println("Enter the number of columns (m): "); int m = scan.nextInt(); // Create a 2D array to represent the image char[][] image = new char[n][m]; // Initialize the array with spaces for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { image[i][j] = ' '; // Fill with blank spaces initially } } // Generate n * m / 4 random points int pointsToGenerate = (n * m) / 4; for (int i = 0; i < pointsToGenerate; i++) { // Randomly select a point within the left half of the array int row = random.nextInt(n); int col = random.nextInt(m / 2); // Mark the selected point and its mirrored point image[row][col] = '*'; // Original point image[row][m - col - 1] = '*'; // Mirrored point } // Print the symmetrical image System.out.println("A randomly generated, symmetrical " + n + " x " + m + " image:"); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { System.out.print(image[i][j]); } System.out.println(); // New line after each row } } }
Leave a Comment