Author Message Please help, I don't understand

What's wrong with this program?
#include <iostream.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <iomanip.h>

int Uniform(double x);
int Expon(double x);
int Norm(double x);
const int size = 10000;
double pi = 3.141529;

double u[int i];
double a[int i];
double b[int i];
double c[int i];

int Uniform(x){
double u[i] = (x-1)/4;
u = 0;
return u[i];

Quote:
}

int Expon(x){
double a[int i] = (-ln(1-u[i]))/1.2;
return a[i];

Quote:
}

int Norm(x){

double d[int i] = (sqrt(-2*ln(u[i]))*cos(2*pi*u[i-1]);
double z[int i] = (sqrt(-2*ln(u[i]))*sin(2*pi*u[i-1]);
double b[int i] = 5 + 3*d[i];
double c[int i] = 5 + 3*z[i];
return b[i], c[i];

Quote:
}

main()
{
srand( time ( NULL ) );
for(int i = 1; i < size; i++){
double x = ((rand() % 10000)/10000) + rand() % 4 + 1;
Uniform(x);
Expon(x);
Norm(x);
for(int j = 0; j < 50; j++){
cout<<"U(a=1, b = 5) observation "<<i<<": "<<u[i]<<endl;
cout<<"Exp(lamda = 1.2) observation "<<i<<": "<<a[i]<<endl;
cout<<"N(mu = 5, o = 3) observation "<<i<<": "<<b[i]<<","<<c[i]<<endl;
return 0;

Quote:
}
}

The first try works, but after that, the output becomes 0.
--

Wed, 20 Oct 2004 06:33:15 GMT  Please help, I don't understand

Quote:
> What's wrong with this program?
<snip>
> double pi = 3.141529;

<snip>

double pi = 3.1415927;

And the rest of your program seems to be strange mix of C++ and imaginary-C.
Half the lines in it are meaningless, and couldn't possibly compile.

You need to write a simpler example.

Then you need to post it, and describe what problem you're encountering, for
instance compiler error messages that you don't understand.

I would suggest that you learn some basic C syntax before you venture into
floating point arithmetic.

/peter
--

Wed, 20 Oct 2004 14:40:32 GMT  Please help, I don't understand

Quote:
>What's wrong with this program?
>#include <iostream.h>

it's c++.  perhaps comp.lang.c++ or clc++.moderated would be better.

--
bringing you boring signatures for 17 years
--

Thu, 21 Oct 2004 22:32:11 GMT  Please help, I don't understand

| What's wrong with this program?

Don't know, what is it supposed to do? Not compile? At least it's invalid
standard C code.

<snip>
--

Thu, 21 Oct 2004 22:32:22 GMT  Please help, I don't understand

Quote:
>The first try works, but after that, the output becomes 0.

How? It does not and cannot compile as either C or C++. In fact I do not
know of any language that would compile the code as it stands. Java
won't, python won't, Objective C won't. I confess that I am not familiar
enough with C# to know if it can tolerate your weird syntax.

--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--

Thu, 21 Oct 2004 22:32:30 GMT  Please help, I don't understand

Quote:

> double u[int i];

When you define an array, its size must be an expression.
"int i" is not an expression.

Quote:
> The first try works, but after that, the output becomes 0.

I don't think the program should have compiled at all.
What compiler did you use?
--

Thu, 21 Oct 2004 22:33:11 GMT  Please help, I don't understand

Quote:
> What's wrong with this program?
> #include <iostream.h>

This means it is old C++ code, not C.

During the standaridisation process for C++, iostream.h was an early version
of
the header file for I/O streams.  Later in the standardisation process, that
was replaced by a standard header called <iostream>.  I believe there is no
formal requirement for a modern compiler to support <iostream.h> but
some may to ensure backward compatibility.

Quote:
> #include <math.h>
> #include <time.h>
> #include <stdlib.h>
> #include <iomanip.h>

> int Uniform(double x);
> int Expon(double x);
> int Norm(double x);
> const int size = 10000;
> double pi = 3.141529;

> double u[int i];
> double a[int i];
> double b[int i];
> double c[int i];

There four lines should not compile in C or C++.  They are invalid
declarations.

Quote:

> int Uniform(x){
> double u[i] = (x-1)/4;

This should also not compile unless i has been declared or #define'd
previously (eg in one of the headers you've #include'd).  I'd suggest
that occurrence is unlikely.

Similar comments apply to large parts of the code below.

Quote:
> u = 0;
> return u[i];
> }

> int Expon(x){
>     double a[int i] = (-ln(1-u[i]))/1.2;
>     return a[i];
> }

> int Norm(x){

>     double d[int i] = (sqrt(-2*ln(u[i]))*cos(2*pi*u[i-1]);
>     double z[int i] = (sqrt(-2*ln(u[i]))*sin(2*pi*u[i-1]);
>     double b[int i] = 5 + 3*d[i];
>     double c[int i] = 5 + 3*z[i];
>     return b[i], c[i];
> }

> main()
> {
> srand( time ( NULL ) );
> for(int i = 1; i < size; i++){
> double x = ((rand() % 10000)/10000) + rand() % 4 + 1;
> Uniform(x);
> Expon(x);
> Norm(x);
> for(int j = 0; j < 50; j++){
> cout<<"U(a=1, b = 5) observation "<<i<<": "<<u[i]<<endl;
> cout<<"Exp(lamda = 1.2) observation "<<i<<": "<<a[i]<<endl;
> cout<<"N(mu = 5, o = 3) observation "<<i<<": "<<b[i]<<","<<c[i]<<endl;
> return 0;
> }
> }

> The first try works, but after that, the output becomes 0.

Really?   You're either using a badly broken compiler, or one that works
with
a language that is neither C nor C++.   This code should not compile, let
alone
run.
--

Sat, 23 Oct 2004 09:42:27 GMT

 Page 1 of 1 [ 7 post ]

Relevant Pages