problem in multiplying arrays


 
Thread Tools Search this Thread
Top Forums Programming problem in multiplying arrays
# 1  
Old 12-05-2011
problem in multiplying arrays

Hi, this is my code.It's simple : there are 2 2D arrays and the multiplied to C.
Code:
#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:
Code:
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??

Last edited by giampoul; 12-05-2011 at 03:48 PM..
# 2  
Old 12-05-2011
Indenting your program so I can read it.
Code:
#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);
}

---------- Post updated at 01:42 PM ---------- Previous update was at 01:38 PM ----------

Code:
        A[0]=0xdeadbeef;
        printf("%x %x %x\n", A[0], B[0], C[0]);

Code:
deadbeef deadbeef deadbeef

A, B, and C are all pointers to the same memory. You're getting the exact same segment ID three times in a row, which gives you the exact same segment three times in a row.

Since you're getting 4096 bytes of memory anyway, plenty of room for all those small arrays in one call, why not just do this:

Code:
A=(int *)shmat(id,NULL, 0);
B=(A + (3*4));
C=(B + (3*4));

Then you get correct results for A and B, but not C. I'm not quite sure what you're doing with C.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 12-05-2011
Sorry about the lining...i fix it.

Your code is helpful. Now i get the right A and B arrays !
But I get for C that :
Code:
C:
40	34	28	53	
112	97	82	726

but it should be:
Code:
C:
40	34	28	22	
112	97	82	67

Maybe a wrong at the multiply??
I thaught that this:
Code:
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;
                }
        }

is the correct multiply code for 2 multidimensional 2Ds arrays.
k=3 is the A columns = B rows.
Is there any other way ??

Last edited by giampoul; 12-05-2011 at 04:04 PM..
# 4  
Old 12-05-2011
Been a while since I did matrix multiplication, going through the math.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 12-05-2011
Quote:
Originally Posted by Corona688
Been a while since I did matrix multiplication, going through the math.
Of course...
i based here http://en.wikipedia.org/wiki/Matrix_multiplication for the multiplication
and here http://sourcecookbook.com/en/recipes...emory-with-ipc for the arrays
# 6  
Old 12-05-2011
You have problems initializing and printing your A array, multiplying by j instead of i so you try to assign 3 rows of 2 instead of 2 rows of 3... Fixing that makes A print rotated 90 degrees to B... Working on it...

---------- Post updated at 02:51 PM ---------- Previous update was at 02:46 PM ----------

You've got your rows and columns mixed up in general I think. A[(row*3) + column] not vice versa.

---------- Post updated at 03:06 PM ---------- Previous update was at 02:51 PM ----------

I found myself making the same mistake as you over and over. When that keeps happening that's a good sign it's time to let the computer start remembering some things instead of the programmer:

Code:
#define OFFSET(COLUMNS, ROW, COL)       ((ROW*COLUMNS)+COL)

...

       A = (int *)shmat(id,NULL, 0);
       B = A + OFFSET(3, 2, 0);
       C = B + OFFSET(3, 4, 0);

...

        x=1;
        for(i=0; i<2; i++)
        for(j=0; j<3; j++)
                A[OFFSET(3, i, j)]=x++;


...

        for(i=0; i<2; i++)
        for(j=0; j<4; j++)
        {
                int d=0, k;
                for(k=0; k<3; k++)
                        d += A[OFFSET(3,i,k)] * B[OFFSET(4,k,j)];

                C[OFFSET(4, j, i)]=d;
        }


...

This User Gave Thanks to Corona688 For This Post:
# 7  
Old 12-05-2011
Code:
#define OFFSET(COLUMNS, ROW, COL)       ((ROW*COLUMNS)+COL)

#include<stdio.h>
#include<sys/shm.h>
#include<sys/stat.h>
#include<stdlib.h>
main()
{
  int *A,*B,*C;
  int i,j,x,d,k;
  int id;
  id = shmget(IPC_PRIVATE,4096, IPC_CREAT|0666);
  A = (int *)shmat(id,NULL, 0);
  B = A + OFFSET(3, 2, 0);
  C = B + OFFSET(3, 4, 0);

  x=1;
  for(i=0; i<2; i++)
  {
    for(j=0; j<3; j++)
    {
      A[OFFSET(3, i, j)]=x++;
    }
  }
		
  x=12;
  for(i=0; i<3; i++)
  {
    for(j=0; j<4; j++)
    {
      B[OFFSET(4, i, j)]=x--;
    }
  }

   for(i=0; i<2; i++)
   {
     for(j=0; j<4; j++)
     {
                int d=0, k;
                for(k=0; k<3; k++)
                d += A[OFFSET(3,i,k)] * B[OFFSET(4,k,j)];
                C[OFFSET(4, j, i)]=d;
     }
   }
        
   printf("C:\n");
   for(i=0;i<2;i++)
   {
     for(j=0;j<4;j++)
     {
       printf("%d\t",C[j*4+i]);       
     }
     printf("\n");
   }
}

