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

كود : Reduced Row Echelon Form

بدأه hamany-pirlo في 24 أغسطس 2009 · 5 رد · 926 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكــم ورحمـة الله وبركاتــه ،،

I am trying to make a program that transforms a normal matrix into the r.r.e.f. form (linear algebra).

The problem is that when running the program it only does the first if rule 
:huh: .

I should say that im only a beginner and im trying to learn from this.

P.S. : the code is written in dev-cpp . 

/* Info. : A program for converting any matrix into an r.r.e.f. form

 */

/*----------------------------------------------------------------------------*/

#include<iostream>

using namespace std;

int main()

{

 int r ,c ,i ,j ;

 

 cout<<"Enter the number of rows : "; //reading data (input)  

 cin>>r;

 cout<<"Enter the number of columns : ";

 cin>>c;

 

 cout<<endl<<endl;

 float row[r][c] ,col[c][r], temp ;

 for(i=0 ; i<r ; i++ )

  {

  cout<<"Enter the elements of the ( "<<i+1<<" ) row : ";

 

  for(i=i , j=0 ; j<c ; j++ )

  {

  cin>>row
[j];

  }

  }

 

 cout<<endl<<endl;

 

 if ( row[0][0]>0 ) //r.r.e.f.

 {

  temp=row[0][0] ;

   

  for(j=0 ; j<c ; j++)

  {

  row[0][j]=row[0][j]/temp ;  

  }

   

  }

   

  else if ( row[0][0]<0 ) //here is the problem!

 {

  temp=row[0][0]*-1 ;

   

  for(j=0 ; j<c ; j++)

  {

  row[0][j]=row[0][j]/temp ;  

  }

   

  }

   

   

   

 

/*----------------------------------------------------------------------------*/

 for(i=0 ; i<r ; i++ ) //checking data (output)

 {

  cout<<"The elements of the ( "<<i+1<<" ) row : ";

   

  for(i=i , j=0 ; j<c ; j++)

  {

  cout<<row
[j]<<" ";

   

  }

  cout<<endl;

  }

cout<<endl<<endl;

system("pause");

return 0;

}

تم تعديل هذه المشاركة بواسطة hamany-pirlo في 24 أغسطس 2009 في 04:39

Program every day for 20 years and you will become a good programmer. Program every day for 10 years and study algorithms on the side and you will become a great programmer !

على الأقل هذا اللي سمعته ...

#2

المصفوفات تعريفها لديك فيه مشكل:

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int r ,c ,i ,j ;
  7.  
  8.  
  9.  
  10. cout<<"Enter the number of rows : "; //reading data (input)
  11. cin>>r;
  12. cout<<"Enter the number of columns : ";
  13. cin>>c;
  14.  
  15. cout<<endl<<endl;
  16.  
  17. float **row /*row[r][c]*/ ,**col /*col[c][r]*/, temp ;
  18. row=new float*[r];
  19.  
  20. for(i=0 ; i<r ; i++ )
  21. row[i]=new float[c];
  22.  
  23. col=new float* [c];
  24. for(i=0 ; i<c ; i++ )
  25. col[i]=new float[r];
  26.  
  27. for(i=0 ; i<r ; i++ )
  28. {
  29. cout<<"Enter the elements of the ( "<<i+1<<" ) row : ";
  30.  
  31. for(i=i , j=0 ; j<c ; j++ )
  32. {
  33. cin>>row[i][j];
  34. }
  35. }
  36.  
  37. cout<<endl<<endl;
  38.  
  39. if ( row[0][0]>0 ) //r.r.e.f.
  40. {
  41. temp=row[0][0] ;
  42.  
  43. for(j=0 ; j<c ; j++)
  44. {
  45. row[0][j]=row[0][j]/temp ;
  46. }
  47.  
  48. }
  49.  
  50. else if ( row[0][0]<0 ) //here is the problem!
  51. {
  52. temp=row[0][0]*-1 ;
  53.  
  54. for(j=0 ; j<c ; j++)
  55. {
  56. row[0][j]=row[0][j]/temp ;
  57. }
  58.  
  59. }
  60.  
  61.  
  62.  
  63.  
  64. /*----------------------------------------------------------------------------*/
  65.  
  66.  
  67. for(i=0 ; i<r ; i++ ) //checking data (output)
  68. {
  69. cout<<"The elements of the ( "<<i+1<<" ) row : ";
  70.  
  71. for(i=i , j=0 ; j<c ; j++)
  72. {
  73. cout<<row[i][j]<<" ";
  74.  
  75. }
  76. cout<<endl;
  77. }
  78.  
  79.  
  80. cout<<endl<<endl;
  81.  
  82. system("pause");
  83. return 0;
  84. }
  85.  
  86.  

tvquran_6.gif

#3

شكرا لك أخي الكريم على هذا التصحيح
:happy:
 ، و لكن المشكلة مازالت قائمة بحيث أنه لما أشغل البرنامج فقط يأخذ

 
if rule
الأول

 ! و حتى لو إني حطيت عدد سالب هم يأخذ الأول فقط

Program every day for 20 years and you will become a good programmer. Program every day for 10 years and study algorithms on the side and you will become a great programmer !

على الأقل هذا اللي سمعته ...

#4

ماذا تريد ان تعمل بالضبط ؟.

tvquran_6.gif

#5

أريد عمل برنامج يقرأ المصفوفة ثم يعمل على الصفوف عمليات ( جمع ، طرح ، قسمة ، ضرب ) بحيث أوصلها إلى شكل 

r.r.e.f. ( reduced row echlon form )

و من ثم طباعة المصفوفة بعد التعديل ، و طريقة التعديل هذه مأخوذة من مادة الجبر الخطي

Program every day for 20 years and you will become a good programmer. Program every day for 10 years and study algorithms on the side and you will become a great programmer !

على الأقل هذا اللي سمعته ...

#6

وجدت هذه الخوارزميه وحولتها لكود سي ++ جربها ان شاء الله تكون مضبوطه

هذه الخوازمية او ما يسمى "pseudocode":

lead := 0

rowCount := the number of rows in M

columnCount := the number of columns in M

for 0 ≤ r < rowCount do

if columnCount ≤ lead then

stop

end if

i = r

while M[i, lead] = 0 do

i = i + 1

if rowCount = i then

i = r

lead = lead + 1

if columnCount = lead then

stop

end if

end if

end while

Swap rows i and r

Divide row r by M[r, lead]

for 0 ≤ i < rowCount do

if i ≠ r do

Subtract M[i, lead] multiplied by row r from row i

end if

end for

lead = lead + 1

end for

هذا الكود بالسي

  1.  
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7. float **M;
  8. int rowCount,columnCount ,lead=0,i;
  9. cout<<"Enter rowCount=";cin>>rowCount;
  10. cout<<"Enter columnCount=";cin>>columnCount;
  11.  
  12. M=new float*[rowCount];
  13. for(int k=0;k<rowCount;k++) M[k]=new float[columnCount];
  14.  
  15. for(int a=0;a<rowCount;a++)
  16. {
  17. for(int b=0;b<columnCount;b++)
  18. cin>>M[a][b];
  19. }
  20. cout<<endl;
  21.  
  22. for(int a=0;a<rowCount;a++)
  23. {
  24. for(int b=0;b<columnCount;b++)
  25. cout<<M[a][b]<<" ";
  26. cout<<endl;
  27. }
  28.  
  29.  
  30. for(int r=0;r<rowCount;r++)
  31. {
  32. if(columnCount <= lead) break;
  33. i=r;
  34. while(M[i][lead]==0)
  35. {
  36. i++;
  37. if(rowCount==i)
  38. {
  39. i==r;
  40. lead++;
  41. if(rowCount==lead) break;
  42. }
  43.  
  44. }
  45. for(int o=0;o<columnCount;o++) swap(M[i][o],M[r][o]);
  46. for(int o=0;o<columnCount;o++) M[r][o]/=M[r][lead];
  47. for(i=0;i<rowCount;i++)
  48. {
  49. if(i!=r)
  50. {
  51. for (int j = 0; j < columnCount; j++)
  52. M[i][j] -= M[i][lead] * M[r][j];
  53. }
  54. }
  55. lead++;
  56. }
  57.  
  58. for(int a=0;a<rowCount;a++)
  59. {
  60. for(int b=0;b<columnCount;b++)
  61. cout<<M[a][b]<<" ";
  62. cout<<endl;
  63. }
  64. return 0;
  65. }
  66.  

tvquran_6.gif

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