read csv now works

This commit is contained in:
AustrianToast 2024-10-04 13:05:02 +02:00
parent c85be15175
commit 5cb04ff579
Signed by: AustrianToast
GPG Key ID: 1B4D0AAF6E558816
4 changed files with 45 additions and 50 deletions

10
graph.c
View File

@ -2,12 +2,12 @@
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
void random_adjacency(const uint64_t row_length, const uint64_t column_length, uint64_t matrix[row_length][column_length]) { void random_adjacency(const uint64_t vertex_count, uint64_t matrix[vertex_count][vertex_count]) {
srand(time(NULL)); srand(time(NULL));
for (uint8_t row_index=0; row_index < row_length; row_index++) { for (uint8_t row_index=0; row_index < vertex_count; row_index++) {
for (uint8_t column_index=0; column_index < column_length; column_index++) { for (uint8_t column_index=0; column_index < vertex_count; column_index++) {
if(column_index == row_index) { if(column_index == row_index) {
matrix[row_index][column_index] = 0; matrix[row_index][column_index] = 0;
continue; continue;
@ -16,3 +16,7 @@ void random_adjacency(const uint64_t row_length, const uint64_t column_length, u
} }
} }
} }
int calculate_eccentricities() {
return 0;
}

View File

@ -3,8 +3,7 @@
#include <stdint.h> #include <stdint.h>
void random_adjacency(const uint64_t row_length, void random_adjacency(const uint64_t vertex_count,
const uint64_t column_length, uint64_t matrix[vertex_count][vertex_count]);
uint64_t matrix[row_length][column_length]);
#endif #endif

28
main.c
View File

@ -20,8 +20,8 @@ void benchmark() {
clock_t start_time; clock_t start_time;
for (uint64_t i = 0; i < iterations; i++) { for (uint64_t i = 0; i < iterations; i++) {
random_adjacency(row_length1, column_length1, matrix1); random_adjacency(row_length1, matrix1);
random_adjacency(row_length2, column_length2, matrix2); random_adjacency(row_length2, matrix2);
start_time = clock(); start_time = clock();
@ -43,18 +43,22 @@ void test() {
read_csv("csv/graph.csv", row_length1, column_length1, matrix1); read_csv("csv/graph.csv", row_length1, column_length1, matrix1);
// print_matrix(row_length1, column_length1, matrix1); printf("G:\n");
print_matrix(row_length1, column_length1, matrix1);
printf("\n");
// uint64_t new_matrix[row_length1][column_length1]; uint64_t new_matrix[row_length1][column_length1];
//
// matrix_multiply_basic(row_length1, column_length1, matrix1, matrix_multiply_basic(row_length1, column_length1, matrix1,
// row_length1, column_length1, matrix1, row_length1, column_length1, matrix1,
// new_matrix); new_matrix);
//
// print_matrix(row_length1, column_length1, new_matrix); printf("G²:\n");
print_matrix(row_length1, column_length1, new_matrix);
printf("\n");
} }
int main(void) { int main(void) {
test(); // test();
// benchmark(); benchmark();
} }

View File

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
void print_matrix(const uint64_t row_length, const uint64_t column_length, const uint64_t matrix[row_length][column_length]) { void print_matrix(const uint64_t row_length, const uint64_t column_length, const uint64_t matrix[row_length][column_length]) {
for (uint64_t column_index=0; column_index < column_length; column_index++) { for (uint64_t column_index=0; column_index < column_length; column_index++) {
@ -39,49 +40,36 @@ int matrix_multiply_basic(const uint64_t row_length1, const uint64_t column_leng
int read_csv(const char *file_name, const uint64_t row_length, const uint64_t column_length, uint64_t output_matrix[row_length][column_length]) { int read_csv(const char *file_name, const uint64_t row_length, const uint64_t column_length, uint64_t output_matrix[row_length][column_length]) {
FILE *file_ptr; FILE *file_ptr;
uint32_t bufsize = row_length*2; // have to account for delimiters uint32_t bufsize = row_length*2; // have to account for delimiters
char file_line[bufsize]; char buffer[bufsize];
char *value, *file_line;
uint64_t row_index = 0, column_index = 0;
file_ptr = fopen(file_name, "r"); file_ptr = fopen(file_name, "r");
if (file_ptr == NULL) { if (file_ptr == NULL) {
printf("Unable to open csv\n"); printf("Unable to open csv\n");
return 1; return 1;
} }
for (uint64_t column_index = 0; column_index < column_length; column_index++) {
printf("row_index: %lu\n", column_index);
if (fgets(file_line, bufsize, file_ptr) == NULL) { while ((file_line = fgets(buffer, bufsize, file_ptr)) != NULL) {
break; // This shit is just needed and I dont know why
} file_line[strcspn(file_line, "\n")] = 0;
char *bruh = strtok(file_line, ";,"); value = strtok(file_line, ";,");
for(uint64_t row_index = 0; row_index < row_length; row_index++) { // for some reason there are two NULLs at the end of a line
if (bruh == NULL) { // and I dont wanna increment the column_index
printf("Fuck You!!!"); if (value == NULL) {
continue; continue;
}
printf("column_index: %lu, bruh: %lu\n", row_index, strtoul(bruh, NULL, 0));
bruh = strtok(NULL, ";,");
} }
printf("\n");
while (value != NULL) {
output_matrix[row_index++][column_index] = strtoul(value, NULL, 0);
value = strtok(NULL, ";,");
}
row_index = 0;
column_index++;
} }
// uint64_t row_index = 0;
// uint64_t column_index = 0;
// while (fgets(file_line, bufsize, file_ptr)) {
// row_index = 0;
// char *bruh = strtok(file_line, ";,");
//
// while (bruh) {
// output_matrix[row_index][column_index] = strtoul(bruh, NULL, 0);
// printf("column_index: %lu, bruh: %s\n", row_index, bruh);
// bruh = strtok(NULL, ";,");
// row_index++;
// }
//
// printf("row_index: %lu\n", column_index);
// column_index++;
// }
fclose(file_ptr); fclose(file_ptr);
return 0; return 0;