Originally Posted by
anduril462
Next time, please post the error messages you get that you don't understand.
I assume the header you posted is determ.h. A header should never include itself. Also, you always want include guards for all of your header files to avoid multiple inclusion. Don't re-declare matrix_element in main, it will be included with determ.h.
Take care of those things and you should find the error list much more manageable.
Well I was mostly just trying to get past the misuse of structures so I could compile, which is exactly what your solution did for me(thank you very much).
This project is already turned in, but I would like to fix it up some more just for personal betterment. I'll show you the output that I get now; which I have no idea how to interpret.
main.c
Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<sys/types.h>
#include<sys/wait.h>
#include"determ.h"
int main (int argc, char *argv[])
{
// VARIABLES //
int size, levels;
element *matrix = NULL;
int i= 0;
int j= 0;
int determinant;
if( argc != 4 ) //Check for correct number of arguments
{
printf("ERROR: Invalid number of arguments.\n", argc);
return 0;
}
else
{
// MATRIX ATTRIBUTES //
size= atoi(argv[2]); //Set size
levels= atoi(argv[3]); //Set levels
// CREATE MATRIX //
element* matrix = (element *)malloc( size*size*sizeof(element) ); //Allocate memory for matrix
fillMatrix(argv[1], matrix, size); //Read numbers for matrix
// CALCULATE DETERMINATE //
pid_t pid;
int pd[2];
int buffer;
int wait_= 0;
int sum;
for( i= size; i> 0; i--)
{
pipe(pd);
pid= fork();
//Child
if(pid == 0)
{
for( j= 0; j= levels; j++)
{
close(pd[0]); //Close read pipe
buffer += calcDET(matrix, size);
write(pd[1], &buffer, sizeof(int));
}
}
//Parent
else
{
close(pd[1]); //Close write pipe
read(pd[0], &determinant, sizeof(int));
sum += determinant;
}
}
printf("\n\nThe determinant of this matrix is: %d \n\n\n", sum);
free(matrix);
}
return 0;
}
determ.h
Code:
typedef struct matrix_element
{
int value, row, column;
}element;
//Reads file into matrix
void fillMatrix(const char *text, element* matrix, int size);
//Calculates sub matrix
element* getSUB(element* matrix, int size, int row, int col);
//Finds determinate
int buildDET(element* matrix, int size);
//Gathers determinate
int calcDET(element* matrix, int size);
f_determ.c
Code:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include"determ.h"
#define true 1
#define false 0
//FUNCTION: fillMatrix()
void fillMatrix(const char *txt_file, element* matrix, int size)
{
FILE *numbers = NULL;
int scan;
//Open file
numbers = fopen(txt_file, "r");
if( numbers == NULL )
{
printf("ERROR: Failed to open file.");
exit(0);
}
else
{
int r= 1;
int c= 1;
int i= 0;
//Fill matrix with numbers from file
while( ( fscanf(numbers, "%d", &scan) ) != EOF )
{
matrix[i].column = c;
matrix[i].row = r;
matrix[i].value = scan;
i++;
c++;
if( c % size == 0)
{
r++;
c= 1;
}
}
fclose(numbers);
}
}
//
//FUNCTION: getSUB
element* getSUB(element* matrix, int size, int row, int col)
{
int i, j, k= 0;
int nSize= size-1;
int scan;
element* subMatrix = (element *)malloc( sizeof(element)*(nSize)*(nSize) );
for( i= 0; i< size*size; i++)
if( (matrix[i].row != row) && (matrix[i].column != col) )
{
subMatrix[k] = matrix[i];
k++;
}
return subMatrix;
}
//
//FUNCTION: buildDET
int buildDET(element* matrix, int size)
{
int det;
if( size == 1 )
{
det= matrix[0].value;
return det;
}
else
{
buildDET( getSUB(matrix, size, matrix[size].row, matrix[size].column), size );
}
}
//FUNCTION: calcDET
int calcDET(element* matrix, int size)
{
int determinate;
while( size > 0 )
{
determinate = buildDET(matrix, size);
}
return determinate;
}
Output:
Code:
msu-mtrahan10@csci:~$ cd CSCI415/Assignment_2
msu-mtrahan10@csci:~/CSCI415/Assignment_2$ ./assignment2 nums.txt 3 3
assignment2: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
assignment2: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
assignment2: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
The determinant of this matrix is: 0