#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
using namespace std;
struct DArray {
int ** data;
int col;
int row;
};
int ** allocArr(int row, int col);
void deallocArr(int ** target, int row);
void readFile(int ** target, const char * filename, int row, int col);
void findClass(DArray train_data, DArray train_class, DArray test_data);
int main(){
char buf[1024*1024];
//alloc
int ** train_data = allocArr(11293, 8165);
int ** train_class = allocArr(11293, 20);
int ** test_data = allocArr(7528, 8165);
readFile(train_data, "DataSet/train_data11293x8165", 11293, 8165);
readFile(train_class, "DataSet/train_class11293x20", 11293, 20);
readFile(test_data, "DataSet/test_data7528x8165", 7528, 8165);
DArray train_d;
DArray train_c;
DArray test_d;
train_d.data = train_data;
train_d.row = 11293;
train_d.col = 8163;
train_c.data = train_class;
train_c.row = 11293;
train_c.col = 20;
test_d.data = test_data;
test_d.row = 7528;
test_d.col = 8163;
findClass(train_d, train_c, test_d);
//dealloc
deallocArr(train_data, 11293);
deallocArr(train_class, 11293);
deallocArr(test_data, 7528);
}
int ** allocArr(int row, int col){
int ** train_data = (int**)malloc(sizeof(int*) * row);
for(int i = 0; i < row; i++){
train_data[i] = (int*)malloc(sizeof(int) * col);
}
return train_data;
}
void deallocArr(int ** target, int row){
for(int i = 0; i < row; i++){
free(target[i]);
}
free(target);
}
void readFile(int ** target, const char * filename, int row, int col){
FILE * file = fopen(filename, "r");
for(int i = 0; i < row; i++){
for( int j = 0; j < col; j++){
if(j < col)
fscanf(file, "%d,", &target[i][j]);
else
fscanf(file, "%d", &target[i][j]);
}
}
fclose(file);
}
void findClass(DArray train_data, DArray train_class, DArray test_data){
for(int i = 0; i < test_data.row; i++){
//test_data[i];
int min_index = -1;
int min = 1000;
for(int j = 0; j < train_data.row; j++){
//train_data[j]
int sum = 0;
for(int k =0; k < train_data.col; k++){
int v = test_data.data[i][k] - train_data.data[j][k];
v = v >0 ? v : -v;
sum += v;
}
if(sum < min){
min_index = j;
min = sum;
}
}
//min이 어떤 클래스 인지.
for(int j = 0; j < train_class.col; j++){
if(train_class.data[min_index][j]){
printf("%d\n", j);
}
}
}
}