Thread: Scanf confusion, 2 dimensional array modification

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    103

    Scanf confusion, 2 dimensional array modification

    Hi,
    I have two questions:
    1) what is the difference between
    Code:
    scanf("%c",&a);
    and
    Code:
    scanf(" %c",&a);
    Note: a is previously declared char. and there is a space right before %c in the second statement.
    The reason I asked is that while implementing a program, the first statement wasn't executed whereas the second was.

    2)
    How can you modify a 2 dimensional array, which represent a matrix, using a function that accepts only an int, specifically an element of this matrix.
    The function should return an array which is a sub matrix with row and column containing the element sent to this function deleted.
    I've thought of using a pointer function, but how I can know the size of the array, and before that, how can I know the other elements of the array.

    Any kind of help would be much appreciated.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. They were both executed. They just do different things. A space in a scanf format always means "skip any and all whitespace characters here" (so spaces, enter keys, tabs). The first will read a space into a, if one is present; the second will skip the space and read the next character.
    2. You don't. Also, functions can't return arrays.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    My C book states that I should use a function to create the submatrix. Yet, this function should be sth like finding_submatrix(a[i][j]). and the book refers to this function later and uses it as a parameter to another function that accepts an array.
    Are you suggesting that this can't be done??
    PS: I'm avoiding global variables so far.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It can return a pointer. Are they expecting you to malloc some int*, and then malloc some ints after that? (I.e., you can "fake" a 2-D array with an int**, and int** is returnable.)

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    My C book states that I should use a function to create the submatrix. Yet, this function should be sth like finding_submatrix(a[i][j]). and the book refers to this function later and uses it as a parameter to another function that accepts an array.
    Are you suggesting that this can't be done??
    PS: I'm avoiding global variables so far.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    No, they are not expecting me to use malloc, I've thought of using it though, but how can you copy the elements of the first matrix to the new sub matrix without sending it to the function...

    Sorry for the last double-posting..

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're going to have to post the exact question. I can say that int funcname(int bob) is completely unable to modify bob, although it can return any new value if it has to. And returning a pointer to a 3x4 matrix inside a 5x5 matrix can't be done without making a new matrix, since you're going to have to skip elements.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Ok.
    We need to implement a recursive function that calculate the determinant of a matrix a using the recursive formula:
    the base case: if a 1*1 matrix(x) then det(a) = x
    the recursive formula: choose any row or column, for each a[i][j] in this row or column form the product:
    power(-1,i+j) * a[i][j] * det(minor(a[i][j]))
    det(a) = sum of this products.

    Write a program that reads a, prints a in a matrix form and prints value od det(a). where det is the function that computes the determinant of a matrix.

    Except this problem of minor function, everything else is straightforward.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's the recursive formula, yes, but that's not the implementation (at least that's not the C implementation). C has no way of getting the size of an array that's passed into a function, so you wouldn't even know how to stop. For that matter, the second dimension of an array passed into a function must be a fixed size, so you couldn't use the same det function on 3x3 as you would on 4x4.

    You'll have to pass the size into the function, as well, and you'll have to build the minors (actually, the original too) into malloced memory.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Ok, That was the confirmation I needed. Thank you very much.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    So I've tried to implement the function that gets the minor (The submatrix). Here is the code:

    Code:
    int **minor(int **matrix, int order, int element){
                                                      int **submatrix = (int **)malloc(sizeof(int) * (order - 1)); /*allocating memory for the submatrix*/
                                                      int row = -1, column = -1,exit_loop = 0;
                                                      for (int i = 0 ; i < order ; i++){  /*Getting the exact position of the element*/
                                                          for (int j = 0 ; j < order ; j++)
                                                              if (element == matrix[i][j]){
                                                                           row = i;
                                                                           column = j;
                                                                           exit_loop = 1;
                                                                           break;
                                                                           }
                                                          if(exit_loop)
                                                                       break;
                                                      }
                                                      int row2 = 0;
                                                      int column2 = 0;
                                                      while(row2 != order - 1){   /*Copying the original matrix to the sub matrix (minor)*/
                                                                    while(column2 != order - 1){
                                                                                  if(row2 != row && column2 != column)
                                                                                          submatrix[row2][column2] = matrix[row2][column2];
                                                                                  column2++;
                                                                          }
                                                                          row2++;
                                                                 }
                                                      return submatrix;
        }
    Where order is the size of matrix (matrix[order][order]).
    the prototype of the function is int **minor(int**,int,int)
    why this call doesn't work??
    Code:
    int **x = minor(matrix, order, 3);
    it gives me the following error:
    Code:
    cannot convert `int (*)[((unsigned int)((int)order))]' to `int**' for argument `1' to `int** minor(int**, int, int)'
    PS: pointer function is something we didn't cover in class yet, so what you see is completely and exclusively the result of my "pointer" logic.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should search here for "malloc 2D array" or similar; you're going to need more than one malloc call.

    And int[][] and int** are not compatible types. I don't know how you managed to get all that stuff inside the [] there, but that's what it comes down to, I think.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    I agree with you on the fact that malloc wasn't doing its job, but I think I can keep one malloc with just one modification.
    Code:
    int **submatrix = (int **)malloc(sizeof(int) * (order - 1)*(order - 1));
    if there is not compatibility like you said. how can I make use of what the function returns?
    PS: never mind what the function is doing right now, it is a 10 minute implementation, i know it has some bugs that I'll fix later.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You'd like to think so, but you'd be wrong. That allocates enough room for the ints, but it will not allow you to use [double][subscripting]. One array index would be all you get.

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Ok. So how can I malloc the memory for the 2 demensional array. and how can I make use of what the funcion returns??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How To pass 2 dimensional array of strings to a function
    By chottachatri in forum C Programming
    Replies: 15
    Last Post: 01-25-2008, 02:20 PM
  2. Replies: 1
    Last Post: 04-25-2006, 12:14 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM