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

[ تمت الإجابة ]ضرب المصفوفات البولينيه

مغلق
بدأه justalayoubi في 3 يوليو 2012 · 17 رد · 1,528 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم جميعا اخوتي ...

لدي برنامج (ساعدني فيه اخ كريم ) في جزء من هذا البرنامج عمليه ضرب مصفوفتين , المشكله اني عندما اعطي البرنامج قيم المصفوفتين 0و1(مصفوفات بولينيه) الخرج يكون قيم غير ال0و1

مع العلم اني في البرنامج كتبت شرط للمصفوفه البولينيه ولكن :sad: لا يحقق .

الشرط في البرنامج هكذا

sum = 0;
      if (sum > 1)
        {
            sum = 1;

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

#include <cstdlib>
#include <cstdio>
#include <iostream>

using namespace std;

int main()
{
    int row, col, mult[10][10], k, sum ;
    /////////////////////////////////////////////////////////////////////////////
    cout << endl << "--------------------The Boalen A matrix------------------------------" << endl;
    int a_rows = 0, a_cols = 0;
    cout << "Please enter rows: ";
    cin >> a_rows;
    cout << "Please enter cols: ";
    cin >> a_cols;

    int *pnewrow;
    int **A = new int * [a_rows];
    int i, x;
    for (i = 0; i < a_rows; i++)
    {
        pnewrow = new int[a_cols];
        *(A + i) = pnewrow;
        for (x = 0; x < a_cols; x++)
        {
            cout << "Row: " << i + 1 << " Col: " << x + 1 << ": ";
            cin >> *(*(A + i) + x); // or cin >> *(pnewrow+x);
        }
    }
    //////////////////////////////////////////////////////////////////////////////
    cout << endl << "--------------------The Boalen B matrix------------------------------" << endl;
    int b_rows = 0, b_cols = 0;
    cout << "Please enter rows: ";
    cin >> b_rows;
    cout << "Please enter cols: ";
    cin >> b_cols;
    int **B = new int * [b_rows];
    for (i = 0; i < b_rows; i++)
    {
        pnewrow = new int[b_cols];
        *(B + i) = pnewrow;
        for (x = 0; x < b_cols; x++)
        {
            cout << "Row: " << i + 1 << " Col: " << x + 1 << ": ";
            cin >> *(*(B + i) + x); // or cin >> *(pnewrow+x);
        }
    }
    //////////////////////////////////////////////////////////////////////////////////
    // TRANSPOSE A matrix
    int t_a_rows = a_cols, t_a_cols = a_rows;
    int **t_A = new int * [t_a_rows];

    for (i = 0; i < t_a_rows; i++)
    {
        pnewrow = new int[t_a_cols];
        *(t_A + i) = pnewrow;
        for (x = 0; x < t_a_cols; x++)
            *(*(t_A + i) + x) = *(*(A + x) + i); // or cin >> *(pnewrow+x);        }
    }// end of Transpose A 

    ////////////////////////////////////////////////////////////////////////////////
    ///INVERSE B
    // == 0 --------> 1
    // <> 0 --------> 0  
    int temp = 0;
    for (i = 0; i < b_rows; i++)
        for (x = 0; x < b_cols; x++)
        {
            temp = *(*(B + i) + x);
            *(*(B + i) + x) = (temp == 0) ? 1 : 0; // or cin >> *(pnewrow+x);        
        }
    ///end INVERSE B

    // test programs:
    cout << "Transpose " << endl;
    for (i = 0; i < t_a_rows; i++)
    {
        for (x = 0; x < t_a_cols; x++)
            cout << *(*(t_A + i) + x) << " "; // or cin >> *(pnewrow+x);        }
        cout << endl;
    }// end test of Transpose A 

    cout << "INVERSE" << endl;

    for (i = 0; i < b_rows; i++)
    {
        for (x = 0; x < b_cols; x++)
            cout << *(*(B + i) + x) << " "; // or cin >> *(pnewrow+x);        

        cout << endl;
    } // end test of Inverse B 

	/////////////////////////////////////////////////////////////////
	// Multiply Transpose A & Inverse B (M matrix)
	int k_rows =0, k_cols = 0;
    int INVERSE[10][10];
	int Transpose[10][10] ;

    if(b_rows==a_cols)
    {
		sum = 0;
      if (sum > 1)
        {
            sum = 1;

	for(i=0;i<a_rows;i++)
        {
            for(x=0;x<b_cols;x++)
            {
               // mult[x]=0;
                for(k=0;k<a_rows;k++)
                {
                  // mult[x]= mult[x] + Transpose[k] * INVERSE[k][x];
                   //cout << mult[x] << " "; // or cin >> *(pnewrow+x);

      sum = sum + Transpose[k]*INVERSE[k][x];
        }

       mult[x] = sum;

      }
    } 
  }
}
	cout << "Now we multiply both the above matrix" << endl;
for ( i = 0 ; i <a_rows ; i++ )
    {
      for ( x = 0 ; x < b_cols ; x++ )
        cout << mult[x] << " ";

      cout << endl;
}
//else
  //  {
    //    printf("Matrix multiplication cannot be done");
    //}
/////////////////////////////////////
///INVERSE 
    // == 0 --------> 1
    // <> 0 --------> 0 
    int **K = new int * [k_rows];
    int temp2 = 0;
    for (i = 0; i < k_rows; i++)
        for (x = 0; x < k_cols; x++)
        {
            temp2 = *(*(K + i) + x);
            *(*(K + i) + x) = (temp2 == 0) ? 1 : 0; // or cin >> *(pnewrow+x);        
        }
    ///end INVERSE K

cout << "INVERSE K" << endl;

    for (i = 0; i < k_rows; i++)
    {
        for (x = 0; x < k_cols; x++)
            cout << *(*(K + i) + x) << " "; // or cin >> *(pnewrow+x);        

        cout << endl;
    } // end test of Inverse B
getchar();
getchar(); 
return 0;
}

=';

اما المخرجات فتكون ...

--------------------The Boalen A matrix------------------------
Please enter rows: 2
Please enter cols: 3
Row: 1 Col: 1: 0
Row: 1 Col: 2: 1
Row: 1 Col: 3: 0
Row: 2 Col: 1: 1
Row: 2 Col: 2: 0
Row: 2 Col: 3: 1

--------------------The Boalen B matrix------------------------
Please enter rows: 2
Please enter cols: 2
Row: 1 Col: 1: 0
Row: 1 Col: 2: 0
Row: 2 Col: 1: 1
Row: 2 Col: 2: 1
Transpose
0 1
1 0
0 1
INVERSE
1 1
0 0
Now we multiply both the above matrix
-858993460 -858993460
-858993460 -858993460
INVERSE K

تم تعديل هذه المشاركة بواسطة justalayoubi في 3 يوليو 2012 في 13:17

#2

أخي .. في الحقيقة لدي الكثير من الملاحظات على كودك ...

مثلا ً : ما فائدة الشرط الذي وضعته إذا كان قبله ما يلغيه وأعني :

sum = 0;
  	if (sum > 1)
    	{
        	sum = 1;

أعطيت sum قيمة 0 فلماذا تضع شرطاً بعد ذلك ..

أحد الملاحظات الهامة جدا :

أنت تتعامل مع مصفوفة بوليانية فلماذا تعرفها int !!!

عندما تتعامل مع مصفوفة بوليانية لا تستخدم عملية الجمع بل استخدم العملية المنطقية | or

لدي بعض الملاحظات الأخرى على تعقيد الكود بشكل زائد عن اللزوم ..

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

أنتظر إجابتك ..

تم تعديل هذه المشاركة بواسطة مصطفى 36a2 في 3 يوليو 2012 في 13:36

#3

يتم ضرب المصفوفات المنطقية باستخدام المؤثر and ( && ) و الجمع باستخدام المؤثر or ( | ).

يمكنك تجربة الأمر في الــ MatLab :

post-219439-052305500 1341312206_thumb.p

post-219439-020492800 1341312222_thumb.p

و هذا مثال بسيط على ضرب المصفوفات المنطقية :

#include<iostream>
using namespace std;
#define size 3

int main() {
    bool result;
    int A[size][size] = {
        {1, 0, 1},
        {0, 1, 1},
        {1, 1, 0}
    }, B[size][size] = {
        {0, 0, 0},
        {1, 0, 1},
        {1, 1, 1}
    }, C[size][size];
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            result = false;
            for (int k = 0; k < size; k++)
                result += A[k] && B[k][j];
            C[j] = result;
            cout << C[j] << "\t";
        }
        cout << endl;
    }
    return 0;
}

