this is the last version, which works fine, also with large array!
Thanx!
Code:
#include<stdio.h>
#include<malloc.h>
#include<math.h>
#include<stdlib.h>
#define LENGTH 50
int *CreateMatrix(int vec1[], int ***mat, int *size)
{
int i=0, j, k=0, count=0;
*size=0;
while (vec1[i]>0)
{
count++;
i++;
}
while ((*size)*(*size)<count)
(*size)++;
*mat=(int **)malloc (sizeof(int) * (*size));
if (!mat) exit(0);
for (i=0;i<*size;i++)
(*mat)[i]=(int *)malloc (sizeof(int) * (*size));
if (!mat[i]) exit(0);
for (i = 0; i < *size; i++) {
for (j = 0;j < *size; j++) {
if (k < count) {
(*mat)[i][j]= vec1[k++];
}
else {
(*mat)[i][j] = -1;
}
}
}
return **mat;
}
int main (void)
{
int i,j;
int vec1[LENGTH]={6,4,9,8,9,1,14,56,64,78,2,73,92,14,64,34,5,4,32,91,34,456,213,56,2,135,2,-1};
int **mat, size=0;
CreateMatrix(vec1, &mat, &size);
for (i = 0; i < size; i++)
{
printf ("\n");
for (j = 0;j < size; j++)
printf ("%d\t", mat[i][j]);
}
}
Ronen