help with matrix addition

This is a discussion on help with matrix addition within the C++ Programming forums, part of the General Programming Boards category; This is the code i have so far. Code: #include <stdio.h> #include <stdlib.h> void enter_point(double point[3]); void translate(double point[2][3]); void ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    13

    help with matrix addition

    This is the code i have so far.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void enter_point(double point[3]);
    void translate(double point[2][3]);
    void scaling(double point[2][3]);
    
    int main()
    
    {
       
       double line[2][3];                     
     
       int menu_option;
    
       for (menu_option=0;;menu_option++)                   //For loop with no end conditions, so it will keep repeating
       {
          printf("\n\n\tPlease choose from the menu\n\n");                 
          printf("        *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n\n");
          printf("\t*\t1.\t\tChange position of P1 (Where P=0)\t*\n");
          printf("\t*\t2.\t\tChange position of P2 (Where P=1)\t*\n");
          printf("\t*\t3.\t\tScale the line\t\t\t\t*\n");                      
          printf("\t*\t4.\t\tRotate the line\t\t\t\t*\n");
          printf("\t*\t5.\t\tTranslate the line\t\t\t*\n");
          printf("\t*\t6.\t\tEvaluate a point on the line\t\t*\n");
          printf("\t*\t7.\t\tExit the program\t\t\t*\n\n");
          printf("        *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n\n\n");
          printf("\tEnter your choice here\t\t");
    
         
          scanf("\t%d", &menu_option);             
       
       
          if (menu_option==1) enter_point(line[0]);
          else if (menu_option==2) enter_point(line[1]);
          else if (menu_option==7) break;                   //Used to break the for loop and exit the program
          else if (menu_option<1) printf("\n\n\tNo\n\tBetween 1 and 7\n\tYou retard\n"); 
          else if (menu_option>7) printf("\n\n\tNo\n\tBetween 1 and 7\n\tYou retard\n");  
    
    }  
       
       
       
    }   
    
      
    
    void enter_point(double point[3])
    {  
       printf("\n\nPlease enter the co-ordinates of your point\n");
       printf("\nx co-ordinate=\t\t");
       scanf("%lf", &point[0]);
       printf("\ny co-ordinate=\t\t");
       scanf("%lf", &point[1]);
       printf("\nz co-ordinate=\t\t");
       scanf("%lf", &point[2]);
    }   
    
    
    void translate(double point[2][3])
    {
       double translation[1][3];
       
       printf("Please enter the amounts you wish to translate the line by, for each axis");
       printf("\nTranslation for x co-ordinate\t\t");
       scanf("%lf", &translation[0]);
       printf("\nTranslation for y co-ordinate\t\t");
       scanf("%lf", &translation[1]);
       printf("\nTranslation for z co-ordinate\t\t");
       scanf("%lf", &translation[2]);
    
    }
    I'm just a tad stumped about how to translate the matrix I have.
    Does anyone know how I would ceate a new [1][3] matrix in the above function which I could add to my existing matrix.

    Help much appreciated.
    Cheers

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I'm just a tad stumped about how to translate the matrix I have.
    What does translate mean? Explain exactly what you want to do to your array.

    Does anyone know how I would ceate a new [1][3] matrix
    A 1x3 array is an array with one row and 3 columns, which is the same thing as a single dimension array: int myArray[3].

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    I want to perform matix addition between a single dimensional array, and the matrix that has stored the data in the working functions i already have.

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    I want to perform matix addition between a single dimensional array, and the matrix that has stored the data in the working functions i already have.
    If by Matrix Addition you are referring to the strict mathematical definition then look here for ideas:-

    http://mathworld.wolfram.com/MatrixAddition.html

    Since matrix addition is intuitive it shouldn't be to hard to devise an algorithm for it. Matrix multiplication is a different matter...



    Lol this source is childish but explains clearly what you need to do in terms of translations/ enlargements etc.

    http://www.bbc.co.uk/schools/gcsebit...ionsrev1.shtml

    And of course without these simple simple starting points for translating points in 2D space we wouldn't be able to end up with models like this:
    Attached Images Attached Images  
    Last edited by treenef; 12-08-2005 at 03:59 AM.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    thanks for the help everyone.
    I understand the maths behind matrix addition and multiplication etc...

    But i's implementing it in c++ thats the problem.

    Here is my function:

    Code:
    void translate()
    {
       printf("\n\nPlease enter the amounts you wish to translate the line by, for each axis");
       printf("\n\nTranslation for x co-ordinate\t\t");
       scanf("%lf", &translation[0]);
       printf("\nTranslation for y co-ordinate\t\t");
       scanf("%lf", &translation[1]);
       printf("\nTranslation for z co-ordinate\t\t");
       scanf("%lf", &translation[2]);
       line[2][3]=translation[3]+line[2][3];
    }
    I have a 2 by 3 array, called line[2][3].
    And a one dimensional array, 3 units wide, called translation[3].
    The last line of the code is a poinless guess but i hope it demonstrates what I am trying to achieve.
    I want to input line[2][3], and change it so that the output is line[2][3] + translation[3],so that line[2][3] has been updated.

    Cheers for all the help.

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by rich999
    I want to input line[2][3], and change it so that the output is line[2][3] + translation[3],so that line[2][3] has been updated.
    That's what your code does. A shorthand would be
    Code:
    line[2][3] += translation[3];
    trouble is that the indexes are out of range . ( indexes start at 0 in C/C++ ).
    it should be
    Code:
    line[1][2] += translation[2];
    Kurt

  7. #7
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    int array_A[][]= {2,3};
    int array_b[][]= {2,3};
    
    New coord = array_A + array_b;
    That's what you should be doing. It won't compile but you should see what's going on.

    I believe this site explains the basics about scaling /rotating lines in the x,y,z axis.

    http://3dhtml.netzministerium.de/

    And of course our own:
    http://www.cprogramming.com/tutorial...nMatrices.html

    So basically, translating points in 3d shouldn't be a problem. It's when you come to scale and rotate them. oooh. That's gonna be a problem.
    Attached Images Attached Images  
    Last edited by treenef; 12-08-2005 at 06:57 AM.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I want to perform matix addition between a single dimensional array, and the matrix that has stored the data in the working functions i already have.
    Yeah, yeah...but what does all that mean specifically. Explain exactly what that means. If you don't know what that means, then you can't program it.

  9. #9
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    I think what he means is this:

    He is given the start and end points of a line in three dimensional space:

    Start = (2,3,0) End = (9,4,2)

    I'm assuming this would be P1 and P2 in his previous program

    Now he has to translate that line using the translation matrix:

    (x,y,z)

    So the start point and end point have moved coordinates.

    Next his program has to handle a scaling of that line and a rotation of that line.

    However, from the links given above, you will realise that scaling and rotating lines in three dimensional space is far from trivial.

    Unless of course, he intends to scale and rotates the line in two-dimensional space. Which of course would be a darn sight easier.

    If it's the former then its gonna be a pretty tough assignment!

    You will probably be needing to multiply matrices... Here's some sample code of how it might be done.

    Code:
    /*================================================
     A program which performs matrix 
     multiplication.
     
     Notes
     -----
     Multiplication can only take place iff
     the number of columns in matrix A is equal 
     to the number of rows in matrix B.
     
     Or as I like to remember it:
         
           C A R B
           
     Columns in
     A must equal
     Rows in
     B
     
     ================================================*/
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    //Function declarations
    void mmult(int,int,int[][100],int[][100]);
    
    int main()
    {
        //Hard code matrix A
        int m1[100][100] = {{1,5},
                            {2,7},
                            {3,4}};
        
        //Hard code matrix B
        int m2[100][100] = {{8,4,3,1},
                            {2,5,8,6}};
        //call function
        mmult(3,4,m1,m2);
        
        //Where '3' is the maximum number of columns in either
        //matrix A or B
        //and '4' is the maximun number of rows in either
        //matrix A or B
        
        printf("\nPress any key to quit");
        int quit;
         
        scanf("%d", &quit);             
         
       return 0;
    }    
    /*======================================================
      Function to multiply matrix
     =====================================================*/                  
    void mmult (int rows, int cols, int m1[][100], int m2[][100]) 
    {
        int m3[100][100];//holds the result of A * B
        for (int i=0; i<rows; i++) 
        {
            for (int j=0; j<cols; j++) 
            {
              int val = 0;
              for (int k=0; k<cols; k++) 
              {
                val = val + m1[i][k] * m2[k][j];
              }
              m3[i][j] = val;
            }
        }
        //print matrix
        for (int i=0; i<rows; i++) 
        {
            for (int j=0; j<cols; j++) 
            {
              printf("%d ",m3[i][j]);
    
              
            }
            printf("\n");
        }
    }
    Last edited by treenef; 12-09-2005 at 06:57 AM.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    Thats the assignment treenef yeah.
    I do find it pretty hard.
    I have done the addition now though amazingly.

    Stuck on scaling the line by multipying the matrix.

    It has to be done in a [2][3] matrix for extra marks
    Will get penalised fo using two 1D arrays.
    Sux.

    Will post finished article on this thread next week if you wana look.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. 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
  3. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21