أرجو لك التوفيق.

المرفقات
MatLab.pngBoolean matrices.png

تم تعديل هذه المشاركة بواسطة أحمد الشنقيطي في 3 يوليو 2012 في 14:28 — السبب: تصحيح الكود.

#4

اخي الكريم انا مبتدئ في ++c

اما الشرط الذي وضعته اذا كانت قيمه sum اكبر من ال0 فليجعلها 1

اما بالنسبه لكتابه الكود فكما تريد ولكن الكود ليس فقط عمليه الضرب ولكن 3 عمليات مع بعضها البعض

#5
اقتباس
يتم ضرب المصفوفات المنطقية باستخدام المؤثر and ( && )

هل أنت متأكد من ذلك أخي أحمد ؟ انظر إلى هذا ..

etT12985.png

أخي

justalayoubi

أنت قمت بإسناد sum=0 ثم قلت( if(sum>1 فهمت عليّ؟ كيف ستكون sum>1 إذا كنت قد أسندت قيمتها للتو ب0 ؟؟

#6

حتي ان لغيت هذا الif البرنامج لايعمل جيدا , لاحظ في المخرجات انا اسندت قيم للمصفوفتين 0 و 1 فقط , المنطقي ان المصفوفه الناتجه تكون عناصرها 0و1 ايضا ولكن المعطي غير ذلك وهذا بغض النظر عن الشرط

تم تعديل هذه المشاركة بواسطة justalayoubi في 3 يوليو 2012 في 14:09

#7
اقتباس
هل أنت متأكد من ذلك أخي أحمد ؟

نعم, أنا متأكد, انظر أنت إلى هذا :) :

#include<iostream>
using namespace std;

int main() {
    bool resul;
    int A[2][2] = {
        {1, 0},
        {0, 1},
    }, B[2][2] = {
        {0, 1},
        {1, 0,}
    }, C[2][2];
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            resul = false;
            for (int k = 0; k < 2; k++)
                resul += A[k] && B[k][j];
            C[j] = resul;
            cout << C[j] << "\t";
        }
        cout << endl;
    }
    return 0;
}
#8

