beroozresani
New Member
سلام. این برنامه جمع دو ماتریس اسپارسه. درست هم هست. ولی کسی میتونه قسمت تابع سام رو واسم تغییر بده؟
ممنون
ممنون
#include <iostream>
#include <conio.h>
using namespace std;
void sum(int S1[][3] , int S2[][3] , int C[][3])
{
int k=1,found;
if (S1[0][0]!=S2[0][0] && S1[0][1]!=S2[0][1])
{
cout <<"Grade of these matrix dos'nt match!\n";
exit(1);
}
C[0][0]=S1[0][0];
C[0][1]=S2[0][1];
C[0][2]=0;
for (int i=1 ; i<=S1[0][2] ; i++)
for (int j=1 ; j<=S2[0][2] ; j++)
if (S1[0]==S2[j][0] && S1[1]==S2[j][1])
{
C[k][0]=S1[0];
C[k][1]=S1[1];
C[k][2]=S1[2]+S2[j][2];
k++;
break;
}
else
{
found=0;
for (int y=k-1 ; y>=1 ; y--)
if (C[y][0]==S1[0] && C[y][1]==S1[1])
found=1;
if (!found)
{
C[k][0]=S1[0];
C[k][1]=S1[1];
C[k][2]=S1[2];
k++;
}
found=0;
for (int y=k-1 ; y>=1 ; y--)
if (C[y][0]==S2[j][0] && C[y][1]==S2[j][1])
found=1;
if (!found)
{
C[k][0]=S2[j][0];
C[k][1]=S2[j][1];
C[k][2]=S2[j][2];
k++;
}
}
for (int i=1 ; i<6 ; i++)
if (C[2])
C[0][2]++;
}
int main()
{
int A[4][3]={{3,4,3},{1,2,5},{2,1,6},{0,3,1}},B[3][3]={{3,4,2},{1,2,4},{0,2,3}},C[6][3]={0};
sum(A , B , C);
for (int i=0 ; i<6 ; i++)
for (int j=0 ; j<3 ; j++)
cout <<C[j]<<endl;
return 0;
}
#include <conio.h>
using namespace std;
void sum(int S1[][3] , int S2[][3] , int C[][3])
{
int k=1,found;
if (S1[0][0]!=S2[0][0] && S1[0][1]!=S2[0][1])
{
cout <<"Grade of these matrix dos'nt match!\n";
exit(1);
}
C[0][0]=S1[0][0];
C[0][1]=S2[0][1];
C[0][2]=0;
for (int i=1 ; i<=S1[0][2] ; i++)
for (int j=1 ; j<=S2[0][2] ; j++)
if (S1[0]==S2[j][0] && S1[1]==S2[j][1])
{
C[k][0]=S1[0];
C[k][1]=S1[1];
C[k][2]=S1[2]+S2[j][2];
k++;
break;
}
else
{
found=0;
for (int y=k-1 ; y>=1 ; y--)
if (C[y][0]==S1[0] && C[y][1]==S1[1])
found=1;
if (!found)
{
C[k][0]=S1[0];
C[k][1]=S1[1];
C[k][2]=S1[2];
k++;
}
found=0;
for (int y=k-1 ; y>=1 ; y--)
if (C[y][0]==S2[j][0] && C[y][1]==S2[j][1])
found=1;
if (!found)
{
C[k][0]=S2[j][0];
C[k][1]=S2[j][1];
C[k][2]=S2[j][2];
k++;
}
}
for (int i=1 ; i<6 ; i++)
if (C[2])
C[0][2]++;
}
int main()
{
int A[4][3]={{3,4,3},{1,2,5},{2,1,6},{0,3,1}},B[3][3]={{3,4,2},{1,2,4},{0,2,3}},C[6][3]={0};
sum(A , B , C);
for (int i=0 ; i<6 ; i++)
for (int j=0 ; j<3 ; j++)
cout <<C[j]<<endl;
return 0;
}