منتدى مصر التقني
هل تريد التفاعل مع هذه المساهمة؟ كل ما عليك هو إنشاء حساب جديد ببضع خطوات أو تسجيل الدخول للمتابعة.

اكتب برنامج Java يقرأ من مصفوفتين ذات ابعاد من المستخدم و إخراج مصفوفة تمثل حاصل ضرب العناصر

اذهب الى الأسفل

اكتب برنامج Java يقرأ من مصفوفتين ذات ابعاد  من المستخدم و إخراج مصفوفة تمثل حاصل ضرب العناصر Empty اكتب برنامج Java يقرأ من مصفوفتين ذات ابعاد من المستخدم و إخراج مصفوفة تمثل حاصل ضرب العناصر

مُساهمة من طرف أحمد مناع الأحد مارس 27, 2022 10:21 am

أهلا بكم

المثال :
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();
        }
  }
}

ـــــــــــــــــــ التوقيع ــــــــــــــــــــ
سبحان الله وبحمدة .....سبحان الله العظيم
أحمد مناع
أحمد مناع
.
.

تاريخ التسجيل : 15/02/2011
المساهمات : 1108
النقاط : 202034
التقيم : 144
الدولة : مصر
الجنس : ذكر

https://egy-tech.forumegypt.net

الرجوع الى أعلى الصفحة اذهب الى الأسفل

الرجوع الى أعلى الصفحة

ََ

مواضيع ذات صلة


 
صلاحيات هذا المنتدى:
لاتستطيع الرد على المواضيع في هذا المنتدى