نعم .. أعلم أن الشرط ليس سبب المشكلة ولكنني وضعته كمثال على زيادة الكتابة في الكود بدون أن يكون لذلك فائدة ..

مثال آخر .. أنت تستخدم مصفوفة ثنائية الأبعاد .. فلماذا التعقيد باستخدام

*(*(B + i) + x)

صحيح أنها نقطة تحسب لك في فهم تكافؤ المؤشرات والمصفوفات .. ولكن ألا ترى أنه من الجمل والأسهل كتابة :

A[x]

أحاول الآن تلخيص الكود .. حتى أستطيع معرفة مكان المشكلة أمهلني عشر دقائق أخرى .. wink.gif

#9

اسهل عليك اخي و اني متأكد من ذلك ان سبب المشكله تبدأ من

// Multiply Transpose A & Inverse B (M matrix)
        int k_rows =0, k_cols = 0;
    int INVERSE[10][10];
        int Transpose[10][10] ;

    if(b_rows==a_cols)
    {
                sum = 0;
      if (sum > 1)
        {
            sum = 1;

        for(i=0;i<a_rows;i++)
        {
            for(x=0;x<b_cols;x++)
            {
               // mult[x]=0;
                for(k=0;k<a_rows;k++)
                {
                  // mult[x]= mult[x] + Transpose[k] * INVERSE[k][x];
                   //cout << mult[x] << " "; // or cin >> *(pnewrow+x);

      sum = sum + Transpose[k]*INVERSE[k][x];
        }

       mult[x] = sum;

      }
    } 
  }
}
        cout << "Now we multiply both the above matrix" << endl;
