اكتب برنامج Java يقرأ من مصفوفتين ذات ابعاد من المستخدم و إخراج مصفوفة تمثل حاصل ضرب العناصر
منتدى مصر التقني :: قسم لغات البرمجة Programming languages :: لغة الجافا Java :: تمارين بلغة الجافا مع الحل Java Solved Programs with solutions
صفحة 1 من اصل 1 • شاطر
اكتب برنامج Java يقرأ من مصفوفتين ذات ابعاد من المستخدم و إخراج مصفوفة تمثل حاصل ضرب العناصر
أهلا بكم
المثال :
الحل :
المثال :
Write a Java or C++ program that reads from user 4x3 matrix (A) and 3x5 Matrix (B) and produces the matrix ( C ) with size 4x5 such that C = A * B.
الحل :
- الكود:
import java.util.Scanner;
public class Main
{
public static void main (String args[])
{
Scanner sc=new Scanner(System.in);
int row_A = 4; //taking row as input
int col_A = 3; //taking column as input
int row_B = 3;
int col_B = 5;
// Declaring the two-dimensional matrix (A)
int matrix_A[][] = new int[row_A][col_A];
int matrix_B[][] = new int[row_B][col_B];
// Read the matrix (A) values
System.out.println ("Enter the elements of the matrix A: ");
//loop for row
for (int i = 0; i < row_A; i++)
//inner for loop for column
for (int j = 0; j < col_A; j++)
matrix_A[i][j] = sc.nextInt ();
// Read the matrix (B) values
System.out.println ("Enter the elements of the matrix B: ");
for (int i = 0; i < row_B; i++)
for (int j = 0; j < col_B; j++)
matrix_B[i][j] = sc.nextInt ();
// Mutliplying Two matrices
int[][] C = new int[row_A][col_B];
for(int i = 0; i < row_A; i++) {
for (int j = 0; j < col_B; j++) {
for (int k = 0; k < col_A; k++) {
C[i][j] += matrix_A[i][k] * matrix_B[k][j];
}
}
}
//accessing array elements
System.out.println ("Elements of the array_A are: ");
for (int i = 0; i < row_A; i++)
{
for (int j = 0; j < col_A; j++)
//prints the array elements
System.out.print (matrix_A[i][j] + " ");
//throws the cursor to the next line
System.out.println ();
}
System.out.println ("Elements of the array_B are: ");
for (int i = 0; i < row_B; i++)
{
for (int j = 0; j < col_B; j++)
//prints the array elements B
System.out.print (matrix_B[i][j] + " ");
System.out.println ();
}
// Displaying the result
System.out.println("Multiplication of two matrices is: ");
for(int[] row : C) {
for (int column : row) {
System.out.print(column + " ");
}
System.out.println();
}
}
}
ـــــــــــــــــــ التوقيع ــــــــــــــــــــ
سبحان الله وبحمدة .....سبحان الله العظيم
| |
منتدى مصر التقني :: قسم لغات البرمجة Programming languages :: لغة الجافا Java :: تمارين بلغة الجافا مع الحل Java Solved Programs with solutions
صفحة 1 من اصل 1
صلاحيات هذا المنتدى:
لاتستطيع الرد على المواضيع في هذا المنتدى
أمس في 6:10 pm من طرف moslema_r
» افضل شركة رش مبيدات بالرياض
الإثنين أبريل 29, 2024 9:00 pm من طرف moslema_r
» ارخص شركة مكافحة الصراصير بالرياض
الخميس أبريل 25, 2024 8:56 pm من طرف moslema_r
» شركة مكافحة حشرات في راس الخيمة
الخميس أبريل 25, 2024 8:34 pm من طرف moslema_r
» تطبيقIncogniton لإدارة ملفات تعريف متعددة للمتصفح
الأربعاء أبريل 24, 2024 3:51 pm من طرف زاكااا
» افضل شركة مكافحة النمل الابيض بالرياض
الإثنين أبريل 22, 2024 9:41 pm من طرف moslema_r
» Exemplary Сasual Dating - Verified Women
الخميس أبريل 18, 2024 9:18 pm من طرف wdqwd
» رش الدفان بالرياض
الخميس أبريل 18, 2024 7:11 pm من طرف moslema_r
» شركة تنظيف خزانات الشارقة
الخميس أبريل 18, 2024 6:57 pm من طرف moslema_r
» شركة مكافحة الفئران بالرياض
الإثنين أبريل 15, 2024 8:23 pm من طرف moslema_r