C multiple files

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
beloni
Learning poster
Posts: 66
Joined: Thu Jan 05, 2006 1:41 pm
Location: Pelotas, RS, Brazil

C multiple files

Post by beloni »

hello,
I want to split my C program in multiple files...
but what is the best way to manage variables from those splited files? just make them extern and define them in main?

Code: Select all

/*matrix.h*/
#ifndef MATRIX_H
#define MATRIX_H

#define MAX_SIZE 200
#define ROW 0
#define COL 1

#include <stdio.h>

	extern double matrix[3][MAX_SIZE][MAX_SIZE];
	extern int bounds[3][2];

	
int getncol( FILE *fin );

void read_matrix( FILE *fin, int m_id );

void print_matrix( int m_id );

#endif

Code: Select all

/*matrix.c*/
#include "matrix.h"

#include <stdio.h>


int getncol( FILE *fin )
{
	char buffer[10000] = {0};
	char *pb;
	int n = 0, counter = 0;
	double v;

	fgets( buffer, 999, fin );
	for( pb = buffer; *pb != '\n'; pb += n )
	{
		sscanf( pb, "%lf%n", &v, &n );
		counter++;
	}
	fseek( fin, 0, SEEK_SET );	/* sets "file pointer" to begin */
	return counter;
}


void read_matrix( FILE *fin, int m_id )
{
	int i = 0, j = 0;

	bounds[m_id][COL] = getncol( fin );
	while( !feof(fin) )
	{
		for( j = 0; j < bounds[m_id][COL]; j++ )
			fscanf( fin, "%lf", &matrix[m_id][i][j] );
		i++;
	}
	bounds[m_id][ROW] = i-1;
}


void print_matrix( int m_id )
{
	int i, j;
	for( i = 0; i < bounds[m_id][ROW]; i++ )
	{
		for( j = 0; j < bounds[m_id][COL]; j++ )
			printf( "%.2lf ", matrix[m_id][i][j] );
		printf( "\n" );
	}
}

Code: Select all

/*multiply.h*/
#ifndef MULTIPLY_H
#define MULTIPLY_H

/*#include <pthread.h>*/


/*	static pthread_mutex_t thread_locker = PTHREAD_MUTEX_INITIALIZER;*/

void* row_multiply( void *info );

#endif

Code: Select all

/*multiply.c*/
#include "matrix.h"
#include "multiply.h"

#include <stdio.h>

#include <pthread.h>


void* row_multiply( void *info )
{
	int i, j;
	int *curr_row = (int*)info;

/*	pthread_mutex_lock( &thread_locker );*/

/*	printf( "multiplying at row: %d\n", *curr_row );*/

	for( i = 0; i < bounds[1][COL]; i++ )	/* for each column from second */
		for( j = 0; j < bounds[1][ROW]; j++ )
			matrix[2][*curr_row][i] += matrix[0][*curr_row][j] * matrix[1][j][i];

/*	pthread_mutex_unlock( &thread_locker );*/
}

Code: Select all

/*main.c*/
#include "matrix.h"
#include "multiply.h"

#include <stdio.h>
#include <stdlib.h>

#include <pthread.h>

	double matrix[3][MAX_SIZE][MAX_SIZE] = { { {0.0} } };
   int bounds[3][2] = { {0.0} };


int main( int argc, char *argv[] )
{
	int i;
	int curr_pos[MAX_SIZE];
	pthread_t threads[MAX_SIZE];
	FILE *fin;

	if( argc != 3 ) {	fprintf( stderr, "INVALID ARGUMENTS\n" ); exit(1); }
	
	fin = fopen( argv[1], "rt" );
	if( !fin ) { fprintf( stderr, "UNABLE TO OPEN FILE STREAM %s\n", argv[1] ); exit(2); } 
	read_matrix( fin, 0 );
	fclose( fin );
/*	print_matrix( 0 );*/

	fin = fopen( argv[2], "rt" );
	if( !fin ) { fprintf( stderr, "UNABLE TO OPEN FILE STREAM %s\n", argv[2] ); exit(2); } 
	read_matrix( fin, 1 );
	fclose( fin );
/*	print_matrix( 1 );*/

	/* crucial test */
	if( bounds[0][COL] != bounds[1][ROW] )
	{
		fprintf( stderr, "UNABLE TO PERFORM MATRIX MULTIPLICATION\n" );
		exit(3);
	}

	for( i = 0; i < bounds[0][ROW]; i++ )
	{
		curr_pos[i] = i;	/* i changes on thread main, but pos[i] doesn't */
		pthread_create( &threads[i], 0, row_multiply, (void*)&curr_pos[i] );
	}

	for( i = 0; i < bounds[0][ROW]; i++ )
		pthread_join( threads[i], 0 );

	bounds[2][ROW] = bounds[0][ROW];
	bounds[2][COL] = bounds[1][COL];
	print_matrix( 2 );

	pthread_exit(0);
	return 0;
}
thanks
"A machine can do the work of fifty ordinary men, but no machine can do the work of one extraordinary man.", Shahriar Manzoor
Post Reply

Return to “C”