for ( i = 0 ; i <a_rows ; i++ )
    {
      for ( x = 0 ; x < b_cols ; x++ )
        cout << mult[x] << " ";

      cout << endl;
}
//else
  //  {
    //    printf("Matrix multiplication cannot be done");
    //}

و علي ما اظن في معادله الضرب :wink:

#10

@أخي أحمد فهمت عليك .. ظننت أنك ستقوم بالعملية التالية :

	for (int i = 0; i < 2; i++) {
    	for (int j = 0; j < 2; j++) {
        	C[j] =A[j]&B[j];
    	}
	}

@أخي justalayoubi هذا الجزء من الكود صحيح تماماً :

#include <cstdlib>
#include <cstdio>
#include <iostream>

using namespace std;

int main()
{
    int row, col, mult[10][10], k, sum ;
    /////////////////////////////////////////////////////////////////////////////
    cout << endl << "--------------------The Boalen A matrix------------------------------" << endl;
    int a_rows = 0, a_cols = 0;
    cout << "Please enter rows: ";
    cin >> a_rows;
    cout << "Please enter cols: ";
    cin >> a_cols;

    int *pnewrow;
    int **A = new int * [a_rows];
    int i, x;
    for (i = 0; i < a_rows; i++)
    {
        pnewrow = new int[a_cols];
        *(A + i) = pnewrow;
        for (x = 0; x < a_cols; x++)
        {
            cout << "Row: " << i + 1 << " Col: " << x + 1 << ": ";
            cin >> *(*(A + i) + x); // or cin >> *(pnewrow+x);
        }
    }
    //////////////////////////////////////////////////////////////////////////////
    cout << endl << "--------------------The Boalen B matrix------------------------------" << endl;
    int b_rows = 0, b_cols = 0;
    cout << "Please enter rows: ";
    cin >> b_rows;
    cout << "Please enter cols: ";
    cin >> b_cols;
    int **B = new int * [b_rows];
    for (i = 0; i < b_rows; i++)
    {
        pnewrow = new int[b_cols];
        *(B + i) = pnewrow;
        for (x = 0; x < b_cols; x++)
        {
            cout << "Row: " << i + 1 << " Col: " << x + 1 << ": ";
            cin >> *(*(B + i) + x); // or cin >> *(pnewrow+x);
        }
    }
    //////////////////////////////////////////////////////////////////////////////////
    // TRANSPOSE A matrix
    int t_a_rows = a_cols, t_a_cols = a_rows;
    int **t_A = new int * [t_a_rows];

    for (i = 0; i < t_a_rows; i++)
    {
        pnewrow = new int[t_a_cols];
        *(t_A + i) = pnewrow;
        for (x = 0; x < t_a_cols; x++)
            *(*(t_A + i) + x) = *(*(A + x) + i); // or cin >> *(pnewrow+x);        }
    }// end of Transpose A 

    ////////////////////////////////////////////////////////////////////////////////
    ///INVERSE B
    // == 0 --------> 1
    // <> 0 --------> 0  
    int temp = 0;
    for (i = 0; i < b_rows; i++)
        for (x = 0; x < b_cols; x++)
        {
            temp = *(*(B + i) + x);
            *(*(B + i) + x) = (temp == 0) ? 1 : 0; // or cin >> *(pnewrow+x);        
        }
    ///end INVERSE B

    // test programs:
    cout << "Transpose " << endl;
    for (i = 0; i < t_a_rows; i++)
    {
        for (x = 0; x < t_a_cols; x++)
            cout << *(*(t_A + i) + x) << " "; // or cin >> *(pnewrow+x);        }
        cout << endl;
    }// end test of Transpose A 

    cout << "INVERSE" << endl;

    for (i = 0; i < b_rows; i++)
    {
        for (x = 0; x < b_cols; x++)
            cout << *(*(B + i) + x) << " "; // or cin >> *(pnewrow+x);        

        cout << endl;
    } // end test of Inverse B

ولكن من بعده مباشرة بدأ التعقيد .. لماذا أنشأت مصفوفتين جديدتين INVERSE و Transpose وبحجم 10*10 إذا كان لديك t_A والمصفوفة B .. ثم لماذا عرفت mult بمساحة 10*10 من البداية

ولقد اكتشفت خطأك الفادحconfused_smile.gif لقد استعملت المصفوفتين INVERSE و Transpose دون إسناد !! blink.gifblink.gifblink.gif

هل عرفت أين الخطأ ...

سأتابع التصحيح ..

#11

حللت المشكلة .. إليك الحل ...

أولا : أزل الشرط الذي أخبرتك عنه فهو أكبر خطأ laugh.gifوسأخبرك لماذا ...

أنت أسندت لsum قيمة 0 ثم جعلت عملية الضرب كلها داخل عبارة شرطية غيرمحققة وهي :sum>1 وبالتالي لم يدخل البرنامج أصلا لعملية الضرب ...

ولم يقم بها أبداً ...

ثانياً : لا تستعمل المصفوفتين INVERSE و Transpose فهما غير ممهدتان استعمل :t_A , B

ثالثا : أمامك خياران :إما أن تجعل كل int للمصفوفات bool أو أن تكتب بدل

mult[x] = sum;

عليك كتابة

mult[x] =(bool) sum;

أخيراً : بعد كل استعمال للمتحول sum عليك إعادة تهيئته بالصفر ..

وهكذا يصبح الكود (لم أغير فيه أي شيء سوى الملاحظات المذكورة .. ) :

#include <cstdlib>
#include <cstdio>
#include <iostream>

using namespace std;

int main()
{
	int row, col, mult[10][10], k, sum ;
	/////////////////////////////////////////////////////////////////////////////
	cout << endl << "--------------------The Boalen A matrix------------------------------" << endl;
	int a_rows = 0, a_cols = 0;
	cout << "Please enter rows: ";
	cin >> a_rows;
	cout << "Please enter cols: ";
	cin >> a_cols;

	int *pnewrow;
	int **A = new int * [a_rows];
	int i, x;
	for (i = 0; i < a_rows; i++)
	{
    	pnewrow = new int[a_cols];
    	*(A + i) = pnewrow;
    	for (x = 0; x < a_cols; x++)
    	{
        	cout << "Row: " << i + 1 << " Col: " << x + 1 << ": ";
        	cin >> *(*(A + i) + x); // or cin >> *(pnewrow+x);
    	}
	}
	//////////////////////////////////////////////////////////////////////////////
	cout << endl << "--------------------The Boalen B matrix------------------------------" << endl;
	int b_rows = 0, b_cols = 0;
	cout << "Please enter rows: ";
	cin >> b_rows;
	cout << "Please enter cols: ";
	cin >> b_cols;
	int **B = new int * [b_rows];
	for (i = 0; i < b_rows; i++)
	{
    	pnewrow = new int[b_cols];
    	*(B + i) = pnewrow;
    	for (x = 0; x < b_cols; x++)
    	{
        	cout << "Row: " << i + 1 << " Col: " << x + 1 << ": ";
        	cin >> *(*(B + i) + x); // or cin >> *(pnewrow+x);
    	}
	}
	//////////////////////////////////////////////////////////////////////////////////
	// TRANSPOSE A matrix
	int t_a_rows = a_cols, t_a_cols = a_rows;
	int **t_A = new int * [t_a_rows];

	for (i = 0; i < t_a_rows; i++)
	{
    	pnewrow = new int[t_a_cols];
    	*(t_A + i) = pnewrow;
    	for (x = 0; x < t_a_cols; x++)
        	*(*(t_A + i) + x) = *(*(A + x) + i); // or cin >> *(pnewrow+x);    	}
	}// end of Transpose A 

	////////////////////////////////////////////////////////////////////////////////
	///INVERSE B
	// == 0 --------> 1
	// <> 0 --------> 0  
	int temp = 0;
	for (i = 0; i < b_rows; i++)
    	for (x = 0; x < b_cols; x++)
    	{
        	temp = *(*(B + i) + x);
        	*(*(B + i) + x) = (temp == 0) ? 1 : 0; // or cin >> *(pnewrow+x);    	
    	}
	///end INVERSE B

	// test programs:
	cout << "Transpose " << endl;
	for (i = 0; i < t_a_rows; i++)
	{
    	for (x = 0; x < t_a_cols; x++)
        	cout << *(*(t_A + i) + x) << " "; // or cin >> *(pnewrow+x);    	}
    	cout << endl;
	}// end test of Transpose A 

	cout << "INVERSE" << endl;

	for (i = 0; i < b_rows; i++)
	{
    	for (x = 0; x < b_cols; x++)
        	cout << *(*(B + i) + x) << " "; // or cin >> *(pnewrow+x);    	

    	cout << endl;
	} // end test of Inverse B 

    	/////////////////////////////////////////////////////////////////
    	// Multiply Transpose A & Inverse B (M matrix)
    	int k_rows =0, k_cols = 0;
	int INVERSE[10][10];
    	int Transpose[10][10] ;

	if(b_rows==a_cols)
	{
            	sum = 0;

    	for(i=0;i<a_rows;i++)
    	{
        	for(x=0;x<b_cols;x++)
        	{
           	// mult[x]=0;
            	for(k=0;k<a_rows;k++)
            	{
              	// mult[x]= mult[x] + Transpose[k] * INVERSE[k][x];
               	//cout << mult[x] << " "; // or cin >> *(pnewrow+x);

  	sum = sum + t_A[k]*B[k][x];
    	}

   	mult[x] = (bool)sum;
  	sum = 0;
	} 
  }
}
    	cout << "Now we multiply both the above matrix" << endl;
for ( i = 0 ; i <a_rows ; i++ )
	{
  	for ( x = 0 ; x < b_cols ; x++ )
    	cout << mult[x] << " ";

  	cout << endl;
}
//else
  //  {
	//	printf("Matrix multiplication cannot be done");
	//}
/////////////////////////////////////
///INVERSE 
	// == 0 --------> 1
	// <> 0 --------> 0 
	int **K = new int * [k_rows];
	int temp2 = 0;
	for (i = 0; i < k_rows; i++)
    	for (x = 0; x < k_cols; x++)
    	{
        	temp2 = *(*(K + i) + x);
        	*(*(K + i) + x) = (temp2 == 0) ? 1 : 0; // or cin >> *(pnewrow+x);    	
    	}
	///end INVERSE K

cout << "INVERSE K" << endl;

	for (i = 0; i < k_rows; i++)
	{
    	for (x = 0; x < k_cols; x++)
        	cout << *(*(K + i) + x) << " "; // or cin >> *(pnewrow+x);    	

    	cout << endl;
	} // end test of Inverse B
getchar();
getchar(); 
return 0;
}

والله ولي التوفيق

السلام عليكم

#12

و لكن المخرجات مازالت كما هي , واليك المخرجات بعد التعديلات التي ذكرتها

--------------------The Boalen A matrix---------------------------
Please enter rows: 3
Please enter cols: 4
Row: 1 Col: 1: 0
Row: 1 Col: 2: 0
Row: 1 Col: 3: 1
Row: 1 Col: 4: 1
Row: 2 Col: 1: 0
Row: 2 Col: 2: 0
Row: 2 Col: 3: 1
Row: 2 Col: 4: 1
Row: 3 Col: 1: 0
Row: 3 Col: 2: 0
Row: 3 Col: 3: 1
Row: 3 Col: 4: 1

--------------------The Boalen B matrix---------------------------
Please enter rows: 3
Please enter cols: 3
Row: 1 Col: 1: 0
Row: 1 Col: 2: 1
Row: 1 Col: 3: 1
Row: 2 Col: 1: 1
Row: 2 Col: 2: 1
Row: 2 Col: 3: 0
Row: 3 Col: 1: 1
Row: 3 Col: 2: 1
Row: 3 Col: 3: 1
Transpose
0 0 0
0 0 0
1 1 1
1 1 1
INVERSE
1 0 0
0 0 1
0 0 0
Now we multiply both the above matrix
-858993460 -858993460 -858993460
-858993460 -858993460 -858993460
-858993460 -858993460 -858993460
INVERSE K




= ';
#13

طبعاً أخي لأنك خالفت شرط الضرب

  if(b_rows==a_cols)

وهذا من نقص الكود .. وعدم اشتماله لحالة عدم التساوي ..

أضف عبارة else بعد الif وستحل مشكلتك .. حاول إدخال المصفوفتين 3*3 وعندها ستجد أن الكود يعمل بشكل صحيح :

--------------------The Boalen A matrix------------------------------
Please enter rows: 3
Please enter cols: 3
Row: 1 Col: 1: 1
Row: 1 Col: 2: 1
Row: 1 Col: 3: 0
Row: 2 Col: 1: 0
Row: 2 Col: 2: 1
Row: 2 Col: 3: 1
Row: 3 Col: 1: 1
Row: 3 Col: 2: 0
Row: 3 Col: 3: 1

--------------------The Boalen B matrix------------------------------
Please enter rows: 3
Please enter cols: 3
Row: 1 Col: 1: 1
Row: 1 Col: 2: 0
Row: 1 Col: 3: 1
Row: 2 Col: 1: 0
Row: 2 Col: 2: 1
Row: 2 Col: 3: 0
Row: 3 Col: 1: 1
Row: 3 Col: 2: 0
Row: 3 Col: 3: 1
Transpose
1 0 1
1 1 0
0 1 1
INVERSE
0 1 0
1 0 1
0 1 0
Now we multiply both the above matrix
0 1 0
1 1 1
1 1 1
INVERSE K

أرجو ان تكون الفكرة وصلت ... طالما أن شرط الضرب محقق فالكود يعمل بشكل صحيح ..

وحما أخبرتك عليك إضافة شرط عدم تحقق شرط الضرب حنى يكون كودك شاملا لجميع الحالات

تم تعديل هذه المشاركة بواسطة مصطفى 36a2 في 3 يوليو 2012 في 15:59

#14

نعم يعمل ولكن فقط في حاله ان المصفوفتين من نفس المرتبه :) في حين ان الشرط يقول : عدد صفوف المصفوفه الثانيه = عدد اعمده المصفوفه الاولي :blink:

