پنجشنبه, ۱۱ دی ۱۳۹۳، ۰۲:۵۴ ب.ظ
ضرب 2 ماتریس
برنامه ای بنویسید که در آن ابعاد 2 ماتریس دلخواه را گرفته ابتدا بررسی کند که حاصلضرب آنها ممکن است یا نه!
سپس مقادیر هر یک را از وردی گرفته و در نهایت ماتریس حاصلضرب را چاپ کند.
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
int i,j,k;
int m1,n1,m2,n2;
cout<<"andaze matrix_a: "<<endl<<endl;
cout<<"andaze satr >>> ";
cin>>m1;
cout<<"andaze soton >>> ";
cin>>n1;
cout<<"..........................."<<endl;
cout<<"andaze matrix_b: "<<endl<<endl;
cout<<"andaze satr >>> ";
cin>>m2;
cout<<"andaze soton >>> ";
cin>>n2;
cout<<endl;
if(n1==m2)
{
cout<<"the multiple is ok"<<endl<<endl;
double matrix_a[m1][n1];
double matrix_b[m2][n2];
double matrix_mul[m1][n2];
cout<<endl<<"maghadir matris a >>> "<<endl;
for(i=0; i<m1; i++)
{
cout<<"satr "<<i<<" : ";
for(j=0; j<n1; j++)
{
cin>>matrix_a[i][j];
}
}
cout<<endl<<"maghadir matris b >>> "<<endl;
for(i=0; i<m2; i++)
{
cout<<"satr "<<i<<" : ";
for(j=0; j<n2; j++)
{
cin>>matrix_b[i][j];
}
}
//zero matrix_mul
for(i=0; i<m1; i++)
{
for(j=0; j<n2; j++)
matrix_mul[i][j]=0;
}
//DO MULTIPLE
for(i=0; i<m1; i++)
{
for(j=0; j<n1; j++)
{
for(k=0; k<n2; k++)
{
matrix_mul[i][k]+=matrix_a[i][j]* matrix_b[j][k];
}
}
}
cout<<endl<<"-------- namayesh matris a,b --------"<<endl<<endl;
for(i=0; i<m1; i++)
{
for(j=0; j<n1; j++)
{
cout<<matrix_a[i][j]<<"\t";
}
cout<<endl;
}
cout<<endl<<endl;
for(i=0; i<m2; i++)
{
for(j=0; j<n2; j++)
{
cout<<matrix_b[i][j]<<"\t";
}
cout<<endl;
}
cout<<endl<<"-------- namayesh matris hasel zarb--------"<<endl<<endl;
for(i=0; i<m1; i++)
{
for(j=0; j<n2; j++)
{
cout<<matrix_mul[i][j]<<"\t";
}
cout<<endl;
}
}
else
cout<<"the multiple is NOT ok!!!"<<endl<<endl;
getch();
}
#include <iostream>
#include <conio.h>
using namespace std;
int amel(int s)
{int k=1,n=0;
for(int i=1;i<=s;i++)
{for(int j=1;j<=i;j++)
{if(i%j==0)
n++;}
if(n==2)
k=i*k;}
return k;
}
main()
{int b;
cin>>b;
cout<<amel(b);
cout << "Hello world!" << endl;
getch();
}