What the program does: Reads from a file a matrix (2d Array) with nrRows rows and nrColomns colomns. All elements of the matrix are int numbers between [0,100). The program has to rearrange all of the elements inside the matrix such that each element is equal to the index of the row it is in.
ex. 5 will be on line(row) 5, 58 on row 58.
The result is written in a file. I need to use only 1 matrix and all memory is allocated dynamically. If I have to add or erase elements, I also readjust the memory of the matrix. Also, I need to keep the shape of a matrix.
ex. 3 rows with 2 colomns. NOT 3 rows where row 1 has 1 colomn, row 2 has 3 colomns, etc..
I think reallocation doesn't work in my c program. The program itself works fine. Help?
#include<stdio.h>
#include<malloc.h>
unsigned short maxim(unsigned short A[100])
{
unsigned short max = 0;
for (unsigned short i = 0; i<100; ++i)
if (A[i] > max)
max = A[i];
return max;
}
void main()
{
FILE *pFile = fopen("test.txt", "r");
unsigned short nrRows, nrColomns;
/* If empty exit */
if (pFile == NULL)
return;
fscanf(pFile, "%d", &nrRows);
fscanf(pFile, "%d", &nrColomns);
/* Memory Allocation */
int** V = (int**)malloc(nrRows * sizeof(int*)); /* Number of lines */
for (unsigned short i = 0; i < nrRows; ++i)
V[i] = (int*)malloc(nrColomns * sizeof(int)); /* Number of colomns */
/* Read the elements */
for (unsigned short i = 0; i < nrRows; ++i)
for (unsigned short j = 0; j < nrColomns; ++j)
fscanf(pFile, "%d", &V[i][j]);
/* Find max + array */
unsigned short A[100] = { '\0' }; unsigned short max = 0;
for (unsigned short i = 0; i < nrRows; ++i)
for (unsigned short j = 0; j < nrColomns; ++j)
{
/* How many times each value between [0 and 100) is found inside the matrix */
A[V[i][j]]++;
/* Find the biggest element */
if (V[i][j] > max)
max = V[i][j];
}
/* Memory Reallocation */
unsigned short maxA = maxim(A); unsigned short ok = 0;
if (maxA > nrColomns){
nrColomns = maxA;
ok++;
}
if (max + 1 > nrRows){
nrRows = max + 1;
ok++;
}
//if (ok != 0)
//{
*V = realloc(*V, nrRows * sizeof(int*));
for (unsigned short i = 0; i < nrRows; i++)
V[i] = (int*)realloc(V, nrColomns * sizeof(int));
//}
/* Rearrange Values */
unsigned short bool = 1;
while (bool != 0)
{
bool = 0;
for (unsigned short i = 0; i < nrRows; ++i)
{
for (unsigned short j = 0; j < nrColomns; ++j)
{
if (V[i][j] != i)
{
/* Swap elements */
unsigned short k = 0;
while (k < nrColomns)
{
if (V[V[i][j]][k] != V[i][j])
{
bool = 1;
/* Do the swapping */
int swap = V[V[i][j]][k];
V[V[i][j]][k] = V[i][j];
V[i][j] = swap;
break;
}
else k++;
}
}
}
}
}
/* Extra Reallocation */
if (maxA < nrColomns)
{
nrColomns = maxA;
for (unsigned short i = 0; i < nrRows; ++i)
V[i] = (int*)realloc(V, nrColomns * sizeof(int));
}
/* Print Result into file */
pFile = fopen("out.txt", "w");
fprintf(pFile, "%d %d \n", nrRows, nrColomns);
for (unsigned short i = 0; i < nrRows; ++i)
{
for (unsigned short j = 0; j < nrColomns; ++j)
fprintf(pFile, "%d ", V[i][j]);
fprintf(pFile, "\n");
}
fclose(pFile);
_getch();
/* Memory Deallocation */
for (unsigned short i = 0; i < nrRows; ++i)
free(V[i]);
free(V);
}
It's just wierd... I've lost enough hours on this problem.
Example of test.txt
4 3 1 2 2 0 0 0 1 1 3 5 3 2