اي يجب علي الكود ان يعمل اذا كانت المصفوفه الاولي 3*2 و المصفوفه الثانيه 3*3

#15

لماذا تستخدم اصلا مصفوفه من النوع int على ما اظن ان C++ بها نوع اسمه bool على غرار C

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#16

الحمدلله هذه المشكله الاولي قد اتحلت (تقريبا) :lol: اما المشكله الثانيه فهي في عكس ناتج الضرب (ايجاد معكوس المصفوفه الناتجه من عمليه الضرب السابقه :wacko: ) .

/////////////////////////////////////
///INVERSE 
        // == 0 --------> 1
        // <> 0 --------> 0 
       // int **K = new int * [k_rows];
        int temp2 = mult[x];
        for (i = 0; i < t_a_rows; i++)
        for (x = 0; x < b_cols; x++)
        {
                temp2 = sum;
                sum = (temp2 == 1) ? 1 : 0; // or cin >> *(pnewrow+x);      
        }
        ///end INVERSE K

cout << "Inverse the above matrix---------------------------------------------" << endl;

        for (i = 0; i < t_a_rows; i++)
        {
        for (x = 0; x < b_cols; x++)
                cout << sum << " "; // or cin >> *(pnewrow+x);      

        cout << endl;
        } // end test of Inverse 

