Matrix Multiplication

This is a discussion on Matrix Multiplication within the C++ Programming forums, part of the General Programming Boards category; Hi, I am new to c++. Below are 2 functions that I have created. But they do not work, I ...

  1. #1
    dlf
    dlf is offline
    Registered User
    Join Date
    Nov 2005
    Posts
    17

    Matrix Multiplication

    Hi,

    I am new to c++.

    Below are 2 functions that I have created.

    But they do not work, I was just hoping someone could point out the error of my ways.

    I am tryin to scale the coordinates of a line.

    Thanking you in advance.

    Code:
    void scaling (double line[][3])
    {
       //double a=0;
       double s;
       double new_line2[2][3];
       
       double scaling_matrix[3][3];
       scaling_matrix[0][0]=s;
       scaling_matrix[1][0]=0;
       scaling_matrix[2][0]=0;
       scaling_matrix[0][1]=0;
       scaling_matrix[1][1]=s;
       scaling_matrix[2][1]=0;
       scaling_matrix[0][2]=0;
       scaling_matrix[1][2]=0;
       scaling_matrix[2][2]=s;
       
       
       
       printf("\nHow much would you like to scale the line by?\nEnter a value between 0 and 1:");
       scanf("%lf", &s);
       
       scaling_multiplication(line[2][3], scaling_matrix[3][3], new_line2[2][3]);
       //scaling_multiplication(line[1], scaling_matrix, new_line2[1]); 
       
       printf("\nThe new start coordinates are: (%lf,%lf,%lf)", new_line2[0][0], new_line2[0][1], new_line2[0][2]);
       printf("\nThe new end coordinates are: (%lf,%lf,%lf)", new_line2[1][0], new_line2[1][1], new_line2[1][2]);
    }  
    
    
    void scaling_multiplication (double D[2][3], double E[3][3], double F[2][3])
    {
       F=D[2][3]*E[3][3];
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    double s;
    scaling_matrix[0][0]=s;
    You aren't assigning any value to s, so it will contain a random junk value.
    ---

    Code:
    scanf("%lf", &s);
    Now you read a value into s, but it's too late.
    ---

    When you use this function:
    Code:
    scaling_multiplication(line[2][3], scaling_matrix[3][3], new_line2[2][3]);
    The function has to be declared before then.
    ----

    This:
    Code:
    void scaling_multiplication (double D[2][3], double E[3][3], double F[2][3])
    declares three variables named D, E, and F that are arrays of the indicated type and size. Any arguments you send to the function, will be stored in the variables D, E, and F--as long as the arguments are the same type as D, E, and F.

    However, this:
    Code:
    F=D[2][3]*E[3][3];
    says to get the value in D at position row 2, column 3 and multiply it by the value in E at position row 3, column 3, and assign it to the array F. You can't assign a double to an array name like F. You can assign a double to a position in F, like this:
    Code:
    F[0][0] = D[2][3]*E[3][3];
    In C++, you can't do this:

    F = D * E;

    and expect that every position in F will be filled with the product of the values in D and E. Instead, you have to go through every position in F and assign it the product of an element in D and an element in E. You can use for-loops to help you do that.
    Last edited by 7stud; 12-04-2005 at 08:48 AM.

  3. #3
    dlf
    dlf is offline
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    Thanks for your help.

    I took on board what you said and changed my code.

    But I still get errors which I don't understand.

    (172) : error 139 - Argument no 1 of 'scaling_multiplication' must be of type '<ptr>double', not 'double'

    and

    (181) : error 192 - An object of type 'double' cannot be assigned to an object of type '<ptr>(double[3])'

    Code:
    void scaling (double line[][3])
    {
       //double a=0;
       double s;
       double new_line2[2][3];
       
       printf("\nHow much would you like to scale the line by?\nEnter a value between 0 and 1:");
       scanf("%lf", &s);
    
       double scaling_matrix[3][3];
       scaling_matrix[0][0]=s;
       scaling_matrix[1][0]=0;
       scaling_matrix[2][0]=0;
       scaling_matrix[0][1]=0;
       scaling_matrix[1][1]=s;
       scaling_matrix[2][1]=0;
       scaling_matrix[0][2]=0;
       scaling_matrix[1][2]=0;
       scaling_matrix[2][2]=s;
       
       scaling_multiplication(line[2][3], scaling_matrix[3][3], new_line2[2][3]);
       
       printf("\nThe new start coordinates are: (%lf,%lf,%lf)", new_line2[0][0], new_line2[0][1], new_line2[0][2]);
       printf("\nThe new end coordinates are: (%lf,%lf,%lf)", new_line2[1][0], new_line2[1][1], new_line2[1][2]);
    }  
    
    
    void scaling_multiplication (double D[2][3], double E[3][3], double F[2][3])
    {
       F=D[2][3]*E[3][3];
    }

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Reread my post again, I added some things.

    Also, you can initialize an array when you create it. To do that you use an initializer list:
    Code:
    double arr[10] = {1, 2, 3};
    If you use an initializer list, and you don't specify all the values for the array, then the rest of the array will be initialized with 0. So, using that fact you can easily initialize a whole array to 0 like this:

    double arr[10] = {0};

    That initializes the first element with 0, and since no values are specified for the rest of the array, the rest of the elements are initialized with 0. It's good practice to always initialize your variables, so whenever you declare a variable or an array, you should initialize it with some value.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    (172) : error 139 - Argument no 1 of 'scaling_multiplication' must be of type '<ptr>double', not 'double'
    When you send an array to a function, you use just the array name. When you do this:

    line[2][3]

    You are sending just the element at position row2, column 3, which is of type double. However, the function is expecting a whole array. A whole array has a different type than a single element of the array.

  6. #6
    dlf
    dlf is offline
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    I have edited the code, but I am slightly confused because the size of the scaling_matrix is different to the size of the line and new line matrix.

    Code:
    void scaling (double line[][3])
    {
       //double a=0;
       double s;
       double new_line2[2][3];
       
       printf("\nHow much would you like to scale the line by?\nEnter a value between 0 and 1:");
       scanf("%lf", &s);
    
       double scaling_matrix[3][3];
       scaling_matrix[0][0]=s;
       scaling_matrix[1][0]=0;
       scaling_matrix[2][0]=0;
       scaling_matrix[0][1]=0;
       scaling_matrix[1][1]=s;
       scaling_matrix[2][1]=0;
       scaling_matrix[0][2]=0;
       scaling_matrix[1][2]=0;
       scaling_matrix[2][2]=s;
       
       scaling_multiplication(line[2][3], scaling_matrix[3][3], new_line2[2][3]);
       
       printf("\nThe new start coordinates are: (%lf,%lf,%lf)", new_line2[0][0], new_line2[0][1], new_line2[0][2]);
       printf("\nThe new end coordinates are: (%lf,%lf,%lf)", new_line2[1][0], new_line2[1][1], new_line2[1][2]);
    }  
    
    
    void scaling_multiplication (double D[2][3], double E[3][3], double F[2][3])
    {
       F[0][0]=D[0][0]*E[0][0];
       F[0][1]=D[1][0]*E[0][1];
       F[0][2]=D[2][0]*E[0][2];
       F[1][0]=D[0][1]*E[1][0];
       F[1][1]=D[1][1]*E[1][1];
       F[1][2]=D[2][3]*E[1][2];
    
    }

  7. #7
    dlf
    dlf is offline
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    I'm sorry to be a pain and i am very grateful for your help, but I don't understand your last post, my knowledge is very basic. Sorry.

  8. #8
    dlf
    dlf is offline
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    I'm sorry to be a pain and i am very grateful for your help, but I don't understand your last post, my knowledge is very basic. Sorry.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    but I am slightly confused because the size of the scaling_matrix is different to the size of the line and new line matrix.
    Why did you make it a different size?
    Code:
    double scaling_matrix[3][3];
    It sounds like you didn't even write the code you have so far.

  10. #10
    dlf
    dlf is offline
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    Honestly, I did write the code I have so far. I am just not very confident. I read in some notes I have that when scaling you have to multiply the points matrix by the scaling matrix. It then goe son to say that the scaling matrix is: [s]=[s 0 0, 0 s 0, 0 0 s]. So thats why I made the scaling_matrix [3][3].

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The bottom line is it doesn't really matter what size the scaling matrix is--as long as it is big enough. Every position in the matrix for the original line has a corresponding position in the scaling matrix, right? As long as the scaling matrix has more rows and more columns than the original line, then there will be an element in that array that will correspond to the same position in the original line's array. For instance, can you name any position: row and column in the original line that is out of bounds of the scaling array?

    Code:
    F[0][0]=D[0][0]*E[0][0];
       F[0][1]=D[1][0]*E[0][1];
       F[0][2]=D[2][0]*E[0][2];
       F[1][0]=D[0][1]*E[1][0];
       F[1][1]=D[1][1]*E[1][1];
       F[1][2]=D[2][3]*E[1][2];
    That should be done with for loops.
    Last edited by 7stud; 12-04-2005 at 09:55 AM.

  12. #12
    dlf
    dlf is offline
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    For instance, can you name any position: row and column in the original line that is out of bounds of the scaling array?

    How do you mean out of bounds? My line only has 2 points a strat and an end. They should both be able to be scaled.

    Code:
    F[0][0]=D[0][0]*E[0][0];
       F[0][1]=D[1][0]*E[0][1];
       F[0][2]=D[2][0]*E[0][2];
       F[1][0]=D[0][1]*E[1][0];
       F[1][1]=D[1][1]*E[1][1];
       F[1][2]=D[2][3]*E[1][2];
    You said this should be done with for loops. How is this possible as every point is different? I have only dealt with very basic for lopps i.e for (i=0; i<10; i++)

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    How do you mean out of bounds?
    If you have an array declared like this:
    Code:
    double myArray[2][3] = {0};
    then myArray has 2 rows with 3 columns per row. If you have another array declared like this:
    Code:
    double scaling[50][100] = {0};
    then any valid index position of an element in myArray is also a valid index position of an element in scaling. For instance, position 0, 2 is a valid index position in myArray, and it is also valid in scaling.

    Is the opposite true? Is a valid index position in scaling also valid in myArray? What about index position 49, 75 in scaling? Is that a valid index position in myArray? Or, is it out of bounds?
    Last edited by 7stud; 12-04-2005 at 04:19 PM.

  14. #14
    dlf
    dlf is offline
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    I'm pretty sure that is a valid position in myArray.

    Is there any more advice you could give me because I just can't get my code to work, it won't even compile.

    Thank you for your help.

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    What about index position 49, 75 in scaling? Is that a valid index position in myArray? Or, is it out of bounds?

    I'm pretty sure that is a valid position in myArray.
    So myArray has an element in row 49, column 75? How is that possible when myArray only has 2 rows and 3 columns?
    Code:
    double myArray[2][3] = {0};
    Unfortunately, this program is beyond your abilities at this time. You need to write at least 10 practice programs using some single dimensional arrays and a for loop to assign each element in the array a value, and then use another for loop to display each value in the array. You also need to become proficient at adding one array to another using a for loop--even when the sizes of the arrays are different. After you've written 10 practice programs doing that with arrays of different sizes and different types, e.g. int, double, and string, then you need to write 10 practice programs using 2 dimensional arrays.

    For two dimensional arrays, you need to use 2 nested for loops to cycle through all the index positions in the array. You also need to get a beginning C++ book like "C++: A Beginner's Guide" and read it and do the exercises.

    Good luck.
    Last edited by 7stud; 12-06-2005 at 10:55 AM.

Page 1 of 2 12 LastLast
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. *operator overloading: scalar matrix multiplication
    By gemini_shooter in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 01:14 PM
  3. Matrix Multiplication ( Accessing data from a file ) HELP
    By six_degreez in forum C Programming
    Replies: 2
    Last Post: 07-24-2008, 05:21 PM
  4. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM

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