PDA

View Full Version : Sorry to bug you guys again, but more c problems


KrazyHorse
17-02-2004, 01:36:50
#include <stdio.h>
#include <math.h>
#include "nr.h"
#include "nrutil.h"

main()

{

float *x, *y, *X, *Y;
float a, s0, M, g, w0;
float **s, **w;
float *Dx, *Dy;
float **f, **F;
float *I;
int i;
int j;
int N = 11;

x = vector (0, N-1);
y = vector (0, N-1);
X = vector (0, N-1);
Y = vector (0, N-1);
w = matrix (0, N-1, 0, N-1);
s = matrix (0, N-1, 0, N-1);
Dx = vector (0, N-1);
Dy = vector (0, N-1);
f = matrix (0, N-1, 0, N-1);
F = matrix (0, N-1, 0, N-1);
I = vector (0, N-1);

a = 0.054;
s0 = 8328.4;
M = 91.187;
g = 2.494;
w0 = 41.54;


for(i = 0; i < N; i++)
x[i] = i / 2;

for (j = 0; j < N; j++)
y[j] = j;

for (i = 0; i < N; i++)
X[i] = a * pow((1-x[i]), (a-1));

for (j = 0; j < N; j++)
Y[j] = a * pow((1-y[j]), (a-1));

for(i = 0; i < N; i++)
{for(j = 0; j < N; j++)
s[i][j] = x[i] * y[j] * s0;
}

for(i = 0; i < N; i++)
Dx[i] = a * pow((1 - x[i]), (a-1));

for(j = 0; j < N; j++)
Dy[j] = a * pow((1 - y[j]), (a-1));

for(i = 0; i < N; i++)
{for(j = 0; j < N; j++)
w[i][j] = w0 * s[i][j] * g * g / ( ( s[i][j] - M * M ) * ( s[i][j] - M * M ) + s[i][j] * s[i][j] * g * g / ( M * M));
}

for(i = 0; i < N; i++)
{for(j = 0; j < N; j++)
f[i][j] = Dx[i] * Dy[j] * w[i][j];
}

/* for(i = 0; i < N; i++) */
/* {for(j = 0; j < N; j++) */
/* f[i][j] = Dx[i] * Dy[j]; */
/* } */

F[0][j] = 0.0;

for(i = 2; i < N; i = i + 2)
{for(j = 0; j < N; j++)
F[i][j] = F[i-2][j] + (f[i-2][j] / 3 + 4 * f[i-1][j] / 3 + f[i][j] / 3) / (N-1);
}

I[0] = 0.0;

for(j = 2; j < N; j = j + 2)
I[j] = I[j-2] + (F[N-1][j-2] / 3 + 4 * F[N-1][j-1] / 3 + F[N-1][j] / 3) / (N-1);


/* for(i = 0; i < N; i= i + 2) */
printf("%18.15f\n", x[5]);

}

Now, most of this is irrelevant right now (it's supposed to be 2-d integration of some shit function via simpson's rule, but no matter)

I was getting a 0 for my integration, so I decided to do some checking.

The relevant part is

float *x, *y, *X, *Y;

int i;
int j;
int N = 11;

x = vector (0, N-1);
y = vector (0, N-1);

for(i = 0; i < N; i++)
x[i] = i / 2;

printf("%18.15f\n", x[5]);

I tell it to print 5/2 and it gives me 2.0000000

I defined x[i] as float; what's the problem?

TV4Fun
17-02-2004, 01:41:55
You defined *x as a float. you need to define x as an array with a specific number of variables.

KrazyHorse
17-02-2004, 01:42:39
Don't understand. Please explain.

KrazyHorse
17-02-2004, 01:43:33
This same procedure worked last time for different problem (writing float *b and then writing b[i] = some shit)

KrazyHorse
17-02-2004, 01:46:18
Just wrote this :

float m;

m = 1/15;

printf("%18.15f\n", m);

got back 0.000000000000000

KrazyHorse
17-02-2004, 01:49:25
If I write 1.000000000/15.000000 though I get the right answer.

Is it possible this is my problem?

TV4Fun
17-02-2004, 01:50:34
It might work if *b is the last variable you define, only because there'll be nothing after it in memory. The proper way do declare an array though, is to declare it as
float x[11];

or however many elements you want to have. It would be better for your program to include the statement
#define N 11

after your includes and then define all your arrays with
float x[N], y[N], X[N], Y[N];

as this will create set arrays going from 0 to N-1.

TV4Fun
17-02-2004, 01:52:16
Also in order for your division of constants to work you need to specify them as floats in the program. The proper way to do this would be to write them as 1.0f/15.0f as you don't need more than one 0 and omitting the f will make the compiler think that they are doubles.

