double not assigning value
Author 
Message 
Grayson William #1 / 4

double not assigning value
Hi, I'm writing a program to perform Gaussian reduction of a matrix to get it into reducedechelon form, and it calls the following function to choose a pivot point and arrange the rows as needed: void choosepivot(double **a, int i, int j, double N) { double pivot = 0.0; int k, ipivot; for(ipivot=i,k=i;k<N;k++) { if(fabs(a[k][j]) > pivot) { pivot = a[k][j]; printf("a[%d][%d]=%f\n", k, j, a[k][j]); printf("pivot=%d\n", pivot); ipivot = k; printf("ipivot=%d\n", ipivot); } } row_swap(a, i, ipivot); return; Quote: }
where a is the matrix itself, i is the row to start from, j is the column, and N is the smaller dimension of the matrix. I pass it an NxN+1 matrix, for example 1 2 5 3 4 6 and two values of (i, j), namely (0, 0) and (1, 1). The problem is, the line pivot = a[k][j] is not working; rather, pivot always stays at 0.0. I know it is getting to the for loop, because the printf() statements are always working. Here is the output for the program:
Value of N: 2 Matrix A: What is the value of (1, 1)? 1 What is the value of (1, 2)? 2 What is the value of (2, 1)? 3 What is the value of (2, 2)? 4 Matrix B: What is the value of (1, 1)? 5 What is the value of (2, 1)? 6 a[0][0]=1.000000 pivot=0 ipivot=0 a[1][0]=3.000000 pivot=0 ipivot=1 0 0 0 0 0 0
Anybody have any ideas? I'm completely stumped. If you need to, I can post the source for the entire program. Thanks, Grayson

Sun, 18 Apr 2004 14:45:03 GMT 


willem veenhove #2 / 4

double not assigning value
Quote:
> double pivot = 0.0; .... > pivot = a[k][j] > is not working; rather, pivot always stays at 0.0. I know it > is getting to the for loop, because the printf() statements > are always working. > printf("pivot=%d\n", pivot);
^^ Bzzt .. you're trying to print a double with a %d format string, which may have all kind of strange effects since printf expects pivot to have sizeof int, which it probably hasn't. Use %f instead, and your problem will be over ... willem

Sun, 18 Apr 2004 15:14:31 GMT 


Des Walke #3 / 4

double not assigning value
Quote: > Hi, > I'm writing a program to perform Gaussian reduction of a matrix to get it > into reducedechelon form, and it calls the following function to choose a pivot > point and arrange the rows as needed: > void choosepivot(double **a, int i, int j, double N) { > double pivot = 0.0; > int k, ipivot; > for(ipivot=i,k=i;k<N;k++) { > if(fabs(a[k][j]) > pivot) { > pivot = a[k][j]; > printf("a[%d][%d]=%f\n", k, j, a[k][j]); > printf("pivot=%d\n", pivot);
This is probably the cause of your problem. The %d format specifier doesn't match the type of pivot. Try %f as you did in the previous printf statement  but I guess you've already worked that out :) Regards Des Walker

Sun, 18 Apr 2004 15:18:17 GMT 


Grayson William #4 / 4

double not assigning value
Quote:
>> Hi, >> I'm writing a program to perform Gaussian reduction of a matrix to get > it >> into reducedechelon form, and it calls the following function to > choose a pivot >> point and arrange the rows as needed: >> void choosepivot(double **a, int i, int j, double N) { >> double pivot = 0.0; >> int k, ipivot; >> for(ipivot=i,k=i;k<N;k++) { >> if(fabs(a[k][j]) > pivot) { >> pivot = a[k][j]; >> printf("a[%d][%d]=%f\n", k, j, a[k][j]); printf("pivot=%d\n", >> pivot); > This is probably the cause of your problem. The %d format specifier > doesn't match the type of pivot. Try %f as you did in the previous > printf statement  but I guess you've already worked that out :) > Regards > Des Walker
Doh! I thought I had gotten rid of all of those when I changed int **a to double **a. Oh well, shows the value of another pair of eyes. I feel really dumb now.. :/ Thanks to both of you who responded. Grayson

Sun, 18 Apr 2004 15:23:19 GMT 


