Function help

This is a discussion on Function help within the C Programming forums, part of the General Programming Boards category; Here is my code: Code: #include <stdio.h> #define count 6 int transpose (int row[], int column[], int number[][6]) { number[row[count]][column[count]] ...

  1. #1
    Registered User dbyte's Avatar
    Join Date
    Jul 2003
    Posts
    15

    Question Function help

    Here is my code:
    Code:
    #include <stdio.h>
    #define count 6
    
    int transpose (int row[], int column[], int number[][6])
    {
        number[row[count]][column[count]] = number[column[count]][row[count]];
    
        return (number);
    }
    
    
    void matrix_M (int row[], int column[], int number[][6])
    {
        for (row[count] = 1; row[count] <= 4; ++row[count])
        {
            for (column[count] = 1; column[count] <= 5; ++column[count])
            {
                printf ("%i\t", number[row[count]][column[count]]);
            }
            printf ("\n");
        } 
    }
    
    void matrix_N (int row[], int column[], int number[][6])
    {
        for (row[count] = 1; row[count] <= 5; ++row[count])
        {
            for (column[count] = 1; column[count] <= 4; ++column[count])
            {
                transpose (row, column, number);
                printf ("%i\t", number[row[count]][column[count]]);
            }
            printf ("\n");
        } 
    }
    
    main ()
    {
        /*Variable declaration*/
        int number[5][6] = {
                                {0,0,0,0,0,0},
                                {0,1,2,3,4,5},
                                {0,6,7,8,9,10},
                                {0,11,12,13,14,15},
                                {0,16,17,18,19,20}
                           };
        int row[count];
        int column[count];
    
        /*Execute functions*/
        matrix_M (row, column, number);
        printf ("\n");
        matrix_N (row, column, number);
    
        system("pause");
    }
    I'm supposed to be displaying a sequential number matrix, first as a 4x5 (matrix_M) & then as a 5x4 (matrix_N). I also must transpose the numbers, so that the output looks like this:

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

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

    My problem is that some of matrix_N is displaying incorrectly. Here is how it looks:

    1 6 11 16
    6 7 12 17
    11 12 13 18
    16 17 18 19
    5 10 15 20

    What/where is the problem? Is it in the transpose function or the matrix_N function?

    Thanks in advance for any help...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    For starters, it's a very odd way to handle it. The whole idea of using a larger array to fake a smaller is just .. odd.

    Next, why are you leaving the first row and column zero-filled?

    Third, you're copying a size 4 array over top of a size 5, and then also the reverse. See the problem? You have no room to add the 5th element in your size 4 array.

    You should be doing something like:
    Code:
    1,2,3,4,5,0,
    9,3,4,5,6,0,
    8,4,5,6,7,0,
    7,5,6,7,8,0,
    0,0,0,0,0,0,
    0,0,0,0,0,0
    This provides room for your array accurately. Now visualize transposing:
    Code:
    1,9,8,7,0,0       <------ see
    2,3,4,5,6,0,
    3,4,5,6,7,0,
    4,5,6,7,8,0,
    5,0,0,0,0,0,
    0,0,0,0,0,0
    
    ^
    |____ see
    Work from that. Start at zero zero as your array indexes, and work up until you are at "this_row_size -1" and "this_column_size -1".

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

  3. #3
    Registered User dbyte's Avatar
    Join Date
    Jul 2003
    Posts
    15
    The first row & column are zero-filled to prevent me from having to include "- 1" in every instance of the array loops. Hence the larger arrays for a smaller set of values. Probably not the cleanest method I agree. I don't understand your comments about 'copying' arrays over one another though. The actual values of number[][] are never changed; only the order in which they are displayed, right?

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    dbyte,
    The problem isn't with all those zeros.
    The problem is the fact that a[x][z] is changed and get the value of a[z][x]. Wheras after, a[z][x] should be changed with a[x][z] which is its initiall value! So it won't change. So you have to save the value of a[x][z] before it was changed and then give his initiall value back.
    Also fuction transpore should be void
    Code:
    #include <stdio.h>
    #define count 6
    
    void transpose (int row[], int column[], int number[][6])
    {
    		number[row[count]][column[count]] = number[column[count]][row[count]];
    }
    
    
    void matrix_M (int row[], int column[], int number[][6])
    {
    	 for (row[count] = 1; row[count] <= 4; ++row[count])
        {
            for (column[count] = 1; column[count] <= 5; ++column[count])
            {
    				printf ("%d\t", number[row[count]][column[count]]);
    		  }
    		  printf ("\n");
    	 }
    }
    
    void matrix_N (int row[], int column[], int number[][6])
    {
    	 int temporary;
    
    	 for (row[count] = 1; row[count] <= 5; ++row[count])
        {
    		  for (column[count] = 1; column[count] <= 4; ++column[count])
    		  {
    				temporary = number[row[count]][column[count]];
    				transpose (row, column, number);
    				printf ("%d\t", number[row[count]][column[count]]);
    				number[row[count]][column[count]] = temporary;
    		  }
            printf ("\n");
        } 
    }
    
    int main ()
    {
    	 /*Variable declaration*/
    	 int number[5][6] = {
    							{0,0,0,0,0,0},
    									 {0,1,2,3,4,5},
                                {0,6,7,8,9,10},
                                {0,11,12,13,14,15},
    									 {0,16,17,18,19,20}
    							  };
        int row[count];
    	 int column[count];
    
        /*Execute functions*/
    	 matrix_M (row, column, number);
    	 printf ("\n");
    	 matrix_N (row, column, number);
    
    	 return 0;
    }

  5. #5
    Registered User dbyte's Avatar
    Join Date
    Jul 2003
    Posts
    15
    Money?,
    Thanks for your help. I made the changes you indicated & everything works as it should. A question though: why is the transpose function supposed to be set to void? Isn't it returning the new value of number[][] to the matrix_N function? Again, it works just like you said it would; I am just trying to understand the logic behind the code.

    Thanks again for your help.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    First of all, the code you wrote can't compile about that function for 2 reasons.
    1) The function protype says that you will return a mere variable of type int, wheras you return the whole 2d array which is of type int (*)[].
    2) Even if that function returned the right value, you wroght
    >transpose (row, column, number);
    which doesn't expect anything. It could work if you wrote something like that: or something
    >number = transpose (row, column, number);
    As you see the number will now get the value returned by the function, wheras previously nothing got the return value of that function.

    Now about the reason for being void. Since you past an array in that function, in fact you past a pointer in the function. So it is a call by-reference and so the changes of that function to the pointer will exist for the whole program. So you don't need to return anything. When you past pointers to functions, whatever happens to that pointer will exist for the whole programm.
    Wheras, when you past integers or characters or whatever which isn't a pointer, the changes which will be made from the function to those variables won't exist for the whole programm, since you past them call by value ( you make a copy of that variable to that function ) and whatever happens to them will only exist in the particular fanction ( and so you will return them back ).

    P.S: sorry for bad English

  7. #7
    Registered User dbyte's Avatar
    Join Date
    Jul 2003
    Posts
    15
    Got it - thanks for the clarification.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

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