Solved

Posted on 2004-10-03
163 Views
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.
0
Question by:biloonline
• 9
• 7
• 4
• +2

LVL 55

Expert Comment

Since this appear to be a homework, you have to post some code, at least a skeleton.
As I know you program in C++, it will be better to you to use it instead of pure C. Please advice.
0

Author Comment

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.
0

Author Comment

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
0

LVL 84

Expert Comment

0

LVL 11

Expert Comment

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
0

LVL 11

Expert Comment

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

0

LVL 55

Expert Comment

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.
0

LVL 55

Expert Comment

I was written very slow, I think....
0

Author Comment

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.
0

LVL 11

Expert Comment

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.

0

Author Comment

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.

0

LVL 84

Expert Comment

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]
0

LVL 11

Expert Comment

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]);
0

LVL 11

Expert Comment

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]) {

0

LVL 11

Expert Comment

and in

char peek() {
char c = getchar();
unget(c,stdin);
return n; }

0

LVL 84

Expert Comment

get char();
is not valid syntax
for(j=0; j<n, j++)
should have ; in place of ,
0

Author Comment

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];
0

LVL 11

Expert Comment

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];
0

Author Comment

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....
0

LVL 11

Expert Comment

the function is ungetc() and not unget()
0

LVL 11

Expert Comment

float A[][capacity], B[][capacity], x[][capacity];

the above IS the definition of the arrays.
0

Author Comment

Ok, great. Thanks a lot.

Now it's down to that one error with the declaration of the arrays.
0

LVL 8

Accepted Solution

ssnkumar earned 63 total points
> 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....

What you have to understand is, there is no distinction between declaration and definition here. Actually in case of variables, declaration itself is defintion (except in the case of extern variables, where the definition will be elsewhere).
So, the memory has to be allocated and compiler needs to know the size of the array.
And C doesn't allow dynamic arrays.
If you want to use dynamic arrays, then you will have to use pointers and assign memory at run time.

One more way to implement dynamic array is like this:

#include <stdio.h>

fun(char *str)
{
char arr[sizeof(str) + 1];

printf("SIZE OF ARR = %d\n", sizeof(arr));
}

main()
{
int n;
char *str = NULL;

printf("Enter number -> ");
scanf("%d", &n);
str = (char *) malloc(n * sizeof(char));
fun(str);
free(str);
}

Hope this helps....

-ssnkumar
0

LVL 55

Assisted Solution

Jaime Olivares earned 62 total points
That's why I suggested you to use this fashion:

void Product (const float **A, const float **x, float **B, int rows, int cols)

or, if you prefere, create a structure similar to this:

typedef struct {
const float **data;
int rows;
int cols;
} Matrix;

and use this function declaraction:

void Product (Matrix *A, Matrix *x, Matrix *B)
0

## Featured Post

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn hoâ€¦
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address.Â This address might be address of another variable/address of devices/address of fuâ€¦
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.