2d array as function parameter problem, help appreciated.

This is a discussion on 2d array as function parameter problem, help appreciated. within the C Programming forums, part of the General Programming Boards category; I have been tinkering with this for a few hours and even with any examples can not find a reason ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    41

    2d array as function parameter problem, help appreciated.

    I have been tinkering with this for a few hours and even with any examples can not find a reason for the Seg fault I'm getting, The code is all the way at the bottom of the post.

    I am still new to C after a long python stint. I am trying to implement a jacobian algorithm to find the solution to a system of equations.

    the header of the Jacobi dunction is commented right now to only take the array, and the length(it is assumed to be square)

    the program dies on a segfault, running gdb gave me that it dies right at

    Code:
    aInv[xCounter][xCounter] = 1.0/A[xCounter][xCounter];
    when i started with this program I hardcoded A right into the function and it went through no problems so this must be with the way im taking in and handling A.

    Im sure the solution isnt terribly difficult but i've exhausted any similar examples I could find and hopefully someone here can offer guidance.


    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    
    int Jacobi(double **A/*,double *b*/,int n/*,double *xo,double tol,int MaxIt*/){
        /*
        USAGE:its = Jacobi(A,b,n,xo,tol,MaxIt);
        INPUTS:A     coefficient matrix for linear system
        b       right-hand side vector
        n       number of equations
        xo      vector containing initial guess
        tol     error tolerance-between successive approximations
        MaxIt   maximum number of iterations
        OUTPUT:
        its     No.of iterations performed(return-1 if max exceeded)
        xo      approximate solution
        */
        breakpt();
        int iterCount = 0;
        int i = 0;
        int j = 0;
        double tempDouble = 0;
        /*First we find A^-1 */
        double aInv[n][n];
        int xCounter = 0;
        int yCounter = 0;
        double lower[n][n];
        double upper[n][n];
        while(yCounter < n){
            xCounter = 0;
            while(xCounter < n){
                if(yCounter == xCounter){
                    printf("%f - a number",&A[xCounter][xCounter]);
                    
                    
                    aInv[xCounter][xCounter] = 1.0/A[xCounter][xCounter];
                    lower[xCounter][xCounter] = 0;
                    upper[xCounter][xCounter] = 0;
                }
                else{
                    aInv[xCounter][yCounter] = 0;
                    if(xCounter < yCounter){
                        lower[xCounter][yCounter] = A[xCounter][yCounter];
                        upper[xCounter][yCounter] = 0;
                    }
                    else{
                        upper[xCounter][yCounter] = A[xCounter][yCounter];
                        lower[xCounter][yCounter] = 0;
                    }
                }
                xCounter++;
                
            }
            yCounter++;
        }
        
        xCounter = 0;
        
        PrintArray(aInv,2,"inverse");
        PrintArray(lower,2,"lower");
        PrintArray(upper,2,"upper");
       /* while (counter < MaxIt){
        
    }*/
    
    }
    
    int main(int argc, char *argv[]){
        
        printf("GOT HERE");
        double a[2][2] = {2,3,5,7};
        
        Jacobi(&a,2);
    
    
    }
    /*Prints a 2d square array of double, for debugging purposes*/
    void PrintArray(double** Arr, int size, char* name){
    int counterX = 0;
    int counterY = 0;
    printf("Printing %s Array...",name);
        while(counterY < size){
            while(counterX < size){
                printf("%f",Arr[counterX][counterY]);
                counterX++;
            }
            printf("\n");
            counterY++;
        
        }
    
    }
    int breakpt(){
    return 1;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    If you're starting with double a[2][2] as your input variable, then the prototype for the function can(*) also be

    int Jacobi ( double a[2][2], int MaxIt );

    Likewise, with the function definition, and you just use normal array subscripting inside the function as if you were writing the same code in main itself.

    And the call would simply be
    Jacobi( a, 2 );

    (*)
    The declaration can also be expressed like this as well, but it's more typing.
    int Jacobi ( double a[][2], int MaxIt );
    int Jacobi ( double (*a)[2], int MaxIt );
    The second form shows the true type of the pointer, which is a "pointer to an array of two doubles".

    This is wrong for a true 2D array...
    int Jacobi ( double **a, int MaxIt );
    Where a is now a "pointer to a pointer to a double".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    41
    Quote Originally Posted by Salem View Post
    If you're starting with double a[2][2] as your input variable, then the prototype for the function can(*) also be

    int Jacobi ( double a[2][2], int MaxIt );
    Right I agree with you and thats where the majority of examples online go to. I think my code was a little misleading because of its infantile state. eventually the function should be able to take in an array of any size that will be dictated by a user's input.

    in my case the array in the parameter must be able to be of any size. I'm still stuck on that part.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    41
    So, After some tinkering I figured out that if i do my function decleration like this:

    int Jacobi(int n,double A[n][n]){

    and declare the size in the array, it seems to not seg fault there. my question becomes, is there a different better way to do this?
    Last edited by somekid413; 01-17-2010 at 03:10 PM.

  5. #5
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    979
    Code:
    int Jacobi(int n,double A[n][n])
    This is OK according to the more recent C standard, but not all compilers will support it. If you don't care about portability, this should be acceptable. If you do care, you should ditch arrays and switch to pointers from the outset.

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. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  3. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  4. 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
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

Tags for this Thread


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