الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

اريد مساعده في برنامج ايجاد حاصل ضرب مصفوفتين

بدأه sarah2010 في 19 يناير 2010 · 7 رد · 9,024 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

هذا البرنامج عبارة عن ضرب مصفوفتين a ,b والناتج يوضع في المصفوفة c. ويجب انه العملية الحسابية توضع في Method تسمى multiplyMatrix

حاولت في هذا البرنامج اني اقوم بضرب مصفوفتينa,b والناتج يوضع في المتغير c بس المشكلة انه المخرجات بتكون غير مش على شكل مصفوفة

هذا هو البرنامج

package secondsemesterassignment1;

public class MultiplyingTwoMatrices{

 public static int[][] multiplyMatrix(int[][]a1 ,int[][]b1){

 int [][]a;
 int [][]b;
 int [][]c;

 int x,y,z;
 a= new int[3][3];
 b=new int[3][3];
 //c= new int [][];
 c= new int[a.length][b[0].length];

 for(x = 0 ; x < c.length ; x++)
 for(y = 0 ; y < c[0].length ; y++)
 for(z = 0; z < b.length ; z++)

 c[x][y]=c[x][y]+a[x][z]*b[z][y];
 return c;

 }//end MultiplyMatrix

 public static void main(String [] args){
 int [][]a ={{1,2,3},{4,5,6},{7,8,9}} ;
 int [][]b ={{2,3,4},{5,6,7},{8,9,1}};

 System.out.print("The multiplication of the two mitricies is "+
 MultiplyingTwoMatrices.multiplyMatrix(a, b));

 }//end main
}//end class

تم تعديل هذه المشاركة بواسطة herch في 19 يناير 2010 في 12:52 — السبب: لا تنس استعمال الوسم [code]

1
#2

للمرة الثانية: يرجى استخدام وسوم code مع الكود لكي يظهر بشكل مناسب

لا يمكن طباعة مصفوفة بالطريقة التي استخدمتيها:

System.out.print("The multiplication of the two mitricies is "+ MultiplyingTwoMatrices.multiplyMatrix(a, b));

بل عليك كتابة طريقة خاصة بطباعة مصفوفة ثنائية البعد، كهذه مثلاً:

    public void printm(int[][] m){
        for(int[] mm: m){
            for(int mmm :mm){
                System.out.print(" " + mmm);
            }
                System.out.println();
        }
        System.out.println();
    }

البرنامج لا يعمل على أية حال، حاولي فيه مجدداً

#3

في multiplyMatrix، أنت تمررين المصفوفتين اللتين تريدين ضرب عناصرهما، لكنك داخل ال method كنت تنشئين مصفوفتين جديدتين، وتهملين المصفوفتين الممررتين.

عملت 3 loops في برنامجك، لكنك تحتاجين فقط ل 2 loops لأن المصفوفات هي ذات بعدين فقط وليس ثلاثة.

import java.util.Arrays;

public class MultiplyingTwoMatrices {

	public static int[][] multiplyMatrix(int[][] a, int[][] b) {

		int x, y;
		int[][] c = new int[a.length][b[0].length];

		for (x = 0; x < c.length; x++) {
			for (y = 0; y < c[0].length; y++) {
					c[x][y] = a[x][y] * b[x][y];
			}
		}
		return c;

	}//end MultiplyMatrix

	public static void main(String[] args) {
		int[][] a = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
		int[][] b = {{2, 3, 4}, {5, 6, 7}, {8, 9, 1}};
		int[][] c = MultiplyingTwoMatrices.multiplyMatrix(a, b);
		System.out.print("The multiplication of the two matricies is ");
		for (int i = 0; i < c.length; i++) {
			System.out.println(Arrays.toString(c));
		}

	}//end main
}//end class
#4

رياضياً: جداء مصفوفتين هو عبارة عن مصفوفة لها نفس عدد أسطر المصفوفة الأولى ونفس عدد أعمدة المصفوفة الثانية (ليس عملية تبديلية)

بحيث يكون قيمة كل عنصر ذو ترتيب (x,y) من المصفوفة الناتجة تساوي جداء السطر x من المصفوفة الأولى مع العمود y من الثانية

public class MultiplyingTwoMatrices {

    public MultiplyingTwoMatrices() {
    }

    public int[][] multiplyMatrix(int[][] a, int[][] b) {
        int[][] c = new int[a.length][b[0].length];

        for (int x = 0; x < c.length; x++) {
            for (int y = 0; y < c[0].length; y++) {
                int sum = 0;
                for (int i = 0; i < a[0].length; i++) {
                     sum += a[x] * b[y];
                }
                c[x][y] = sum;
            }
        }
        return c;
    }

