I have to create a matrix but with the following requirements:
- create a function for it
- the matrix has to be dynamic allocated inside the function
- the matrix has to be returned as a parameter of the function (function has to be void)
What I tried:
void createMatrix(int n,float** matrix)
{
matrix = (float**)malloc(n * sizeof(float*));
for (int i = 0; i < n; i++)
matrix[i] = (float*)malloc(n* sizeof(float));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
matrix[i][j] = 0;
}
int main()
{
int n;
FILE* fis;
fis = fopen("fileIn.txt", "r");
if (!fis)
{
printf("File not opened");
return;
}
fscanf(fis, "%d", &n);
fclose(fis);
float** matrix;
createMatrix(n, &matrix);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
printf("%f ", matrix[i][j]);
printf("\n");
}
return;
}