I'm having a bit of trouble doing this and have searched High and low on the internet for help, but to no avail.
I'm basically trying to create a random matrix and print it out, I have a function double random()
which works and has been tested and I have defined a structure as follows:
typedef struct matrep {
unsigned rows, columns;
double *data;
} MATRIX;
for which I have allocated memory properly, I use this and my random function to create a random matrix but what happens is the pointer never moves,
MATRIX *rand_matrix( MATRIX *mat )
{
for ( int i=0; i < mat->rows; i++ )
{
for ( int j=0; j < mat->columns; j++ )
{
*(mat->data) = random() ;
}
}
return mat ;
}
I know it never moves because when I print out the matrix using this function
void print_matrix(MATRIX *mat )
{
int i, j ;
if ( (mat->data)==0 || (mat->rows)==0 || (mat->columns)==0 )
{
printf("Empty matrix\n" );
return ;
}
printf( "\n\nMatrix of dimensions %d x %d\n\n", mat->rows, mat->columns) ;
for ( i=0; i < mat->rows; i++ )
{
for ( j=0; j < mat->columns; j++ )
{
printf("\t%1.2lf", *(mat->data) );
}
printf("\n") ;
}
}
and exchange random in the matrix above with 'j' it ends up printing out a matrix with the correct number of rows and collumns but each value is equal to the biggest value of j.
Basically what I was hoping you could help me with is figuring out how to increment my *(mat->data)
pointer. I heard something about when you call the arrow operator it increments automatically but it doesnt seem to be working and when i try *(mat->data)++
I get a nice big error.
Any help would be great thanks a million.