Thread: values assignment to matrix pointer

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 02:50 PM.

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