Thread: Matrix Addition

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    20

    Exclamation Matrix Addition

    Write a void function called matrixAddition that has three two-dimensional array parameters. The function should compute the matrix sum of the first two parameters and store the result in the third parameter. The size of each of the arrays should be specified with the defined constants ROW and COLUMN.

    Matrix Addition

    To sum two matrices add the corresponding elements in each matrix.

    Example

    2 2 3 3 1 2 3 4 2+1 2+2 3+3 3+4 3 4 6 7 1 2 1 2 + 1 1 1 1 = 1+1 2+1 1+1 2+1 = 2 3 2 3
    1 2 3 4 2 3 4 5 1+2 2+3 3+4 4+5 3 5 7 9



    Code:
    const int MAX_WORD = 10;
    const int ROW = 3;
    const int COLUMN = 4;
    const int SS1 = 4;
    const int SS2 = 1;
    const int SORTED1 = 8;
    const int SORTED2 = 1;
    const int SORTED3 = 5;
     
    int main()
    {
     // Matrix Addition Test----------------------------
      int m1[ROW][COLUMN];
      int m2[ROW][COLUMN];
      int m3[ROW][COLUMN];
      printf("\nget matrix 1 input: enter 12 integers\n");
      for(int r = 0; r < ROW; ++r){
             for(int c = 0; c < COLUMN; ++c){
                     scanf("%d",&m1[r][c]);
             }
      }
      printf("\nget matrix 2 input: enter 12 integers\n");
      for(int r = 0; r < ROW; ++r){
             for(int c = 0; c < COLUMN; ++c){
                     scanf("%d",&m2[r][c]);
             }
      }
      printf("\nm1\n");
      matrixPrint(m1);
      printf("\nm2\n");
      matrixPrint(m2);
      printf("\nm1+m2=m3\n");
      matrixAddition(m1, m2, m3);
      matrixPrint(m3);
      return 0;
    }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    What is your question?
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    20
    How do you start it?

    Code:
    void matrixAddition(m1, m2, m3);
    

    ???

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by cmp View Post
    How do you start it?

    Code:
    void matrixAddition(m1, m2, m3);
    

    ???
    First work out how you are going to do it -> What do you have to add together? How is the matrix represented? What elements need to be added.

    Second -> What Algorithm do you want to use? Is there a for loop that you can use -> Have a look on Google - What algorithms are out there?

    Third -> The actual coding.


    Be careful that you don't sound too much like someone that wants other people to do their work for them: I'm sure that this is not the case, but it is starting to sound that way...
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I'd also recommend you work on one program at a time. Finish one before moving on to the second, then third, etc..

    Jim

  6. #6
    Registered User
    Join Date
    Mar 2014
    Location
    Zagreb, Croatia, Croatia
    Posts
    15
    If I understood it right all you need to do is add elements from m1 to m2 and save the result in m3 with the element locations corresponding the ones from m1 & m2.
    The way I'd solve this (if that's what should be done anyway) is to create a for loop within the function, that way you can do all the additions within a single call of the function but you need to turn in pointers as arguments
    e.g. void matrixAddition(int* m1, int* m2, int* m3); but if you want to use regular int values void matrixAddition(int m1, int m2, int m3); the only option you have is to "walk" through the matrix within main() and call the function
    for each individual element. I will write both versions and people can correct me if I'm wrong.
    ( Note: Please specify which type of function you need to make next time )
    Code:
    void matrixAddition(int* m1, int* m2, int* m3)
    {
    int i, j;
    for(i=0;i<ROW;i++)
     for(j=0;j<COLUMN;j++)
      m3[i*ROW+j]=m1[i*ROW+j]+m2[i*ROW+j];
    return;
    }
    Note that you are using pointers in this example so you need to turn over addresses as arguments e.g. calling a function looks like

    Code:
    matrixAddition(&m1[0][0], &m2[0][0], &m3[0][0]);
    /*..OR..*/
    matrixAddition(m1[0], m2[0], m3[0]);
    And if you use the other way your main() will have 2 for loops and you'll be calling a function within second loop:
    Code:
    for(i=0;i<ROW;i++)
     for(j=0;j<COLUMN;j++)
      m3[i][j]=matrixAddition(m1[i][j], m2[i][j]);
    and the function should look something like
    Code:
    int matrixAddition(int m1, int m2)
    {
    return m1+m2;
    }
    Note that the example with pointers is much more efficient in execution speed and memory usage.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Repeated printing of a specific elements-sparse matrix addition?
    By black_stallion in forum C Programming
    Replies: 0
    Last Post: 11-05-2011, 03:32 AM
  2. matrix addition program using arrays
    By suryak in forum C Programming
    Replies: 6
    Last Post: 04-10-2011, 06:37 AM
  3. Need help in Matrix Addition & finding Inverse of a Matrix
    By ssatyan.129 in forum C Programming
    Replies: 6
    Last Post: 05-15-2009, 02:48 PM
  4. howto build this addition matrix
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 12-24-2008, 12:19 AM
  5. help with matrix addition
    By rich999 in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2005, 02:30 PM