bkasterm

August 15th, 2005, 06:12 PM

I am trying to write a progam that in the end will use binomial coefficients of large numbers. I have used analogous code to the below with all types changed to integers, and no problems (but can't make MAX below very big then as the numbers will grow too large for the int type). With this code there also are no problems as long as MAX is not big (900 here appears to be big !?!?).

I am neither an experienced programming nor experienced at using gdb. I always thought I had some understanding, but this is thoroughly confusing me.

This is the gdb output:

************************* begin output ******************

Program received signal SIGSEGV, Segmentation fault.

main () at chooseTry2.c:13

13 int n = 5;

************************* end output *********************

The progam in total is (it is a bit messy because I have been

working with it a while, think it is readable enough though):

**************************begin program *********************

#include <stdio.h>

#include <gmp.h>

#define MAX 900

// maximum index is one less than this

int main()

{

mpz_t choose[MAX][MAX]; // contains the binomials, note

// choose[i][j] only used if j <= i

{ // setting the choose array.

int n = 5;

int r = 9;

for (n= 0; n < MAX; n++)

{

for ( r = 0 ; r <= n; r++)

{

if ( r == 0 || r == n)

{

mpz_init(choose[n][r]);

mpz_set_ui(choose[n][r],1);

}

else

{

mpz_init(choose[n][r]);

mpz_add(choose[n][r],choose[n-1][r-1],choose[n-1][r]);

}

}

}

}

{ // testing small outputs

int n,r;

for (n= 0; n < 10; n++)

{

for ( r = 0 ; r <= n; r++)

{

printf("n = %i", n);

printf(" r = %i ", r);

mpz_out_str( stdout, 10, choose[n][r] );

printf("\n");

}

}

mpz_out_str( stdout, 10, choose[499][200]);

}

return 0;

};

******************************end program **************************

Best,

Bart

I am neither an experienced programming nor experienced at using gdb. I always thought I had some understanding, but this is thoroughly confusing me.

This is the gdb output:

************************* begin output ******************

Program received signal SIGSEGV, Segmentation fault.

main () at chooseTry2.c:13

13 int n = 5;

************************* end output *********************

The progam in total is (it is a bit messy because I have been

working with it a while, think it is readable enough though):

**************************begin program *********************

#include <stdio.h>

#include <gmp.h>

#define MAX 900

// maximum index is one less than this

int main()

{

mpz_t choose[MAX][MAX]; // contains the binomials, note

// choose[i][j] only used if j <= i

{ // setting the choose array.

int n = 5;

int r = 9;

for (n= 0; n < MAX; n++)

{

for ( r = 0 ; r <= n; r++)

{

if ( r == 0 || r == n)

{

mpz_init(choose[n][r]);

mpz_set_ui(choose[n][r],1);

}

else

{

mpz_init(choose[n][r]);

mpz_add(choose[n][r],choose[n-1][r-1],choose[n-1][r]);

}

}

}

}

{ // testing small outputs

int n,r;

for (n= 0; n < 10; n++)

{

for ( r = 0 ; r <= n; r++)

{

printf("n = %i", n);

printf(" r = %i ", r);

mpz_out_str( stdout, 10, choose[n][r] );

printf("\n");

}

}

mpz_out_str( stdout, 10, choose[499][200]);

}

return 0;

};

******************************end program **************************

Best,

Bart