aboutsummaryrefslogtreecommitdiffstats
path: root/input.c
blob: bf4dfbdc477dea5a42cf9368802e0e4b47705326 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef DET_INPUT_C
#define DET_INPUT_C DET_INPUT_C
//how many digits is a number from -n is allowed to have
#define MAX_DIGITS 100
#include<stdlib.h>
#include<stdio.h>
#include"gmp.h"
#include<time.h>



void input_det(FILE* input,mpq_t ***det,size_t *size)
{


	fscanf(input,"%zu",size);
	(*det)=malloc(sizeof(mpq_t*)*(*size) + sizeof(mpq_t)*(*size)*(*size));
	size_t i,j;

	for(i=0;i<(*size);++i)
	{
		(*det)[i]=((mpq_t*)(((u_int8_t*)(*det)) + sizeof(mpq_t*)*(*size) + sizeof(mpq_t)*(*size)*i));
		for(j=0;j<(*size);++j)
		{
			
			mpq_init((*det)[i][j]);
			gmp_fscanf(input,"%Qi",(*det)[i][j]);
		}
	}
}
/*
 * places a randomised matrix in det
 * whose digits are 1 to MAX_DIGITS long
 *
 */
void random_table(mpq_t ***det,size_t size)
{
	(*det)=malloc(sizeof(mpq_t*)*size + sizeof(mpq_t)*size*size);
	size_t i,j;
	size_t sz;
	char *number;//we don't want a stack overflow so it goes on the heap
	number = malloc(sizeof(char)*MAX_DIGITS+1);
	srand(time(NULL));

	for(i=0;i<size;++i)
	{
		(*det)[i]=((mpq_t*)(((u_int8_t*)(*det)) + sizeof(mpq_t*)*size + sizeof(mpq_t)*size*i));
		for(j=0;j<size;++j)
		{
			
			
			for(sz=rand()%MAX_DIGITS,number[sz+1]='\0';sz>0;number[sz]=rand()%10+'0',--sz);
			number[0]=rand()%9 + '1';

			mpq_init((*det)[i][j]);
			gmp_sscanf(number,"%Qi",(*det)[i][j]);
		}
	}
}
#endif//#ifndef DET_INPUT_C