return 0;
}
=';

المشكله دائما يعطيني الناتج اما كله اصفار او كله آحاد , كما في الشكل

--------------------The Boalen A matrix------------------------------
Please enter rows: 3
Please enter cols: 3
Row: 1 Col: 1: 1
Row: 1 Col: 2: 1
Row: 1 Col: 3: 0
Row: 2 Col: 1: 0
Row: 2 Col: 2: 0
Row: 2 Col: 3: 1
Row: 3 Col: 1: 1
Row: 3 Col: 2: 1
Row: 3 Col: 3: 1

--------------------The Boalen B matrix------------------------------
Please enter rows: 3
Please enter cols: 3
Row: 1 Col: 1: 0
Row: 1 Col: 2: 1
Row: 1 Col: 3: 0
Row: 2 Col: 1: 0
Row: 2 Col: 2: 1
Row: 2 Col: 3: 1
Row: 3 Col: 1: 0
Row: 3 Col: 2: 1
Row: 3 Col: 3: 1

---------------------Transpose A-------------------------------------
1 0 1
1 0 1
0 1 1

--------------------Inverse B-----------------------------------------
1 0 1
1 0 0
1 0 0
Now we multiply both the above matrix---------------------------------
1 0 1
1 0 1
1 0 0
Inverse the above matrix---------------------------------------------
0 0 0
0 0 0
0 0 0


