I wrote a program in c that creates randomly matrix.
It creates a string like this (3,-6,2;5,2,-9;-8,20,7). ";" cuts every row and a "," every column. Now i wrote a rust program that makes matrixaddition or mult. I call it with this:
./matrix ./test 3 3
"*" ./test 3 3
./matrix calls my rust program and i give it 3 arguments. (Matrix 1, Operator, Matrix2) It works and the calculation is fine but Matrix 1 and 2 are always equal. I think it is because I use srand depending on time and because i call it at the same time it creates two times the same. I also tested the Matrixrandomizer without including it in my rust call and it always creates different matrix.
Here you can see my c Code.
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
int main (int argc, char* argv[]) {
// Zufallszahlengenerator initialisieren
srand(time(NULL));
if(argc < 3) {
printf("Es fehlen Argumente");
}
char matrix[100] = "";
int r, c;
r = atoi(argv[1]);
c = atoi(argv[2]);
if(r > 0 && c > 0) {
for(int i = 0; i < r; i++) {
for(int j = 0; j < c; j++){
if(j == c - 1) {
int test = (1+rand()%9);
char buffer[50];
sprintf(buffer, "%d", test);
strcat(matrix, buffer);
}
if(j < c - 1){
int test = (1+rand()%9);
char buffer[50];
sprintf(buffer, "%d", test);
strcat(matrix, buffer);
strcat(matrix, ",");
}
}
if(i != r - 1) {
strcat(matrix, ";");
}
}
}
printf("%s", matrix);
}