KrazyHorse
17-02-2004, 01:53:12
Ah.

But I was told that if I defined x = vector(0, N-1) after writing float *x then this would work (it's possible that the vector notation is a special thing in my numerical recipes library)

TV4Fun
17-02-2004, 01:56:54
I have not seen the vector function, I could not tell you how it works, but from what you're doing it looks like you should just define your variables as arrays and leave out the vectors.

KrazyHorse
17-02-2004, 02:00:26
Originally posted by TV4Fun
Also in order for your division of constants to work you need to specify them as floats in the program. The proper way to do this would be to write them as 1.0f/15.0f as you don't need more than one 0 and omitting the f will make the compiler think that they are doubles.

Just figured something out; to divide my indices I need to create new floats then define these floats to be equal to my indices

i.e.

int i, j;
int N = 11;

float r, s, t;

r= i
s= j
t = N

This seems to work

TV4Fun
17-02-2004, 02:05:21
You could just cast your indices as floats. You would do that by writing static_cast<float>(i) in place of i in youe calculations.

KrazyHorse
17-02-2004, 02:06:01
Okay. Sounds good, because my workaround isn't working.

TV4Fun
17-02-2004, 02:06:58
So the proper way to write that would be
x[i] = static_cast<float>(i) / 2.0f;

TV4Fun
17-02-2004, 02:08:03
And rememver to use the proper indentation. It makes your code much more readable.

KrazyHorse
17-02-2004, 02:08:19
What library do I need to include? static_cast gives me an undeclared error

KrazyHorse
17-02-2004, 02:08:53
Originally posted by TV4Fun
And rememver to use the proper indentation. It makes your code much more readable.

I am here, but the code doesn't post properly on CG

TV4Fun
17-02-2004, 02:09:35
Sorry, static_cast is from c++, I believe for c it is float(i).

TV4Fun
17-02-2004, 02:10:13
Originally posted by KrazyHorse
I am here, but the code doesn't post properly on CG Put it inside [code] tags.

KrazyHorse
17-02-2004, 02:12:49
parse error before float

KrazyHorse
17-02-2004, 02:14:21
Originally posted by TV4Fun
Put it inside [code] tags.

Corrected

TV4Fun
17-02-2004, 02:14:25
It might be (float) i

KrazyHorse
17-02-2004, 02:15:34
Brilliant. :beer:

Thanks TV

TV4Fun
17-02-2004, 02:16:17
No problem. I love this kind of stuff.

KrazyHorse
17-02-2004, 02:18:36
It's outputting a number. I have no idea if it's the right number or not, but it's better than 0. :beer:

KrazyHorse
17-02-2004, 02:22:52
giving me 1.5 * 10 ^ (-3)

KrazyHorse
17-02-2004, 02:26:26
Wow. When I increase step number to N = 400 that answer jumps to 0.400

It is a very steeply varying function of x (there is an integrable singularity at x = 1 and y = 1)

KrazyHorse
17-02-2004, 02:30:16
Hah. When I increased step number to 4001 (increasing total evaluated points steps to somewhere around 16 million) the value goes to 1.416

And the computer takes almost a full minute to run the program.

TV4Fun
17-02-2004, 02:43:50
It would help if I knew what your program was supposed to do.

KrazyHorse
17-02-2004, 03:01:03
It integrates the function D(x1)D(x2)w(s) from x1 = 0 to 1 and x2 = 0 to 1

s = x1 * x2 * s0
D(x) = a / (1-x)^(1-a)
w(s) = w0 * g ^2 / ((s^2 - M^2)^2 + s^2 * g^2 / M^2)

s0, w0, a, g and M are constants

This is a cross-section calculation for the e+e- -> Z process

KrazyHorse
17-02-2004, 03:01:56
Next I get to do it via Monte Carlo integration.


Wheeeeee

Sir Penguin
17-02-2004, 03:18:23
One big problem is your variables need more descriptive names.

SP

TV4Fun
17-02-2004, 03:23:01
That would help.

KrazyHorse@home
17-02-2004, 04:02:15
my variables need to be as quick to type as possible

TV4Fun
17-02-2004, 04:04:11
Try find and replace.

KrazyHorse@home
17-02-2004, 04:06:36
find and replace doesn't work on xemacs

:p

TV4Fun
17-02-2004, 04:07:35
Try an IDE.

TV4Fun
17-02-2004, 04:08:00
Try sed.

KrazyHorse@home
17-02-2004, 04:11:15
I'll keep using what I "know" how to use.

Sir Penguin
17-02-2004, 04:21:52
Then don't expect people who don't know what your variables mean to be able to help you. :)