=';
#17

حل مشكلتك بسيط جداً ... ولكنني أستغرب من نوعية الأخطاء التي ترتكبها أخي!!

حاول التركيز قليلا أثناء كتابة الكود ...

حسناً .. أرجو أن أفيدك اليوم .. أظن أن هدفك الآن هو عكس عناصر المصفوفة أي استبدال كل 0 ب 1 وكل 1 ب 0

وعلى حسب فهمي فقد أخطأت خطئين كل واحد أكبر من الثاني ...

الخطأ الأول :

 sum = (temp2 == 1) ? 1 : 0;

أنت تخزن ناتج العكس في المتحول sum ولا تعيد تخزينه في المصفوفة ... أي أن عملية العكس لا يُحتفظ بناتجها ...

أما الخطأ الثاني :

cout << sum << " ";

لماذا أخي ؟؟؟ لماذا؟

المتحول sum يحتفظ بآخر قيمة قام بعكسها... وبالتالي لن يعرض إلا 1 أو 0 ..

إليك التصحيح الآن :

///INVERSE 
    	// == 0 --------> 1
    	// <> 0 --------> 0 
   	// int **K = new int * [k_rows];
    	int temp2 = mult[x];
    	for (i = 0; i < t_a_rows; i++)
    	for (x = 0; x < b_cols; x++)
    	{
            	temp2 = mult[x]
            	mult[x]= (temp2 == 1) ? 1 : 0; // or cin >> *(pnewrow+x);  	
    	}
    	///end INVERSE K

