column-major matrix Q....

This is a discussion on column-major matrix Q.... within the C Programming forums, part of the General Programming Boards category; I think I just need a kick start on this one, because I dont know where to begin. I know ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    73

    column-major matrix Q....

    I think I just need a kick start on this one, because I dont know where to begin. I know what I have to do in theory, but pulling it off is another thing:

    Write the function vec_to_mat (int *v, int *mat, int n); v is a pointer to the beginning of an integer array (vector) with n x n elements stored in column-major representation (see below for explanation). The function should copy the elements from v to mat (which is a matrix of size n x n). From example,

    If v is {1, 2, 3, 4, 5, 6, 7, 8, 9}, the value of mat should be,

    1 4 7
    2 5 8
    3 6 9

    The values 1, 2, and 3 go down the first column of matrix mat, and then 4, 5, 6 goes down the second column and so on.

    I have written this much so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int vec_to_mat (int *v, int *mat, int n){
    int a;
    
     for(a=0, a<n, a++){
    
     }
    
    }
    
    int main()
    {
    	int a [] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    	int matrix [3][3];
    
    	vec_to_mat (a, matrix[0], 3);
    
    	for (int i = 0; i < 3; i++) {
    		for (int j = 0; j < 3; j++) {
    			printf ("%d", matrix [i][j]);
    		}
    		printf ("\n");
    	}
    
          system("PAUSE");
    }
    I know it isn't much, but I dont know where to begin on the copying of the values from *v into a matrix like that without doing it element by element.

  2. #2
    Registered User joed's Avatar
    Join Date
    Mar 2004
    Posts
    59
    Code:
    int vec_to_mat(int *v, int *mat, int n)
    {
      int x, y;
    
      for(y = 0; y < n; y++)
        for(x = 0; x < n; x++)
          mat[x + n * y] = v[y + n * x];
    }
    Note how the roles of x and y are swapped in the last line. Just treat the 2D arrays like 1D arrays for stuff like this, since they are both fundamentally the same. If you want to be slick, you can incorporate the printf into the same loop. Hope that helps.

    -Joe

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    Thank you for the response, although in many ways that was more than I needed. I cant complain though. I changed some stuff around to fit into pointer notation. The final source looks like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int vec_to_mat(int *v, int *mat, int n)
    {
      int x, y;
    
      for(y = 0; y < n; y++)
        for(x = 0; x < n; x++)
          *(mat + (x + n * y)) = *(v + (y + n * x));
    }
    
    int main()
    {
    	int a [] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    	int matrix [3][3];
    
    	vec_to_mat (a, matrix[0], 3);
    
    	for (int i = 0; i < 3; i++) {
    		for (int j = 0; j < 3; j++) {
    			printf ("%d", matrix [i][j]);
    		}
    		printf ("\n");
    	}
    
          system("PAUSE");
    }

  4. #4
    Registered User joed's Avatar
    Join Date
    Mar 2004
    Posts
    59
    It probably can be done with one loop, but the basic idea was easier to show with two. The multiplies would be slow for a large array, maybe try a version without them? Stuff to try...

    -Joe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 09:48 PM
  2. BorlandC proj
    By mary18 in forum C Programming
    Replies: 68
    Last Post: 02-20-2008, 11:22 AM
  3. Retrieving Multiple inputs with fgets?
    By Axel in forum C Programming
    Replies: 25
    Last Post: 09-13-2005, 05:04 PM
  4. Bitwise OR
    By tinkerbell20 in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2005, 03:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM

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