SP

TV4Fun
17-02-2004, 04:23:21
Originally posted by KrazyHorse
It integrates the function D(x1)D(x2)w(s) from x1 = 0 to 1 and x2 = 0 to 1

s = x1 * x2 * s0
D(x) = a / (1-x)^(1-a)
w(s) = w0 * g ^2 / ((s^2 - M^2)^2 + s^2 * g^2 / M^2)

s0, w0, a, g and M are constants

This is a cross-section calculation for the e+e- -> Z process I must get around to taking calculus so I know what the hell you're talking about.

Sir Penguin
17-02-2004, 04:32:25
Can anyone tell me how to get a plain C project with Visual C++ in Visual Studio .NET 2k3 Pro?

SP

KrazyHorse@home
17-02-2004, 04:52:43
Originally posted by Sir Penguin
Then don't expect people who don't know what your variables mean to be able to help you. :)

SP

I can generally narrow down the problem to something I can put into cogent question form.

JM^3
17-02-2004, 04:54:33
use fortran, is a lot easier for what you are doing

Jon Miller

KrazyHorse@home
17-02-2004, 04:59:09
The other students are using C.

That means I use C too.

Plus I've now got this mass of computer knowledge *snigger* that I don't want to go to waste.

2 assignments down, 10 to go.

JM^3
17-02-2004, 05:01:57
well, you are less likely to screw up in fortran

all I am saying

it is much more intuitive

Jon Miller

TV4Fun
17-02-2004, 18:08:25
Originally posted by Sir Penguin
Can anyone tell me how to get a plain C project with Visual C++ in Visual Studio .NET 2k3 Pro?

SP New Project|VC++ project|Makefile project

QtFLW@Work
17-02-2004, 21:11:20
10 PRINT "HELLO WORLD"
20 GOTO 10

TV4Fun
17-02-2004, 21:16:58
int main()
{
while (true)
printf("Hello World!");

return 0;
}

Sir Penguin
17-02-2004, 21:34:02
Originally posted by TV4Fun
New Project|VC++ project|Makefile project
Well, it gave me a different error ("Error spawning ''."). I wonder if it has to do with the fact that I don't have a makefile. :confused:

This is stupid. Maybe I'll just learn C# instead.

SP

TV4Fun
17-02-2004, 21:39:43
Start a new project. Go to project|Add existing item. Find your source file.

TV4Fun
17-02-2004, 21:42:11
Oh right.

Sorry,

GEEK FORUM!!

JM^3
17-02-2004, 21:46:41
wrong place

only geeks go here

I Am scared, I think I will ealce

JOn Miller

Sir Penguin
17-02-2004, 21:48:10
Originally posted by TV4Fun
Start a new project. Go to project|Add existing item. Find your source file.
I've done that. The F5 key, which normally builds and debugs, is set just to debugging and says that it can't find (projectname).exe. The build option is mapped to CTRL-SHIFT-B, and gives the error spawning error.

SP

TV4Fun
17-02-2004, 22:59:50
What is your project meant to be? Win32? Console? What exactly does it need to do?

Sir Penguin
17-02-2004, 23:47:28
Well, it's supposed to help me figure out how to work on a C program in VS .NET 2K3. :)

This is the code:
/* Neil MacMillan
* Playing with C
*/

#include <stdio.h>
#include <string.h>

char first[100]; // holds the first name
char last[100]; // holds the last name
char full[200]; // holds the full name

int main()
{
// Get names from input
printf("First name: ");
fgets(first,sizeof(first),stdin);
printf("Last name: ");
fgets(last,sizeof(last),stdin);

// Put them together
first[strlen(first)-1] = '\00';
strncpy(full,first,sizeof(first));
strncat(full," ",1);
strncat(full,last,sizeof(last));

// Output
printf("Full name is %s",full);
return(0);
}

Clearly, it's supposed to end up as just a console program. It's almost exactly the same as an example from Practical C Programming, except they used strcpy and strcat. There might be some other minor differences, but regardless, it compiles fine under gcc.

SP

TV4Fun
18-02-2004, 00:05:33
New Project|VC++ Project|Win32 Project|OK|Application Settings|Console Application|Empty Project|Finish|Add your files.

Sir Penguin
18-02-2004, 00:22:38
Awesome! Thanks.

SP

TV4Fun
18-02-2004, 00:33:52
Dou itasshimashita.

KrazyHorse@home
18-02-2004, 01:54:39
Okay; who the fuck moved this?

The Management is getting uppity again...

TV4Fun
18-02-2004, 03:00:21
Nav has drowned.