    public void printm(int[][] m){
        for(int[] mm: m){
            for(int mmm :mm){
                System.out.print(" " + mmm);
            }
                System.out.println();
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int[][] a = {{1, 2, 3},
                    {4, 5, 6},
                    {7, 8, 9}};

        int[][] b = {{2, 3, 4},
                    {5, 6, 7},
                    {8, 9, 1}};

        MultiplyingTwoMatrices nc = new MultiplyingTwoMatrices();
        nc.printm(a);
        nc.printm(b);
        int[][] c = nc.multiplyMatrix(a, b);
        System.out.print("The multiplication of the two mitricies is: ");
        nc.printm(c);
    }
}

تم تعديل هذه المشاركة بواسطة Speed_Of_Light في 19 يناير 2010 في 18:28

1
#5

فعلا، لم أنتبه لهذه النقطة.

ليس الامر غريبا، كنت دائما سيء في الرياضيات :lol:

#6

اشكرك اخيspeed_of_light

بس كيف ممكن اني اعملها في فقط 2 methods

لاني حاولت ما طلعت معي النتيجة صح ,كمان ايش تقصد بالكود اني اعملها فيcode ؟؟؟

Speed_Of_Light كتب:

رياضياً: جداء مصفوفتين هو عبارة عن مصفوفة لها نفس عدد أسطر المصفوفة الأولى ونفس عدد أعمدة المصفوفة الثانية (ليس عملية تبديلية)

بحيث يكون قيمة كل عنصر ذو ترتيب (x,y) من المصفوفة الناتجة تساوي جداء السطر x من المصفوفة الأولى مع العمود y من الثانية

public class MultiplyingTwoMatrices {

    public MultiplyingTwoMatrices() {
    }

    public int[][] multiplyMatrix(int[][] a, int[][] b) {
        int[][] c = new int[a.length][b[0].length];

        for (int x = 0; x < c.length; x++) {
            for (int y = 0; y < c[0].length; y++) {
                int sum = 0;
                for (int i = 0; i < a[0].length; i++) {
                     sum += a[x] * b[y];
                }
                c[x][y] = sum;
            }
        }
        return c;
    }

    public void printm(int[][] m){
        for(int[] mm: m){
            for(int mmm :mm){
                System.out.print(" " + mmm);
            }
                System.out.println();
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int[][] a = {{1, 2, 3},
                    {4, 5, 6},
                    {7, 8, 9}};

        int[][] b = {{2, 3, 4},
                    {5, 6, 7},
                    {8, 9, 1}};

        MultiplyingTwoMatrices nc = new MultiplyingTwoMatrices();
        nc.printm(a);
        nc.printm(b);
        int[][] c = nc.multiplyMatrix(a, b);
        System.out.print("The multiplication of the two mitricies is: ");
        nc.printm(c);
    }
}

#7

أشكرك اخي herch

انا حاولت اعملها في 4loops وما طلعت معي وحاولت اني اعملها 3 loop.

herch كتب:

في multiplyMatrix، أنت تمررين المصفوفتين اللتين تريدين ضرب عناصرهما، لكنك داخل ال method كنت تنشئين مصفوفتين جديدتين، وتهملين المصفوفتين الممررتين.

عملت 3 loops في برنامجك، لكنك تحتاجين فقط ل 2 loops لأن المصفوفات هي ذات بعدين فقط وليس ثلاثة.

import java.util.Arrays;

public class MultiplyingTwoMatrices {

	public static int[][] multiplyMatrix(int[][] a, int[][] b) {

		int x, y;
		int[][] c = new int[a.length][b[0].length];

		for (x = 0; x < c.length; x++) {
			for (y = 0; y < c[0].length; y++) {
					c[x][y] = a[x][y] * b[x][y];
			}
		}
		return c;

	}//end MultiplyMatrix

	public static void main(String[] args) {
		int[][] a = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
		int[][] b = {{2, 3, 4}, {5, 6, 7}, {8, 9, 1}};
		int[][] c = MultiplyingTwoMatrices.multiplyMatrix(a, b);
		System.out.print("The multiplication of the two matricies is ");
		for (int i = 0; i < c.length; i++) {
			System.out.println(Arrays.toString(c));
		}

	}//end main
}//end class

#8

انظري جيداً سارة !

الأكواد كلها أمامك ! عليك أن تتعلمي قرائتها !

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…