0

I am coding a matrix, whose entries are polynomials with rational coefficients. Any help would be greatly appreciated.
I have rational number and rational polynomial declared:
rational_number.h

struct long_rational{
    long p;
    long q;
    };

typedef struct long_rational rational;

polynomial.h

#define MAX_DEGREE 200

struct rational_polynomial{
    long degree;
    rational coef[MAX_DEGREE]; //rational coefficients in increase power.
};

typedef struct rational_polynomial polynomial;

poly_mat.c in its entirety

#include "poly_mat.h"

#define NR_END 1
#define FREE_ARG char*

polynomial **poly_matrix( long nrl, long nrh, long ncl, long nch )
/* allocates a matrix with polynomial entries in the range m[nrl..nrh][ncl..nch] */
{
    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
    polynomial **m;
    /* allocate pointers to rows */
    m=( polynomial ** ) malloc( ( size_t )( ( nrow+NR_END )*sizeof( polynomial* ) ) );
    if ( !m ) nrerror( "allocation failure 1 in matrix()" );
    m += NR_END;
    m -= nrl;
    /* allocate rows and set pointers to them */
    m[nrl]=( polynomial * ) malloc( ( size_t )( ( nrow*ncol+NR_END )*sizeof( polynomial ) ) );
    if ( !m[nrl] ) nrerror( "allocation failure 2 in matrix()" );
    m[nrl] += NR_END;
    m[nrl] -= ncl;
    for ( i=nrl+1; i<=nrh; i++ ) m[i]=m[i-1]+ncol;
    /* return pointer to array of pointers to rows */
    return m;
}

void **free_poly_matrix( polynomial **m, long nrl, long nrh, long ncl, long nch )
/* free a polynomial matrix allocated by poly_matrix() */
{
    free( ( FREE_ARG ) ( m[nrl]+ncl-NR_END ) );
    free( ( FREE_ARG ) ( m+nrl-NR_END ) );
}

void init_random_poly_matrix( int **m, long nrl, long nrh, long ncl, long nch )
/* initialize a random polynomial matrix with coefficient <=100*/
{
    long i,j;
    long iseed = ( long )time( NULL );
    srand ( iseed );
    for ( i=nrl; i<=nrh; i++ )
    {
        for ( j=ncl; j<=nch; j++ )
        {
            m[i][j].degree=( rand()%MAX_DEGREE );
            for ( k=0;k<=MAX_DEGREE;k++ )
            {
                m[i][j].coef[k].p = (rand()%100 );
                m[i][j].coef[k].q = (1+rand()%100 );
            }
        }
    }
}

Here is the enigmatic error message:

gcc -Wall   -c -o poly_mat.o poly_mat.c
poly_mat.c: In function ‘init_random_poly_matrix’:
poly_mat.c:6: error: expected declaration specifiers before ‘(’ token
poly_mat.c:28: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
poly_mat.c:35: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
poly_mat.h:14: error: old-style parameter declarations in prototyped function definition
poly_mat.c:51: error: expected ‘{’ at end of input
make: *** [poly_mat.o] Error 1

poly_mat.h with missing semicolon filled.

#ifndef POLY_MAT_H
#define POLY_MAT_H

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "nrutil.h"
#include "rational_number.h"
#include "polynomial.h"

/* matrix with polynomial entries */
polynomial **poly_matrix( long nrl, long nrh, long ncl, long nch );
void init_random_poly_matrix( int **m, long nrl, long nrh, long ncl, long nch );
void **free_poly_matrix( polynomial **m, long nrl, long nrh, long ncl, long nch );

#endif

Now I can not access member of polynomials in the array with dot operator.
New Error Message:

gcc -Wall   -c -o poly_mat.o poly_mat.c
poly_mat.c: In function ‘init_random_poly_matrix’:
poly_mat.c:43: error: request for member ‘degree’ in something not a structure or union
poly_mat.c:46: error: request for member ‘coef’ in something not a structure or union
poly_mat.c:47: error: request for member ‘coef’ in something not a structure or union
make: *** [poly_mat.o] Error 1

Edit 2: Found the mistake. declare it as int** instead of polynomial**.

mtvec
  • 17,846
  • 5
  • 52
  • 83
user103500
  • 77
  • 1
  • 8
  • Are you sure that `polynomial` is visible in the .cpp file? And that it actually is the typedef at that point? – Bo Persson Apr 28 '12 at 08:48
  • There seem to be a lot of small errors in this code that would prevent compilation (or at least give compiler warnings), which suggests that you designed the code and typed it all in without testing any of it. *Start small, build up, test at every stage, never add to code that doesn't work.* – Beta Apr 28 '12 at 14:03

1 Answers1

0

without knowin poly_mat.h, but I grok the errormessage, that there is an syntactic error in poly_mat.h. I believe there is a missing bracket/brace or semikolon in or before line 14 while declaring the prototype for init_random_poly_matrix().

Peter Miehle
  • 5,984
  • 2
  • 38
  • 55