Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Is these functions C90 and/or C99 ?
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[i][j] = 0.;
for (k = 0; k < dim; k++)
{
mat_s[i][j] = mat_s[i][j] + mat_e1[i][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[i][j] = 1;
else
A[i][j] = 0;

for (i = 0; i < DIMENSION; i++)
for (j = 0; j < DIMENSION; j++)
B[i][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[i][j]);
printf ("\n");
}

return EXIT_SUCCESS;
}

Report this thread to moderator Post Follow-up to this message
Old Post
Bernard
06-26-04 12:06 AM


Re: Is these functions C90 and/or C99 ?
Bernard wrote:
>
>
> 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[i][j] = 0.;
> 	  for (k = 0; k < dim; k++)
> 	    {
> 	      mat_s[i][j] = mat_s[i][j] + mat_e1[i][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[i][j] = 1;
>       else
> 	A[i][j] = 0;
>
>   for (i = 0; i < DIMENSION; i++)
>     for (j = 0; j < DIMENSION; j++)
>       B[i][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[i][j]);
>       printf ("\n");
>     }
>
>   return EXIT_SUCCESS;
> }

> gcc -Wall -std=c89 -pedantic -o main main.c
main.c: In function `Product_array':
main.c:8: warning: ISO C90 forbids variable-size array `mat_e1'
main.c:8: warning: ISO C90 forbids variable-size array `mat_e1'
main.c:8: warning: ISO C90 forbids variable-size array `mat_e2'
main.c:8: warning: ISO C90 forbids variable-size array `mat_e2'
main.c:8: warning: ISO C90 forbids variable-size array `mat_s'
main.c:8: warning: ISO C90 forbids variable-size array `mat_s'
> gcc -Wall -std=c99 -pedantic -o main main.c
>

Evidently, it is a valid C99 program but not a valid C89 program.

Report this thread to moderator Post Follow-up to this message
Old Post
E. Robert Tisdale
06-26-04 12:07 AM


Re: Is these functions C90 and/or C99 ?
On Wed, 23 Jun 2004 15:15:59 -0700, "E. Robert Tisdale"
<E.Robert.Tisdale@jpl.nasa.gov> wrote :
 

> Evidently, it is a valid C99 program but not a valid C89 program.

Thanks for your comments.
But if it is a valid C99 program, why does it only work if I use the old
style function parameters declaration ?

I'm aware that the following declaration is correct :
void Product_array(int dim, double mat_e1[dim][dim], double
mat_e2[dim][dim], double mat_s[dim][dim])

But why is the previous declaration wrong ?
I thought that the old-style and new-style functions parameters
declaration was the same ?!?

Report this thread to moderator Post Follow-up to this message
Old Post
Bernard
06-26-04 12:07 AM


Re: Is these functions C90 and/or C99 ?
Bernard wrote:

> E. Robert Tisdale wrote :
> 
> 
>
> But, if it is a valid C99 program, why does it only work
> if I use the old style function parameters declaration?

Evidently, the compiler needs to parse the type declaration of int dim
before is can use it to parse the dimensions of the arrays.

> I'm aware that the following declaration is correct:
> void Product_array(int dim, double mat_e1[dim][dim], double
> mat_e2[dim][dim], double mat_s[dim][dim])
>
> But why is the previous declaration wrong?
> I thought that the old-style and new-style functions parameters
> declaration was the same?

This is what you wrote:

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

Note that the type declaration of int dim is parsed before it is used
in the two-dimensional array type declarations.

Report this thread to moderator Post Follow-up to this message
Old Post
E. Robert Tisdale
06-26-04 12:07 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

C archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:19 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.