Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > program isn't running properly

Reply
Thread Tools

program isn't running properly

 
 
ianweise@gmail.com
Guest
Posts: n/a
 
      03-07-2007
On Mar 7, 8:42 am, Stuart Redmann <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > This is what happens when you take people from a Mathematics and
> > Financial background, and try and make them develop C++ programs with
> > no real instruction on how to properly build them. We are definately
> > computer programming "noobs" =( and find it very frustrating. So I
> > think everything I do is gonna seem basic, strange, and chunky to
> > anyone with an extensive knowledge of C++

>
> The thing that bugs me is why you chose C++ as development tool? Having
> worked about 10 years with C++ (the last 3 of them on a daily basis), I
> would consider myself as experienced with a tendency to expert, but
> there are still a lot of things that I don't know. If you ask me I'd say
> that C++ is definitely not the best choice for beginners, as it offers
> too many features that are designed for real large scale projects but
> are only confusing to beginners. I doubt that you would have had this
> trouble if you had used other tools like Matlab or Maple (though they
> are a lot more expensive than a C++ compiler that you can get everywhere
> for free).
>
> Having a lot of global variables is not bad in general, but makes the
> code you have written next to impossible to re-use. For real small scale
> programs (say less than 500 lines of code) global variables are
> acceptable. If you want to spend more time learning C++ I'd advice you
> to re-write your code with proper functions and without any global
> variables. If you want to spend more time on the problem you have in
> hand, you should better use another tool.
>
> Regards,
> Stuart


Sure, other programs are easier to use, and more user friendly, and
nicer for beginners; i agree whole heartedly. However, its not like
using C++ is an option. We are told by our proffessor to write this
program in C++ to figure out these financial things. Monte Carlo
simulation is something I have done in the past, and used other
software that was amazingly simple, such as crystal ball, where you
just input some stuff and away you go. However, we have to sit here
and derive how to do very basic simple things that excel, matlab, etc.
can do (or even using a statistics text book!); for example, we had to
write a program that calculated the normal_cdf function. I don't know
why, its not up to me, but we have to do what our proffessors tell us
to do on the software they assign us.

ian

 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      03-07-2007
On 7 Mar, 10:15, (E-Mail Removed) wrote:
> On Mar 7, 8:42 am, Stuart Redmann <(E-Mail Removed)> wrote:
>
>
>
> > (E-Mail Removed) wrote:
> > > This is what happens when you take people from a Mathematics and
> > > Financial background, and try and make them develop C++ programs with
> > > no real instruction on how to properly build them. We are definately
> > > computer programming "noobs" =( and find it very frustrating. So I
> > > think everything I do is gonna seem basic, strange, and chunky to
> > > anyone with an extensive knowledge of C++

>
> > The thing that bugs me is why you chose C++ as development tool? Having
> > worked about 10 years with C++ (the last 3 of them on a daily basis), I
> > would consider myself as experienced with a tendency to expert, but
> > there are still a lot of things that I don't know. If you ask me I'd say
> > that C++ is definitely not the best choice for beginners, as it offers
> > too many features that are designed for real large scale projects but
> > are only confusing to beginners. I doubt that you would have had this
> > trouble if you had used other tools like Matlab or Maple (though they
> > are a lot more expensive than a C++ compiler that you can get everywhere
> > for free).

>
> > Having a lot of global variables is not bad in general, but makes the
> > code you have written next to impossible to re-use. For real small scale
> > programs (say less than 500 lines of code) global variables are
> > acceptable. If you want to spend more time learning C++ I'd advice you
> > to re-write your code with proper functions and without any global
> > variables. If you want to spend more time on the problem you have in
> > hand, you should better use another tool.

>
> > Regards,
> > Stuart

>
> Sure, other programs are easier to use, and more user friendly, and
> nicer for beginners; i agree whole heartedly. However, its not like
> using C++ is an option. We are told by our proffessor to write this
> program in C++ to figure out these financial things. Monte Carlo
> simulation is something I have done in the past, and used other
> software that was amazingly simple, such as crystal ball, where you
> just input some stuff and away you go. However, we have to sit here
> and derive how to do very basic simple things that excel, matlab, etc.
> can do (or even using a statistics text book!); for example, we had to
> write a program that calculated the normal_cdf function. I don't know
> why, its not up to me, but we have to do what our proffessors tell us
> to do on the software they assign us.


Since you have a background in math you should have no problem with
the idea of functions, so try to organize your code as a number of
functions that you call. Try to make each function depend only on its
given parameters, this should reduce the number of global variables.
Start with the basic functionalities and then build more "high-level"
functions that perform their job by calling other functions. This way
you will get code that can more easily be reused in other assignments.

--
Erik Wikström

 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      03-07-2007
Not a comment on any bugs as much as on style.

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>
>>
>> Also I was struck by how you were calling the function uniform_deviate
>> in a loop, but the way it is written it will always return the same
>> value, so what is the point of calling it repeatedly?

>
> this is my current code on that section:
>
> double uniform_deviate ()
> {
> a=1366;
> b=150889;
> N=714025;
> Vj = (a*Vj + b) % N;
> U=(double)Vj/(N-1);
> return U;
> }


Right, you're using a lot of global variables here. Now, if a, b and N are
being set by this function and not used elsewhere, why not just make them
local? (but then without looking at the rest of the program I don't know if
you're using a, b or N anywhere else, that's part of the evils of global
variables). I would first attempt to change this one function to this
(which may or may not work depending on the rest of your code)

double uniform_deviate( double& Vj )
{
int a = 1366;
int b = 150889;
int N = 714025;
Vj = ( a * Vj + b ) % N;
double U = Vj / ( N - 1 );
return U;
}

This function now does not depend on any variables that weren't passed to it
or are local to it. The only variable it changes that can be seen from
outside it are Vj, which is passed as a reference (meaning any changes in
this function will be seen by whatever calls it). Also the value that is
returned. But again, without looking at the rest of your code (which I
really didn't) I can't tell if you need a, b and N anywhere else. Or even
U. But U is being reutrned so that shouldn't matter.


> double normal_deviate ()
> {
> V1=1.0;
> V2=1.0;
> while(V1*V1+V2*V2 > 1)
> {
> V1=2*uniform_deviate()-1;
> V2=2*uniform_deviate()-1;
> }
> W=(V1*V1+V2*V2);
> Z=V1*pow((-2*log(W))/W,0.5);
> return Z;
> }
>
> important i guess to also note that in int main () Vj starts at a
> value of 1.
>
> i dont think that it calls the same number every time? i did a cout
> <<U << in the uniform deviate function, and it was a different value
> each time (because Vj keeps changing, its a very basic random number
> generator, not a good one though). And the values in normal deviate
> also appear to be changing, because i ran a cout <<Z << in there as
> well, and it was numbers from the normal curve. Now each time i run
> the program, it will give me the exact same "random" numbers i think,
> but i think thats what we are supposed to do for this one. its a bad
> random number generator mainly =P
>
> What I am currently struggling with is the calculations within the int
> main ().
>
> I was miscalculating Am, because i was simply takign the last Vt value
> and adding the last Si value from the loop.
>
> here is the relevant code:
>
> double Values[10];
> for(i=0; i<M; i++)
> {
> Si = So*exp((r-0.5*v*v)*T+v*pow(T,0.5)*normal_deviate());
> Values[i] = exp(-r*T)*MAX(K-Si,0);
>
> Vt = exp(-r*T)*MAX(K-Si,0);
> //in this space here i need to calculate a sum of all the Si's. or
> rather, need to use it in the line below. is there a sum function
> where i could say sum Values[] and have it add all the numbers stored
> in my array? because i need that sum divided by M, as seen below.
>
>
>
> }
> Am = ***need sum of numbers from array, not Vt*** Vt/M;
> for(i=0; i<M; i++)
> {
> LP=LP+pow((Values[i]-Am),2.0);
>
> }
> Bm = LP/(M-1);
> Lower = Am-(1.96*(pow(Bm,0.5))/(pow(M,0.5)));
> Upper = Am+(1.96*(pow(Bm,0.5))/(pow(M,0.5)));
>
> cout << "The mean is = " <<Am <<endl;
> cout << "The variance is = " <<Bm <<endl;
> cout << "The confidence interval is (" <<Lower << ", " <<Upper <<")
> "<<endl;
> return 0;
>



 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      03-07-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>
>>
>> Also I was struck by how you were calling the function uniform_deviate
>> in a loop, but the way it is written it will always return the same
>> value, so what is the point of calling it repeatedly?

>
> this is my current code on that section:
>
> double uniform_deviate ()
> {
> a=1366;
> b=150889;
> N=714025;
> Vj = (a*Vj + b) % N;
> U=(double)Vj/(N-1);
> return U;
> }
> double normal_deviate ()
> {
> V1=1.0;
> V2=1.0;
> while(V1*V1+V2*V2 > 1)
> {
> V1=2*uniform_deviate()-1;
> V2=2*uniform_deviate()-1;
> }
> W=(V1*V1+V2*V2);
> Z=V1*pow((-2*log(W))/W,0.5);
> return Z;
> }
>
> important i guess to also note that in int main () Vj starts at a
> value of 1.
>
> i dont think that it calls the same number every time? i did a cout
> <<U << in the uniform deviate function, and it was a different value
> each time (because Vj keeps changing, its a very basic random number
> generator, not a good one though). And the values in normal deviate
> also appear to be changing, because i ran a cout <<Z << in there as
> well, and it was numbers from the normal curve. Now each time i run
> the program, it will give me the exact same "random" numbers i think,
> but i think thats what we are supposed to do for this one. its a bad
> random number generator mainly =P
>
> What I am currently struggling with is the calculations within the int
> main ().
>
> I was miscalculating Am, because i was simply takign the last Vt value
> and adding the last Si value from the loop.
>
> here is the relevant code:
>
> double Values[10];
> for(i=0; i<M; i++)
> {
> Si = So*exp((r-0.5*v*v)*T+v*pow(T,0.5)*normal_deviate());
> Values[i] = exp(-r*T)*MAX(K-Si,0);
>
> Vt = exp(-r*T)*MAX(K-Si,0);
> //in this space here i need to calculate a sum of all the Si's. or
> rather, need to use it in the line below. is there a sum function
> where i could say sum Values[] and have it add all the numbers stored
> in my array? because i need that sum divided by M, as seen below.


John has already given good advice about your other issues, but I
don't think he answered this. Yes, there's a standard library function
you can use to compute a sum. It's called 'std::accumulate', and it's
declared by standard header <numeric>.

Here's an example of how to use it:

#include <iostream>
#include <numeric>

int main(void)
{
double values[] = {1, 2, 3, 4, 5};
size_t elems(sizeof values / sizeof *values);
std::cout << std::accumulate(values, values + elems, 0.0) << '\n';
return 0;
}

I also second John's advice to use a 'std::vector' instead of an array.

Here's an example of how to compute the sum using a vector:

#include <iostream>
#include <numeric>
#include <vector>

int main(void)
{
std::vector<double>::size_type m(5);
std::vector<double> values(m);

for(double d = 0; d < m; ++d)
values[d] = d + 1;

std::cout << std::accumulate(values.begin(), values.end(), 0.0) << '\n';
return 0;
}

(The last argument to 'accumulate()' is a starting value to which the
array or vector elements will be added. That's why it's called
'accumulate'
and not 'sum').

-Mike


 
Reply With Quote
 
Puppet_Sock
Guest
Posts: n/a
 
      03-07-2007
On Mar 7, 1:51 am, (E-Mail Removed) wrote:
[snip]
> We are definately
> computer programming "noobs"


General hint then: There is a news group "down the web"
that has "learning" in the title. Possibly they will have
much more help for you there.

Also, you want to get yourself a good textbook.
I quite like _Accelerated C++_ by Koenig and Moo.
You probably want that first, then Stroustrup's
latest on C++. Then after that, depending on how
much development you are going to do you may want
some good texts on how to program well. Say, along
the lines of _Code Complete_.
Socks

 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      03-07-2007
<(E-Mail Removed)> wrote:

> Sure, other programs are easier to use, and more user friendly, and
> nicer for beginners; i agree whole heartedly. However, its not like
> using C++ is an option. We are told by our proffessor to write this
> program in C++ to figure out these financial things. Monte Carlo
> simulation is something I have done in the past, and used other
> software that was amazingly simple, such as crystal ball, where you
> just input some stuff and away you go. However, we have to sit here
> and derive how to do very basic simple things that excel, matlab, etc.
> can do (or even using a statistics text book!); for example, we had to
> write a program that calculated the normal_cdf function. I don't know
> why, its not up to me, but we have to do what our proffessors tell us
> to do on the software they assign us.


Were you told not to use rand() and srand() which are an intrinsic part of
C++? That seems to be what your uniform_deviate() is doing. A few minutes
of patient searching might find code for normal_deviate() as well. Adding
the word 'snippets' to a search is sometimes helpful in looking for source
code.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Running a program (gperf) inside another C program Alexander Mahone C Programming 4 05-26-2008 09:31 PM
running another program from a C++ program Lorenzo Bettini C++ 3 09-24-2005 10:05 AM
Help !I want to write a program to count the running time of another program freehomesp@yahoo.com.cn C Programming 1 08-12-2005 06:13 AM
More American Graffiti: Properly Framed, Properly Scored? Scot Gardner DVD Video 0 09-02-2003 02:28 AM
Perl script not running properly... abstractjoe Perl Misc 0 07-11-2003 06:18 PM



Advertisments