values assignment to matrix pointer

This is a discussion on values assignment to matrix pointer within the C Programming forums, part of the General Programming Boards category; This code build dinemically allocated square matrix (designed for the dimenssion of the 1D array), and copy into it an ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    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 
                                                  //about this stage
      		       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;
    }
    Last edited by ronenk; 02-28-2005 at 01:50 PM.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote 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).

    Instead of this:
    Code:
    while (k<temp)
    		for (i=0;i<*size;i++)
    		   for (j=0;j<*size;j++)
    		
    		   {
    		        (*mat)[i][j]= vec1[k];  //the problem is 
                                                  //about this stage
      		       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
    Last edited by Dave Evans; 02-28-2005 at 03:17 PM.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    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. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well that's pretty clear. You can't use the modulus operator on anything other than integral types.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    Quote 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. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote 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
    Last edited by Dave Evans; 03-01-2005 at 08:25 AM.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    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
    Last edited by ronenk; 03-01-2005 at 09:27 AM.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote 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. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    OK, thanx.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers, structures, and malloc
    By lugnut in forum C Programming
    Replies: 24
    Last Post: 10-09-2008, 04:52 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Very handy matrix functions - part 1
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 05-20-2004, 10:38 AM
  4. Matrix Reloaded Questions (SPOILERS_
    By Xei in forum A Brief History of Cprogramming.com
    Replies: 73
    Last Post: 10-19-2003, 02:21 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21