Results 1 to 10 of 10
Hi,
I have a problem with this code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define PI 3.14159265358979323846264338327
void func1(double *matrix1, int N_spaces);
int main(int argc,char **argv) {
double *matrix1=NULL;
int ...
- 07-02-2009 #1Just Joined!
- Join Date
- Jul 2009
- Posts
- 7
I can't find it help!!!
Hi,
I have a problem with this code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define PI 3.14159265358979323846264338327
void func1(double *matrix1, int N_spaces);
int main(int argc,char **argv) {
double *matrix1=NULL;
int N_spaces = 16;
int i;
matrix1 = malloc((N_spaces+1)*2*sizeof(double));
func1(matrix1, N_spaces);
return 0;
}
void func1(double *matrix1, int N_spaces) {
double x,y,h;
int j,z;
h = 2/(double)N_spaces;
for(j=0,z=N_spaces+j; j<=N_spaces; j++,z++) {
x = -1+j*h;
y = sin(PI*x);
matrix1[j] = x;
matrix1[z] = y;
printf("%d %d %.7f %.7f\n", j, z, matrix1[j],matrix1[z] );
}
for(j=0,z=N_spaces+j; j<=N_spaces; j++,z++) {
printf("%d %d %.7f %.7f\n", j, z, matrix1[j],matrix1[z] );
}
}
The output is the following:
0 16 -1.0000000 -0.0000000
1 17 -0.8750000 -0.3826834
2 18 -0.7500000 -0.7071068
3 19 -0.6250000 -0.9238795
4 20 -0.5000000 -1.0000000
5 21 -0.3750000 -0.9238795
6 22 -0.2500000 -0.7071068
7 23 -0.1250000 -0.3826834
8 24 0.0000000 0.0000000
9 25 0.1250000 0.3826834
10 26 0.2500000 0.7071068
11 27 0.3750000 0.9238795
12 28 0.5000000 1.0000000
13 29 0.6250000 0.9238795
14 30 0.7500000 0.7071068
15 31 0.8750000 0.3826834
16 32 1.0000000 0.0000000
0 16 -1.0000000 1.0000000
1 17 -0.8750000 -0.3826834
2 18 -0.7500000 -0.7071068
3 19 -0.6250000 -0.9238795
4 20 -0.5000000 -1.0000000
5 21 -0.3750000 -0.9238795
6 22 -0.2500000 -0.7071068
7 23 -0.1250000 -0.3826834
8 24 0.0000000 0.0000000
9 25 0.1250000 0.3826834
10 26 0.2500000 0.7071068
11 27 0.3750000 0.9238795
12 28 0.5000000 1.0000000
13 29 0.6250000 0.9238795
14 30 0.7500000 0.7071068
15 31 0.8750000 0.3826834
16 32 1.0000000 0.0000000
As you notice the
0 16 -1.0000000 -0.0000000 and 0 16 -1.0000000 1.0000000 lines are different but they shouldnt!!!
Can you spot what I can't?
Thank you.
- 07-02-2009 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
I haven't taken the time to analyze your code, but your #define for PI is not a valid double precision floating point number, which only allow approximately 15 significant digits of precision, but you are using 30. If your system supports long doubles then you can get 30 digits, but you aren't using that here. In any case, such a number can skew your results. Whether or not it will depends upon how the compiler deals with the extra digits. In any case, computer "innumeracy" of the sort you are experiencing is usually due to the programmer not understanding how digital processors handle numeric data like this, and so end up with invalid results upon occasion.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 07-02-2009 #3Just Joined!
- Join Date
- Jul 2009
- Posts
- 7
Hi,
thanx for the reply.
I tried reducing the digits of PI but the result was the same.
- 07-06-2009 #4Banned
- Join Date
- Jun 2009
- Posts
- 68
For one thing, at the end of main() - before return 0;:
free(matrix1);
is always the proper thing to do.
For another, why is:
int i;
declared in main()?
For another:
I get "undefined reference to sin".
sin() isn't in the headers you have in this code.
Last of all, I haven't spent too long looking at this, but I don't understand exactly what you're doing. Not to sound lazy, but if you wrote a description of exactly what you're trying to do it might help.
- 07-06-2009 #5Just Joined!
- Join Date
- Jul 2009
- Posts
- 7
Hi,
thank you for the reply.
As for the free() before return I'd rather follow Theo De Raadt's advice and not to, as it is most likely to cause problems rather than solve them.
The undefined reference is due to you not passing the -lm flag to the compiler.
The program just uses a function that fills an array with values nothing more.
- 07-06-2009 #6Just Joined!
- Join Date
- Jul 2009
- Posts
- 7
Check your compiler settings, ofcourse it compiles without errors.
Tip: cc prog.c -lm
- 07-06-2009 #7Just Joined!
- Join Date
- Jul 2009
- Posts
- 7
cc is gcc

what errors do you get?
- 07-06-2009 #8Just Joined!
- Join Date
- Jul 2009
- Posts
- 7
Thank you anyway

P.S. It really doesn't matter why I do it, it matters why it doesn't work
- 07-06-2009 #9Just Joined!
- Join Date
- Jul 2009
- Posts
- 7
it can't get any simpler
- 07-06-2009 #10Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
Should your loop tests be "<=" or "<" ? ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )


Reply With Quote