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

ضرب مصفوفتين

بدأه نادين01 في 28 ديسمبر 2014 · 3 رد · 1,197 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

 

لدي مشكلة في عملية ضرب مصفوفتين باستخدام Thread

لكن المشكل فالكود ان thread واحد يحسب جميع الاسطر

مثال اذا كانت المصفوفة 3*3 وعدد threads 3  فكل thread يحسب سطر

package thread; import java.lang.*;import java.io.*;import java.util.*; class MatriceOpx extends Thread { static int matrice1[][]; static int vecter[][];//poure jeri les thread static int matrice2[][]; static int resultat[][]; static int n; static int m;  //static int id=2; static int th; MatriceOpx(int i) { 	th=i; this.start(); } public void run(){ int i,j,rang;for(rang=0;rang<n;rang++){	if(vecter[rang][0]==0){for(i=0;i<n;i++) { resultat[rang]=0; for(j=0;j<n;j++) resultat[rang]=resultat[rang]+matrice1[rang][j]*matrice2[j];  } vecter[rang][0]=1; vecter[rang][1]=th; 	} }}public static void main(String args[]) { int i,j,k; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("<-- Multiplication de 2 matrices carres des entiers naturels --> "); Scanner e1= new Scanner(System.in); n = e1.nextInt();System.out.println("Veuillez introduiez le nombre de lignes et de colonnes des matrices : ");System.out.println("Veuillez introduiez le nombre de thread:");try{	m = e1.nextInt();}catch(Exception e){}  if (m > n) {System.out.print("Non, m <= n");System.out.print("Veuillez introduiez le nombre de thread:"); try {	 m = e1.nextInt(); }catch(Exception e){} } matrice1=new int[n][n]; matrice2=new int[n][n]; resultat=new int[n][n]; vecter=new int[n][2]; //inisialisation for(i=0;i<n;i++){ 	 vecter[0]=0; } System.out.println("Veuillez remplir les cases de la matrice une : "); for(i=0;i<n;i++) { for(j=0;j<n;j++) { try { matrice1[j]=150; }catch(Exception e){} } } System.out.println("Veuillez remplir les cases de la matrice deux : "); for(i=0;i<n;i++) { for(j=0;j<n;j++) { try { matrice2[j]=50; }catch(Exception e){} } } MatriceOpx mat[]=new MatriceOpx[m];for(i=0;i<m;i++){ 	 mat=new MatriceOpx(i);  } try {	  for(i=0;i<m;i++) mat.join(); }catch(Exception e){} System.out.println("Ci dessous le resultat :"); for(i=0;i<n;i++){ k=i+1; System.out.println("le ligne numéro:" + k + " dans la matrice resultat fait le calcule à l'aide de thread N° : " +vecter[1] + "");  for(j=0;j<n;j++) System.out.println(resultat[j]); } } }
1
#2

هذه الطريقة قد تساعدك

 

/** * * @author Mohamed */public class Arrays {                public Arrays(){        int[] x = {1,3,5,7,9,11};        int[] y = {2,4,6,8,10,12};                if(x.length == y.length){            for(int i = 0; i < x.length; i++){                System.out.println(x * y);            }        }else{            System.err.println("Arrays is not equal");        }    }        public static void main(String[] args) {        new Arrays();    }}

تم تعديل هذه المشاركة بواسطة Mohamed Hamdan في 29 ديسمبر 2014 في 00:41

#3

لكن اخي الكريم اريد استعمال threads

1
#4

مرحبا اخي هل يمكنك تغيير جمل الطباعة الى اللغة العربية لكي افهم ما تريد لأني لا افهم الفرنسية بالضبط مع العلم اني اريد منك ان تقوم بتغييرها في هذا الكود


 

package arrays;import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.Scanner;public class MatriceOpx extends Thread{        private int [][] resultat,matrice2,vecter,matrice1;    private int Integer1,Integer2,Integer3;        public MatriceOpx(int i){        Integer3 = i;        this.start();    }    public MatriceOpx(){        int i,j,k;        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        System.out.print("Multiplication de 2 matrices carres des entiers naturels : ");        Scanner e1 = new Scanner(System.in);        Integer1 = e1.nextInt();        System.out.print("Veuillez introduiez le nombre de thread: ");        try{            Integer2 = e1.nextInt();        }catch(Exception e){}        if (Integer2 > Integer1) {            System.out.println("Non, Integer2 <= Integer1");        }else{            matrice1 = new int[Integer1][Integer1];            matrice2 = new int[Integer1][Integer1];            resultat = new int[Integer1][Integer1];            vecter = new int[Integer1][2];            for(i = 0; i < Integer1; i++){                vecter[0] = 0;            }            for(i = 0; i < Integer1; i++) {                for(j = 0; j < Integer1; j++){                    try{                        matrice1[j] = 150;                    }catch(Exception e){}                }            }            System.out.println();            System.out.println("Veuillez remplir les cases de la matrice deux : ");            for(i = 0;i < Integer1; i++) {                for(j = 0; j < Integer1; j++){                    try{                        matrice2[j] = 50;                    }catch(Exception e){}                }            }            MatriceOpx mat[] = new MatriceOpx[Integer2];            for(i = 0; i < Integer2; i++){                mat = new MatriceOpx(i);            }            try {                for(i = 0; i < Integer2;i++)                    mat.join();            }catch(Exception e){}            System.out.println();            System.out.println("Ci dessous le resultat :");            System.out.println();                        k = i + 1;            System.out.println("le ligne numéro:" + k + " dans la matrice resultat fait le calcule à l'aide de thread N° : " + vecter[1] + "");            for(j = 0; j < Integer1; j++)                System.out.println(resultat[j]);        }    }     public void run(){        for(int i = 0; i < Integer1; i++){            if(vecter[0] == 0){                for(int c = 0; i < Integer1; c++){                    resultat = 0;                    for(int j = 0; j < Integer1; j++)                    resultat[c] = resultat[c] + matrice1[j] * matrice2[j][c];                }                vecter[0] = 1;                vecter[1] = Integer3;            }        }    }    public static void main(String[] args0){        new MatriceOpx();    }}

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