Hi, this is my code.It's simple : there are 2 2D arrays and the multiplied to C.
#include<stdio.h>
#include<sys/shm.h>
#include<sys/stat.h>
#include<stdlib.h>
main()
{
int *A; //A[2][3]
int *B; //B[3][4]
int *C; //C[2][4]
int i,j,x,k,d;
int id;
id = shmget(IPC_PRIVATE,4096, IPC_CREAT|0666);
A = (int *)shmat(id,NULL, 0);
x=1;
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
A[j*3+i]=x;
x++;
}
}
B = (int *)shmat(id,NULL, 0);
x=12;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
B[j*4+i]=x;
x--;
}
}
printf("A:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",A[j*3+i]);
}
printf("\n");
}
printf("B:\n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%d\t",B[j*4+i]);
}
printf("\n");
}
//Multiply to C[2][4], k common A,B
C = (int *)shmat(id,NULL, 0);
for(i=0;i<2;i++)
{
for(j=0;j<4;j++)
{
for(k=0;k<3;k++)
{
d=d+A[k*3+i]*B[j*4+k];
}
C[j*4+i]=d;
d=0;
}
}
printf("C:\n");
for(i=0;i<2;i++)
{
for(j=0;j<4;j++)
{
printf("%d\t",C[j*4+i]);
}
printf("\n");
}
shmdt(A);
shmdt(B);
shmdt(C);
shmctl(id,IPC_RMID,NULL);
}
A should be:
1 2 3
4 5 6
and B:
12 11 10 9
8 7 6 5
4 3 2 1
but what i take is that:
A:
12 2 3
8 11 6
B:
12 11 10 9
8 7 6 5
4 3 2 1
C:
172 1915 1738 1561
16720 32032223 29070862 26109501
C is also wrong...
Any ideas??