Yes! That do the job !
If you have the time can you explain me a little these lines:
Code:
B = A + OFFSET(3, 2, 0);
C = B + OFFSET(3, 4, 0)

and what does the OFFSET do ?
Also why its wrong with the 3 shmat() functions? In another program i made where A and B were read by a .txt file the arrays are correct.
I am at beginner level and i am trying a project... Of course this is not all of it. I just split it to pieces so i can do it easier.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Multiplying 2D arrays using fork()

HI, i am trying to multiply 2 2D arrays (a,b) using fork. The answer will be at c. Each child have to calculate 1 row of c. The code is right, as i think of it, with no errors but i dont get the correct c array... I think there is maybe a mistake in i dimension ... Anyway, here is the code: ... (16 Replies)
Discussion started by: giampoul
16 Replies

2. Shell Programming and Scripting

Multiplying array element

I am trying to take all the elements of an array and multiply them by 2, and then copy them to a new array. Here is what I have i=0 for true in DMGLIST do let DMGSIZES2="${DMGSIZES}"*2 let i++ done unset i echo ${DMGSIZES2} It does the calculation correctly for the first element,... (7 Replies)
Discussion started by: nextyoyoma
7 Replies

3. Shell Programming and Scripting

Problem with arrays and loop

Hello , im sorry for my english . im trying to create a dynamic menu that will display if the interface is ACTIVE OR STOPPED/FAILED for some reason i cant get it to work properly start_interface_func() { i=0 for interface_chk in 11 71 73 72 12 47 48 49 50 20 23 24 25 46 21 22 27 28... (5 Replies)
Discussion started by: visiown
5 Replies

4. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

5. Shell Programming and Scripting

Problem with arrays

Hi I have two arrays: arr1 = (demo demo2 demo3 demo4 demo5) arr2 = (demo2 test demo) I want to check that the values the "arr2" are present in "arr1" Example arr1 = (demo demo2 demo3 demo4 demo5) arr2 = (demo2 test demo) Output: Error arr1 = (demo demo2 demo3 demo4 demo5)... (3 Replies)
Discussion started by: blito_loco
3 Replies

6. UNIX for Dummies Questions & Answers

Problem assigning variables to arrays

Hi All, I have a problem assigning variables to script.I have a script in which i have a while loop now i have to assign some values obtained to an array which will be used later in the script.Can anyone help how to do that. At present my scrot looks like: co=0 pco=0 co=`cat /tmp/highcpu... (4 Replies)
Discussion started by: usha rao
4 Replies

7. Programming

C programming + problem with char arrays

Im trying to write some code atm which gets the complete pathname of a folder and strips off references to the parent folders. The end result should be just the name of the folder. Currently Im able to extract the folder name, however Im getting junk added onto the name as well which is making... (7 Replies)
Discussion started by: JamesGoh
7 Replies

8. Shell Programming and Scripting

Problem with arrays in awk

Hello! I'm trying to make a script that will make a list of the files in a source tree and sort them by size. Problem is I've run into a weird problem. print array will give me numbers like 160, 220, 444 that i don't even know where they come from, and print array will give me the correct numbers... (5 Replies)
Discussion started by: Glauco
5 Replies

9. Shell Programming and Scripting

Multiplying Floats/Decimals

Is there a way that i can get something like this to work: Number=`expr 80 \* 10.69` i.e. To multiply an integer by a decimal or a decimal by a decimal etc...? thanks (10 Replies)
Discussion started by: rleebife
10 Replies

10. Shell Programming and Scripting

Error only when multiplying two numbers

Hi $ a=10 ; b=2 $ expr $a + $b 12 $ expr $a - $b 8 $ expr $a / $b 5 $ expr $a * $b expr: syntax error Any idean why I am getting this error only when multiplying two numbers. Whats the exact syntax? Thanks a lot to all in advance CSaha (5 Replies)
Discussion started by: csaha
5 Replies
Login or Register to Ask a Question