B
Bernard
Hello,
I would like your advices on the following two functions which multiply
two arrays.
About Multiply_array, I think that this function is ISO C90, but for
Product_array I have some doubts...
You can see that Product_array try to use the new advantages of C99
(Variable Length Array) but still I'm a little surprised that this
code can compile and run flawlessly (with gcc 3.2).
I notice that if I use the new function parameters definition :
void Product_array(double mat_e1[dim][dim], double mat_e2[dim][dim],
double mat_s[dim][dim], int dim)
It doesn't even compile !
So here are my questions :
1) Can you tell me if Multiply_array is ISO C90 ?
2) Can you tell me if Product_array is ISO C99 and tell me why it works
? (for instance if it's just a new function of the C99 ?)
Thanks in advance and here is the code :
#include <stdio.h>
#include <stdlib.h>
#define DIMENSION 10
void Product_array (mat_e1, mat_e2, mat_s, dim)
int dim;
double mat_e1[dim][dim], mat_e2[dim][dim], mat_s[dim][dim];
{
int i, j, k;
for (i = 0; i < dim; i++)
{
for (j = 0; j < dim; j++)
{
mat_s[j] = 0.;
for (k = 0; k < dim; k++)
{
mat_s[j] = mat_s[j] + mat_e1[k] * mat_e2[k][j];
}
}
}
}
void Multiply_array (void *A, void *B, int Nblignes, int
Nbcolonnes, int DimMultiplication, void *X)
{
int i, j, k;
double produit;
double *Matrice_A = A;
double *Matrice_B = B;
double *Matrice_X = X;
for (i = 0; i < Nblignes; i++)
for (j = 0; j < Nbcolonnes; j++)
{
produit = 0;
for (k = 0; k < DimMultiplication; k++)
produit =
produit + Matrice_A[i * DimMultiplication +
k] * Matrice_B[k * Nbcolonnes + j];
Matrice_X[i * Nbcolonnes + j] = produit;
}
}
int main (void)
{
double A[DIMENSION][DIMENSION];
double B[DIMENSION][DIMENSION];
double C[DIMENSION][DIMENSION];
int i, j;
for (i = 0; i < DIMENSION; i++)
for (j = 0; j < DIMENSION; j++)
if (i == j)
A[j] = 1;
else
A[j] = 0;
for (i = 0; i < DIMENSION; i++)
for (j = 0; j < DIMENSION; j++)
B[j] = i + j;
Product_array (A, B, C, DIMENSION);
/*Multiply_array(A, B, DIMENSION, DIMENSION, DIMENSION, C); */
for (i = 0; i < DIMENSION; i++)
{
for (j = 0; j < DIMENSION; j++)
printf ("%f ", C[j]);
printf ("\n");
}
return EXIT_SUCCESS;
}
I would like your advices on the following two functions which multiply
two arrays.
About Multiply_array, I think that this function is ISO C90, but for
Product_array I have some doubts...
You can see that Product_array try to use the new advantages of C99
(Variable Length Array) but still I'm a little surprised that this
code can compile and run flawlessly (with gcc 3.2).
I notice that if I use the new function parameters definition :
void Product_array(double mat_e1[dim][dim], double mat_e2[dim][dim],
double mat_s[dim][dim], int dim)
It doesn't even compile !
So here are my questions :
1) Can you tell me if Multiply_array is ISO C90 ?
2) Can you tell me if Product_array is ISO C99 and tell me why it works
? (for instance if it's just a new function of the C99 ?)
Thanks in advance and here is the code :
#include <stdio.h>
#include <stdlib.h>
#define DIMENSION 10
void Product_array (mat_e1, mat_e2, mat_s, dim)
int dim;
double mat_e1[dim][dim], mat_e2[dim][dim], mat_s[dim][dim];
{
int i, j, k;
for (i = 0; i < dim; i++)
{
for (j = 0; j < dim; j++)
{
mat_s[j] = 0.;
for (k = 0; k < dim; k++)
{
mat_s[j] = mat_s[j] + mat_e1[k] * mat_e2[k][j];
}
}
}
}
void Multiply_array (void *A, void *B, int Nblignes, int
Nbcolonnes, int DimMultiplication, void *X)
{
int i, j, k;
double produit;
double *Matrice_A = A;
double *Matrice_B = B;
double *Matrice_X = X;
for (i = 0; i < Nblignes; i++)
for (j = 0; j < Nbcolonnes; j++)
{
produit = 0;
for (k = 0; k < DimMultiplication; k++)
produit =
produit + Matrice_A[i * DimMultiplication +
k] * Matrice_B[k * Nbcolonnes + j];
Matrice_X[i * Nbcolonnes + j] = produit;
}
}
int main (void)
{
double A[DIMENSION][DIMENSION];
double B[DIMENSION][DIMENSION];
double C[DIMENSION][DIMENSION];
int i, j;
for (i = 0; i < DIMENSION; i++)
for (j = 0; j < DIMENSION; j++)
if (i == j)
A[j] = 1;
else
A[j] = 0;
for (i = 0; i < DIMENSION; i++)
for (j = 0; j < DIMENSION; j++)
B[j] = i + j;
Product_array (A, B, C, DIMENSION);
/*Multiply_array(A, B, DIMENSION, DIMENSION, DIMENSION, C); */
for (i = 0; i < DIMENSION; i++)
{
for (j = 0; j < DIMENSION; j++)
printf ("%f ", C[j]);
printf ("\n");
}
return EXIT_SUCCESS;
}