PDA

View Full Version : سوال: مرتب سازی المان های ماتریس



Faraz Mehraien
شنبه 21 دی 1392, 17:47 عصر
سلام.من یه کد زدم که المان های ماتریس رو در هر سطر صعودی مرتب کنه اما کار نمی کنه.اشکالش از کجاس؟؟از کامپایلر dev استفاده می کنم اینم کدم

#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
int main()
{
int i,j,p,q,c1,c2,r1,r2,k,temp=1;
char ans1,ans2,answer;
int matrix1[100][100];
int matrix2[100][100];
int sum[100][100];
int multi[100][100];
int min[100][100];
char func[80];
cout<<"enter first matrix\n";
cout<<"enter numbers of rows\n";
cin>>r1;
cout<<"enter numbers of column\n";
cin>>c1;

// getting first matrix
for(i=0;i<r1 ; i++)
{
for(j=0;j<c1 ;j++)
{
cout<<"enter eleman ";
cin>>matrix1[i][j];
}
}
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~\n";

// getting second matrix
cout<<"enter second matrix\n";
cout<<"enter numbers of rows\n";
cin>>r2;
cout<<"enter numbers of column\n";
cin>>c2;

for(p=0;p<r2 ; p++)
{
for(q=0;q<c2 ;q++)
{
cout<<"enter eleman ";
cin>>matrix2[p][q];
}
}
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
for(i=c1,p=0;i>0,p<r1;i--,p++)
{
for(j=0,q=0;j<c1,q<r1;j++,q++)
if(matrix1[i][j]>matrix1[i][j+1])
{
k=matrix1[i][j];
matrix1[i][j]=matrix1[i][j+1];
matrix1[i][j+1]=k;
}
}
cout<<"\nsorted matrix1 = ";
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
{
cout<<matrix1[i][j]<<" ";
if(j==r1-1)
cout<<"\n\n";
for(i=c2,p=0;i>0,p<r2;i--,p++)
{
for(j=0,q=0;j<c2,q<r2;j++,q++)
if(matrix2[i][j]>matrix2[i][j+1])
{
k=matrix2[i][j];
matrix2[i][j]=matrix2[i][j+1];
matrix2[i][j+1]=k;
}
}
cout<<"\nsorted matrix2 = ";
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
{
cout<<matrix2[i][j]<<" ";
if(j==r2-1)
cout<<"\n\n";

}}
getch ();
return 0;
}