Update lab2

This commit is contained in:
kxtzzl 2023-04-05 12:30:01 +03:00 committed by GitHub
parent 4f86e8cf76
commit 511422ad6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 6 deletions

15
lab2
View File

@ -1,5 +1,6 @@
import java.util.Scanner; import java.util.Scanner;
import java.util.Random; import java.util.Random;
public class Lab2 { public class Lab2 {
public static void main(String[] args) { public static void main(String[] args) {
int C5, C11, C7; int C5, C11, C7;
@ -28,8 +29,7 @@ public class Lab2 {
max = sc.nextInt(); max = sc.nextInt();
if (min >= -128 && max <= 127 && min < max) { if (min >= -128 && max <= 127 && min < max) {
valid = true; valid = true;
} } else {
else {
System.out.println("\nInvalid input. Please enter the values again."); System.out.println("\nInvalid input. Please enter the values again.");
} }
} }
@ -50,24 +50,27 @@ public class Lab2 {
System.out.print("\nEnter the constant to multiply the matrix by: "); System.out.print("\nEnter the constant to multiply the matrix by: ");
int constant = sc.nextInt(); int constant = sc.nextInt();
byte[][] multipliedMatrix = new byte[rows][cols];
System.out.println(" The matrix multiplied by " + constant + " is:"); System.out.println(" The matrix multiplied by " + constant + " is:");
for (int i = 0; i < rows; i++) { for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) { for (int j = 0; j < cols; j++) {
System.out.printf(" %-5d", matrix[i][j] * constant); multipliedMatrix[i][j] = (byte) (matrix[i][j] * constant);
System.out.printf(" %-5d", multipliedMatrix[i][j]);
} }
System.out.println(); System.out.println();
} }
System.out.println("\n Press enter to search for the average value of the matrix."); System.out.println("\n Press 'enter' to search for the average value of the matrix.");
sc.nextLine(); sc.nextLine();
sc.nextLine(); sc.nextLine();
int sum = 0; int sum = 0;
for (int i = 0; i < rows; i++) { for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) { for (int j = 0; j < cols; j++) {
sum += matrix[i][j]; sum += multipliedMatrix[i][j];
} }
} }
double average = (double) sum / (rows * cols); double average = (double) sum / (rows * cols);
System.out.println("The average value of the matrix is: " + average); System.out.println("The average value of the multiplied matrix is: " + average);
} }
} }