# Matrix Multiplication

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 12-04-2005
dlf
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.

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]; }```
• 12-04-2005
7stud
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.
• 12-04-2005
dlf

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]; }```
• 12-04-2005
7stud

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.
• 12-04-2005
7stud
Quote:

(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.
• 12-04-2005
dlf
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]; }```
• 12-04-2005
dlf
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.
• 12-04-2005
dlf
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.
• 12-04-2005
7stud
Quote:

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.
• 12-04-2005
dlf
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].
• 12-04-2005
7stud
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.
• 12-04-2005
dlf
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++)
• 12-04-2005
7stud
Quote:

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?
• 12-06-2005
dlf
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.

• 12-06-2005
7stud
Quote:

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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last