read_csv doesn't work

This commit is contained in:
AustrianToast 2024-10-04 01:03:39 +02:00
parent 808d971af6
commit 04a8712b0f
Signed by: AustrianToast
GPG Key ID: 1B4D0AAF6E558816
5 changed files with 145 additions and 26 deletions

View File

@ -3,12 +3,13 @@
#include <stdlib.h>
void random_adjacency(const uint64_t row_length, const uint64_t column_length, uint64_t matrix[row_length][column_length]) {
srand(time(NULL));
for (uint8_t row_index=0; row_index < row_length; row_index++) {
for (uint8_t column_index=0; column_index < column_length; column_index++) {
if(column_index == row_index) {
matrix[row_index][column_index] = 1;
matrix[row_index][column_index] = 0;
continue;
}
matrix[row_index][column_index] = rand()%2;

View File

@ -3,6 +3,8 @@
#include <stdint.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 row_length,
const uint64_t column_length,
uint64_t matrix[row_length][column_length]);
#endif

62
main.c
View File

@ -1,16 +1,60 @@
#include "graph.h"
#include "matrix.h"
#include <stdint.h>
#include <stdio.h>
#include <time.h>
void benchmark() {
const uint64_t row_length1 = 100;
const uint64_t column_length1 = 100;
uint64_t matrix1[row_length1][column_length1];
const uint64_t row_length2 = 100;
const uint64_t column_length2 = 100;
uint64_t matrix2[row_length2][column_length2];
uint64_t new_matrix[row_length1][column_length2];
double elapsed_time = 0.0;
const uint64_t iterations = 10000;
clock_t start_time;
for (uint64_t i = 0; i < iterations; i++) {
random_adjacency(row_length1, column_length1, matrix1);
random_adjacency(row_length2, column_length2, matrix2);
start_time = clock();
matrix_multiply_basic(row_length1, column_length1, matrix1,
row_length2, column_length2, matrix2,
new_matrix);
elapsed_time += (double)(clock() - start_time) / CLOCKS_PER_SEC;
}
printf("%lu iterations of matrix_multiply_basic took roughly %f seconds\n", iterations, elapsed_time);
printf("1 iteration of matrix_multiply_basic took on average roughly %f seconds\n", elapsed_time/iterations);
}
void test() {
const uint64_t row_length1 = 5;
const uint64_t column_length1 = 5;
uint64_t matrix1[row_length1][column_length1];
read_csv("csv/graph.csv", row_length1, column_length1, matrix1);
// print_matrix(row_length1, column_length1, matrix1);
// uint64_t new_matrix[row_length1][column_length1];
//
// matrix_multiply_basic(row_length1, column_length1, matrix1,
// row_length1, column_length1, matrix1,
// new_matrix);
//
// print_matrix(row_length1, column_length1, new_matrix);
}
int main(void) {
const uint64_t row_length = 5;
const uint64_t column_length = 5;
uint64_t matrix1[row_length][column_length];
uint64_t matrix2[row_length*2][column_length*2];
random_adjacency(row_length, column_length, matrix1);
random_adjacency(row_length*2, column_length*2, matrix2);
print_matrix(row_length, column_length, matrix1);
test();
// benchmark();
}

View File

@ -1,9 +1,10 @@
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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 row_index=0; row_index < row_length; row_index++) {
printf("%lu ", matrix[row_index][column_index]);
@ -12,15 +13,77 @@ void print_matrix(const uint64_t row_length, const uint64_t column_length,
}
}
int matrix_multiply_basic(const uint64_t row_length1,
const uint64_t column_length1,
const uint64_t matrix1[row_length1][column_length1],
const uint64_t row_length2,
const uint64_t column_length2,
const uint64_t matrix2[row_length2][column_length2],
int matrix_multiply_basic(const uint64_t row_length1, const uint64_t column_length1, const uint64_t matrix1[row_length1][column_length1],
const uint64_t row_length2, const uint64_t column_length2, const uint64_t matrix2[row_length2][column_length2],
uint64_t output_matrix[row_length1][column_length2]) {
int ret = 0;
if (row_length1 != column_length2) {
return 1;
}
return ret;
for (uint64_t i = 0; i < row_length1; i++) {
for(uint64_t j = 0; j < column_length2; j++) {
uint64_t sum = 0;
for (uint64_t k = 0; k <row_length1; k++) {
sum += matrix1[i][k] * matrix2[k][j];
}
output_matrix[i][j] = sum;
}
}
return 0;
}
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;
uint32_t bufsize = row_length*2; // have to account for delimiters
char file_line[bufsize];
uint64_t row_index = 0;
uint64_t column_index = 0;
file_ptr = fopen(file_name, "r");
if (file_ptr == NULL) {
printf("Unable to open csv\n");
return 1;
}
for (column_index = 0; column_index < column_length; column_index++) {
printf("row_index: %lu\n", column_index);
if (fgets(file_line, bufsize, file_ptr) == NULL) {
break;
}
char *bruh = strtok(file_line, ";,");
for(row_index = 0; row_index < row_length; row_index++) {
if (bruh == NULL) {
printf("Fuck You!!!");
continue;
}
printf("column_index: %lu, bruh: %lu\n", row_index, strtoul(bruh, NULL, 0));
bruh = strtok(NULL, ";,");
}
printf("\n");
}
// 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);
return 0;
}

View File

@ -3,12 +3,16 @@
#include <stdint.h>
void print_matrix(const uint64_t row_length, const uint64_t column_length,
void print_matrix(const uint64_t row_length,
const uint64_t column_length,
const uint64_t matrix[row_length][column_length]);
// Takes three matrices, the first two will be multiplied and restult will be written to output_matrix.
// Matrix requirements are as specified in the parameters.
// Function return 0 on success and 1 on failure.
/*
First two matrices will be multiplied and
restult will be written to output_matrix.
Matrix requirements are as specified in the parameters.
Function return 0 on success and 1 on failure.
*/
int matrix_multiply_basic(const uint64_t row_length1,
const uint64_t column_length1,
const uint64_t matrix1[row_length1][column_length1],
@ -17,4 +21,9 @@ int matrix_multiply_basic(const uint64_t row_length1,
const uint64_t matrix2[row_length2][column_length2],
uint64_t output_matrix[row_length1][column_length2]);
int read_csv(char *file_name,
uint64_t row_length,
uint64_t column_length,
uint64_t output_matrix[row_length][column_length]);
#endif