cout << "Inverse the above matrix---------------------------------------------" << endl;

    	for (i = 0; i < t_a_rows; i++)
    	{
    	for (x = 0; x < b_cols; x++)
            	cout << mult[x]<< " "; // or cin >> *(pnewrow+x);  	

    	cout << endl;
    	} // end test of Inverse 

return 0
}

كملاحظة أخيرة : لا تخف من استخدام نفس المتحول في الطرفين ... لاحظت أنك تحب استخدام المتحولات المؤقتةtemp ولا أرى لها أي داع...

والحل الذي قدمته لك لم أغير فيه إلا ما نبهتك عليه ...

هذا إذا كان فهمي لما تريده صحيحا .. أما لو كان غير ذلك ,,, فنفس الملاحظات سأكررها لك ... لأن الخطئين نفسهما لأي عمل تريد القيام به على

المصفوفة ...

والله ولي التوفيق

تم تعديل هذه المشاركة بواسطة مصطفى 36a2 في 4 يوليو 2012 في 19:27

1
#18

شكرا اخي علي الملاحظه ...

استخدامي ل sum علي غرار انني عرفت mult[x] = sum في الجزء الاسبق (عند اجراء عمليه الضرب) :S . قمت بالتعديلات التي ذكرتها والآن الكود يعمل جيدا :haha: ..

جزآكم الله الف خير و زادكم تقدما ورفعا ...

هذا الموضوع مغلق.

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

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

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

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

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