# Thread: values assignment to matrix pointer

1. ## values assignment to matrix pointer

This code build dinemically allocated square matrix (designed for the dimenssion of the 1D array), and copy into it an array. The empty places in the matrix are filled with -1.
The thing is I have the pointer to the matrix, but I cannot figure out how to asign vec1's values to it.

Code:
```#include<stdio.h>
#include<malloc.h>
#include<math.h>
#include<stdlib.h>

#define LENGTH   11

void CreateMatrix(int vec1[], int ***mat, int *size)
{

int i=0, j, k=0, count=0, temp;
while (vec1[i]>0)
{
count++;
i++;
}
temp=count;
while (sqrt(count)-4!=0)
count++;
*size =count/4;
*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);

while (k<temp)
for (i=0;i<*size;i++)
for (j=0;j<*size;j++)

{
(*mat)[i][j]= vec1[k];  //the problem is
k++;
}
while (k<(*size)*(*size))
for (i=0;i<*size;i++)
for (j=0;j<*size;j++)
{
*mat[i][j]=-1;
k++;
}
}

int main (void)
{
int vec1[LENGTH]={6,4,9,8,9,1,14,56,64,78,-1};
int **mat, size=0;
CreateMatrix(vec1, &mat, &size);
return 1;
}```

2. Originally Posted by ronenk
This code build dinemically allocated square matrix (designed for the dimenssion of the 1D array), and copy into it an array. The empty places in the matrix are filled with -1.
The thing is I have the pointer to the matrix, but I cannot figure out how to asign vec1's values to it.

The assignment that you flagged is OK, but the loop is not (goes beyond the end of the vec1[] array).

Code:
```while (k<temp)
for (i=0;i<*size;i++)
for (j=0;j<*size;j++)

{
(*mat)[i][j]= vec1[k];  //the problem is
k++;
}
while (k<(*size)*(*size))
for (i=0;i<*size;i++)
for (j=0;j<*size;j++)
{
*mat[i][j]=-1;
k++;
}```
Why not just something like this:
Code:
```  k = 0; /* actually was initialized to 0 */
for (i = 0; i < *size; i++) {
for (j = 0;j < *size; j++) {
if (k < temp) {
(*mat)[i][j]= vec1[k++];
}
else {
(*mat)[i][j] = -1;
}
}
}```
By the way, what's up with this:

Code:
```        while (sqrt(count)-4!=0)
count++;
*size =count/4;```
For your case isn't this the same as
Code:
```count = 16;
*size = 4;```
If the vector length was greater than 16, it's an infinite loop. If the vector length was less than or equal to 16, then count = 16, *size = 4.

Maybe the calling program should supply size (to create a size x size matrix).

Regards,

Dave

3. Why not just something like this:

Code:
k = 0; /* actually was initialized to 0 */
for (i = 0; i < *size; i++) {
for (j = 0;j < *size; j++) {
if (k < temp) {
(*mat)[i][j]= vec1[k++];
}
else {
(*mat)[i][j] = -1;
}
}
}
No reason... I accept this version.

By the way, what's up with this:

Code:
while (sqrt(count)-4!=0)
count++;
*size =count/4;

actually the intention was to do something like this:

Code:
```
while (vec1[i]>0)
{
count++;
i++;
}
temp=count;
while ((sqrt(count))%4!=0)   //error here
count++;
*size =count/4;```
In order to be able to be ajusted to any length of vec1. the thing is I get the error: "%' : illegal, left operand has type 'double ' " in marked line.

4. Well that's pretty clear. You can't use the modulus operator on anything other than integral types.

Quzah.

5. Originally Posted by quzah
Well that's pretty clear. You can't use the modulus operator on anything other than integral types.

Quzah.
I see. thanx.

6. Originally Posted by ronenk
actually the intention was to do something like this:

Code:
```

// count is the number of elements of the vector
while ((sqrt(count))%4!=0)   //error here
count++;
*size =count/4;```
In order to be able to be ajusted to any length of vec1. the thing is I get the error: "%' : illegal, left operand has type 'double ' " in marked line.

But what you want to do is find the smallest square matrix that can hold the values of the vector, right? Regardless of the problem with the % operator (you could use fmod(), by the way) I don't see where this would do it for, say count = 17. I think that a vector with 17 elements would result in a 16x16 matrix with your calculation --- check it out.

Here's a way (no floats, no square roots):

Code:
```#include <stdio.h>

int main()
{
int x, y;

while (1) {
printf("Enter the vector size: ");
if (scanf("%d", &y) != 1) {
break;
}
printf("You entered %d\n", y);

x = 1;
while (x * x < y) {
x++;
}
printf("Vector size = %d, matrix size is %d x %d\n\n", y, x, x);
}
return 0;
}```
Regards,

Dave

7. 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

8. Originally Posted by ronenk
this is the last version, which works fine, also with large array!
Thanx!
Code:
```	*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);

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
Be sure to free() everything that you got from malloc() before returning from main(). (I think it's a good habit to check the return value from malloc() each time, just in case...)

Regards,

Dave

9. OK, thanx.