biloonline
asked on
Array Multiplication Function.. Please Help
Can someone please help me write a function that multiplies two matrices (arrays) whose size can be passed to the function? I don't know how to get started. Thank you.
ASKER
Well, this is pretty much all I could do on my own, which is why I asked for help:
void Product (int n, const float A[] [capacity], const float x[] [capacity], float B[] [capacity]) {
int i, j, k;
for(i=0; i<n; i++)
for(j=0; j<n; j++)
I know it's not much, which is why I didn't include it initially. If you still feel you wouldn't be able to help, I understand. Thanks anyway.
void Product (int n, const float A[] [capacity], const float x[] [capacity], float B[] [capacity]) {
int i, j, k;
for(i=0; i<n; i++)
for(j=0; j<n; j++)
I know it's not much, which is why I didn't include it initially. If you still feel you wouldn't be able to help, I understand. Thanks anyway.
ASKER
Just for reference, and the possibility that it might be useful in helping me out, this is the program I've written so far:
#include <stdio.h>
const capacity = 101;
void Input (int* nPtr, float A[] [capacity]);
void Solution (int n, const float A[] [capacity], const float B[] [capacity], float x[] [capacity]);
void Product (int n, const float A[] [capacity], const float x[] [capacity], float B[] [capacity]);
void Output (int n, const float A[] [capacity]);
char peek();
int main(void)
{
return 0;
}
void Input (int* nPtr, float A[] [capacity]) {
int i, j;
for(*nPtr=0; peek() != '\n'; (*nPtr)++)
scanf("%f", &A[0][*nPtr]);
for(i=1; i<*nPtr; i++) {
for(j=0; j<*nPtr; j++)
scanf("%f", &A[i][j]); }
get char(); }
void Solution (int n, const float A[] [capacity], const float B[]
[capacity], float x[] [capacity]) {
int i, j, k;
for(j=0; j<n, j++)
for(i=n-1; i>=0; i--) {
x[i][j] = B[i][j];
for(k=i+1; k<n; k++)
x[i][j] -= A[i][k] * x[k][j];
x[i][j] /= A[i][j]; } }
void Product (int n, const float A[] [capacity], const float x[]
[capacity], float B[] [capacity]) {
int i, j, k;
for(i=0; i<n; i++)
for
void Output (int n, const float A[] [capacity]) {
int i, j;
for(i=0; i<n; i++) {
for(j=0; j<n; j++)
printf("%5.1f", A[i][j]);
printf("\n"); } }
char peek() {
char c = getchar();
unget(c,stdin);
return n; }
Please notice that the only two portions missing from this program are the Product function that multiplies the two matrices and the heart of the main() function. I can handle the main(), I hope. I need help with the Product function please.
The intended output of this program is as follows:
input the n by n upper-triangular matrix A by rows:
1 2 3
0 1 2
0 0 1
input the n by n matrix B by rows:
1 2 3
4 5 6
7 8 9
the solution of AX = B is:
-0.0 -0.0 -0.0
-10.0 -11.0 -12.0
7.0 8.0 9.0
the product AX equals
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0
#include <stdio.h>
const capacity = 101;
void Input (int* nPtr, float A[] [capacity]);
void Solution (int n, const float A[] [capacity], const float B[] [capacity], float x[] [capacity]);
void Product (int n, const float A[] [capacity], const float x[] [capacity], float B[] [capacity]);
void Output (int n, const float A[] [capacity]);
char peek();
int main(void)
{
return 0;
}
void Input (int* nPtr, float A[] [capacity]) {
int i, j;
for(*nPtr=0; peek() != '\n'; (*nPtr)++)
scanf("%f", &A[0][*nPtr]);
for(i=1; i<*nPtr; i++) {
for(j=0; j<*nPtr; j++)
scanf("%f", &A[i][j]); }
get char(); }
void Solution (int n, const float A[] [capacity], const float B[]
[capacity], float x[] [capacity]) {
int i, j, k;
for(j=0; j<n, j++)
for(i=n-1; i>=0; i--) {
x[i][j] = B[i][j];
for(k=i+1; k<n; k++)
x[i][j] -= A[i][k] * x[k][j];
x[i][j] /= A[i][j]; } }
void Product (int n, const float A[] [capacity], const float x[]
[capacity], float B[] [capacity]) {
int i, j, k;
for(i=0; i<n; i++)
for
void Output (int n, const float A[] [capacity]) {
int i, j;
for(i=0; i<n; i++) {
for(j=0; j<n; j++)
printf("%5.1f", A[i][j]);
printf("\n"); } }
char peek() {
char c = getchar();
unget(c,stdin);
return n; }
Please notice that the only two portions missing from this program are the Product function that multiplies the two matrices and the heart of the main() function. I can handle the main(), I hope. I need help with the Product function please.
The intended output of this program is as follows:
input the n by n upper-triangular matrix A by rows:
1 2 3
0 1 2
0 0 1
input the n by n matrix B by rows:
1 2 3
4 5 6
7 8 9
the solution of AX = B is:
-0.0 -0.0 -0.0
-10.0 -11.0 -12.0
7.0 8.0 9.0
the product AX equals
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0
you first need to know the formula for matrix multiplication ...
for multiplying a
MxN matrix by a NxP matrix
C[i,j] = sigma (0 -> N) A[i][k] * B[k][j]; /// sorry this is the best i could do with writing the formula )
so basically you will have three for loops one inside the other like below
you will have two matrix of size MxN and N X P
also note when you multiply to matrix of size PxQ and RxS
matrix muliplication is not defined when
Q != R
for(i=0; i<M; i++)
{
for(j=0; j<P; j++)
{
C[i][j] = 0.0;
for(k=0; k<N; k++)
{
C[i][j] += A[i][k] * B[k][j];
}
}
}
-----------------
I just wrote the above off hand , you are advised to check the formula and also ensure what you do is correct but the basic idea is correct
for multiplying a
MxN matrix by a NxP matrix
C[i,j] = sigma (0 -> N) A[i][k] * B[k][j]; /// sorry this is the best i could do with writing the formula )
so basically you will have three for loops one inside the other like below
you will have two matrix of size MxN and N X P
also note when you multiply to matrix of size PxQ and RxS
matrix muliplication is not defined when
Q != R
for(i=0; i<M; i++)
{
for(j=0; j<P; j++)
{
C[i][j] = 0.0;
for(k=0; k<N; k++)
{
C[i][j] += A[i][k] * B[k][j];
}
}
}
-----------------
I just wrote the above off hand , you are advised to check the formula and also ensure what you do is correct but the basic idea is correct
also a good book to have is
Numerical Recipes in C ,
only thing I hate about the book is that in it he arrays begin from 1 and not 0 and hence all programs have to be modified slightly before you can use if youwant array subscripts to start with 0
Numerical Recipes in C ,
only thing I hate about the book is that in it he arrays begin from 1 and not 0 and hence all programs have to be modified slightly before you can use if youwant array subscripts to start with 0
That means that capacity is a global constant ?
It is a bit annoying to operate dynamic multi-dimensinal arrays in pure C, but not impossible. Also you can use pointers instead of passing arrays, assuming both matrices have the same size. Something like this:
void Product (iconst float **A, const float **x, float **B, int rows, int cols)
Anyway, the basic matrix multiplication algorithm you will find in the internet is:
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
for (k = A[i][j] = 0; k < n; k++)
A[i][j] += B[i][k] * C[k][j];
Now you must have to take a decision of how to manipulate the data itself.
It is a bit annoying to operate dynamic multi-dimensinal arrays in pure C, but not impossible. Also you can use pointers instead of passing arrays, assuming both matrices have the same size. Something like this:
void Product (iconst float **A, const float **x, float **B, int rows, int cols)
Anyway, the basic matrix multiplication algorithm you will find in the internet is:
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
for (k = A[i][j] = 0; k < n; k++)
A[i][j] += B[i][k] * C[k][j];
Now you must have to take a decision of how to manipulate the data itself.
I was written very slow, I think....
ASKER
Thanks for all the helpful hints. However, my real problem wasn't in figuring out how to multiply matrices, but rather how to tie the function in with the rest of the program with proper syntax. Thanks again.
You should have a matrix_multiply function which takes the two arrays and their sizes as argument ,
you need to pass only P, Q and R to multiply a PxQ and QxR matrices,
you will also need to pass as argument the result matrix ,
The pointer to the first element can be passed , and since you know the size you can then access all the elements.
you need to pass only P, Q and R to multiply a PxQ and QxR matrices,
you will also need to pass as argument the result matrix ,
The pointer to the first element can be passed , and since you know the size you can then access all the elements.
ASKER
Keeping in mind everybody's advice, I have come up with the following program for the previously described out:
#include <stdio.h>
const capacity = 101;
void Input (int* nPtr, float A[] [capacity]);
void Solution (int n, const float A[] [capacity], const float B[] [capacity], float x[] [capacity]);
void Product (int n, const float A[] [capacity], const float x[] [capacity], float B[] [capacity]);
void Output (int n, const float A[] [capacity]);
char peek();
int main(void)
{
int n;
float A[capacity], B[capacity], x[capacity];
printf("input the n by n upper-triangular matrix A by rows: ");
Input(&n, A);
printf("input the n by n matrix B by rows: ");
Input(&n, B);
printf("the solution of AX = B by rows: ");
Solution(n, A, B, x);
Output(n, A);
printf("the product AX equal ");
Product(n, A, x, B);
Output(n, A);
return 0;
}
void Input (int* nPtr, float A[] [capacity]) {
int i, j;
for(*nPtr=0; peek() != '\n'; (*nPtr)++)
scanf("%f", &A[0][*nPtr]);
for(i=1; i<*nPtr; i++) {
for(j=0; j<*nPtr; j++)
scanf("%f", &A[i][j]); }
get char(); }
void Solution (int n, const float A[] [capacity], const float B[] [capacity], float x[] [capacity]) {
int i, j, k;
for(j=0; j<n, j++)
for(i=n-1; i>=0; i--) {
x[i][j] = B[i][j];
for(k=i+1; k<n; k++)
x[i][j] -= A[i][k] * x[k][j];
x[i][j] /= A[i][j]; } }
void Product (int n, const float A[] [capacity], const float x[] [capacity], float B[] [capacity]) {
int i, j, k;
for(i=0; i<n; i++)
for(j=0; j<n; j++)
for(k=A[i][j]=0; k<n; k++)
A[i][j] += B[i][k] * x[k][j]; }
void Output (int n, const float A[] [capacity]) {
int i, j;
for(i=0; i<n; i++) {
for(j=0; j<n; j++)
printf("%5.1f", A[i][j]);
printf("\n"); } }
char peek() {
char c = getchar();
unget(c,stdin);
return n; }
However, I am being notified of several errors that I don't understand how to solve. A quick scan over the program would be appreciated. Thank you.
#include <stdio.h>
const capacity = 101;
void Input (int* nPtr, float A[] [capacity]);
void Solution (int n, const float A[] [capacity], const float B[] [capacity], float x[] [capacity]);
void Product (int n, const float A[] [capacity], const float x[] [capacity], float B[] [capacity]);
void Output (int n, const float A[] [capacity]);
char peek();
int main(void)
{
int n;
float A[capacity], B[capacity], x[capacity];
printf("input the n by n upper-triangular matrix A by rows: ");
Input(&n, A);
printf("input the n by n matrix B by rows: ");
Input(&n, B);
printf("the solution of AX = B by rows: ");
Solution(n, A, B, x);
Output(n, A);
printf("the product AX equal ");
Product(n, A, x, B);
Output(n, A);
return 0;
}
void Input (int* nPtr, float A[] [capacity]) {
int i, j;
for(*nPtr=0; peek() != '\n'; (*nPtr)++)
scanf("%f", &A[0][*nPtr]);
for(i=1; i<*nPtr; i++) {
for(j=0; j<*nPtr; j++)
scanf("%f", &A[i][j]); }
get char(); }
void Solution (int n, const float A[] [capacity], const float B[] [capacity], float x[] [capacity]) {
int i, j, k;
for(j=0; j<n, j++)
for(i=n-1; i>=0; i--) {
x[i][j] = B[i][j];
for(k=i+1; k<n; k++)
x[i][j] -= A[i][k] * x[k][j];
x[i][j] /= A[i][j]; } }
void Product (int n, const float A[] [capacity], const float x[] [capacity], float B[] [capacity]) {
int i, j, k;
for(i=0; i<n; i++)
for(j=0; j<n; j++)
for(k=A[i][j]=0; k<n; k++)
A[i][j] += B[i][k] * x[k][j]; }
void Output (int n, const float A[] [capacity]) {
int i, j;
for(i=0; i<n; i++) {
for(j=0; j<n; j++)
printf("%5.1f", A[i][j]);
printf("\n"); } }
char peek() {
char c = getchar();
unget(c,stdin);
return n; }
However, I am being notified of several errors that I don't understand how to solve. A quick scan over the program would be appreciated. Thank you.
void Input (int* nPtr, float A[] [capacity]);
float A[capacity], B[capacity], x[capacity];
Input(&n, A);
float A[] [capacity] is different from float A[capacity]
float A[capacity], B[capacity], x[capacity];
Input(&n, A);
float A[] [capacity] is different from float A[capacity]
you have declared A as
float A[capacity], B[capacity], x[capacity]; /// i.e A is a one dimensional array of size capacity
while you call Input as
Input(&n, A);
and Input expects the second argument to be float A[][capacity]
see
void Input (int* nPtr, float A[][capacity]);
float A[capacity], B[capacity], x[capacity]; /// i.e A is a one dimensional array of size capacity
while you call Input as
Input(&n, A);
and Input expects the second argument to be float A[][capacity]
see
void Input (int* nPtr, float A[][capacity]);
other error includes
1) get char(); }
which should be a single word getchar()
2) for(j=0; j<n, j++) in void Solution()
that should be a ';' after j<n and not a ','
3) in void Product()
for(k=A[i][j]=0; k<n; k++)
you are asisgning to A which you have declared to be constant
void Product (int n, const float A[] [capacity], const float x[] [capacity], float B[] [capacity]) {
1) get char(); }
which should be a single word getchar()
2) for(j=0; j<n, j++) in void Solution()
that should be a ';' after j<n and not a ','
3) in void Product()
for(k=A[i][j]=0; k<n; k++)
you are asisgning to A which you have declared to be constant
void Product (int n, const float A[] [capacity], const float x[] [capacity], float B[] [capacity]) {
and in
char peek() {
char c = getchar();
unget(c,stdin);
return n; }
your "n" is undeclared
char peek() {
char c = getchar();
unget(c,stdin);
return n; }
your "n" is undeclared
get char();
is not valid syntax
for(j=0; j<n, j++)
should have ; in place of ,
is not valid syntax
for(j=0; j<n, j++)
should have ; in place of ,
ASKER
Ok, what do you suppose the following error means:
"Data type incomplete"
in reference to the following line:
float A[][capacity], B[][capacity], x[][capacity];
"Data type incomplete"
in reference to the following line:
float A[][capacity], B[][capacity], x[][capacity];
The size of the array has to be known at compile time , hence you cant declare arrays like
float A[][capacity], B[][capacity], x[][capacity];
float A[][capacity], B[][capacity], x[][capacity];
ASKER
Thank you all for your input. My program is now down to only 2 errors.
First, the compiler is having a problem with "unget" portion of the "unget(c, stdin)" function.
Second is the error that avizit points out:
"The size of the array has to be known at compile time , hence you cant declare arrays like
float A[][capacity], B[][capacity], x[][capacity];"
I don't understand why the arrays cannot be initially declared in this manner, since they are later defined within the program through the functions....
First, the compiler is having a problem with "unget" portion of the "unget(c, stdin)" function.
Second is the error that avizit points out:
"The size of the array has to be known at compile time , hence you cant declare arrays like
float A[][capacity], B[][capacity], x[][capacity];"
I don't understand why the arrays cannot be initially declared in this manner, since they are later defined within the program through the functions....
the function is ungetc() and not unget()
float A[][capacity], B[][capacity], x[][capacity];
the above IS the definition of the arrays.
the above IS the definition of the arrays.
ASKER
Ok, great. Thanks a lot.
Now it's down to that one error with the declaration of the arrays.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
As I know you program in C++, it will be better to you to use it instead of pure C. Please advice.