Anyone know C++ ? (NLC)

Anything goes in here.....
User avatar
graeme
Posts: 3528
Joined: Tue Mar 15, 2005 11:29 am
Location: Kintore

Post by graeme » Fri Feb 01, 2008 10:54 am

campbell wrote: It's a shame your lecturer hasn't put all this into better context for you, because it is possible to gradually develop an interest in computer programming without the in-at-the-deep-end style being used here!

I agree you should indeed do your own coursework, you will never learn how to do it for yourself in future if the help is delivered verbatim - but Graeme and Fergus I take my hat off to you both for what you knocked out and your intentions are totally sound. Very much in the spirit of this group in fact. I particularly liked Fd's suggestion for you to get googling and researching...just don't copy the stuff you find, play with it and make it your own.
Nail/Head Campbell. (See, we always agree! :) )

It's no coincidence that Tom and I (dare I say two reasonably intelligent individuals) both needed help with the same module at the same uni; it's a very poor introduction to programming. The CompSci crowd get to focus on learning C all week, but the engineers are supposed to pick it up along side their main subject with a teeeeny fraction of the tutoring and time. It turns programming into a frightening distraction instead of the fascinating subject it should be.

My posted solution works, but is not hand-inable. There's more to be done. Validation of input, error handling, commenting, increasing the precision, renaming everything...

A wee bit of SE tutoring will go a long way, so Tom, do get in touch with Robin or I or anyone else who has offered.
211
958

User avatar
Tom
Posts: 3220
Joined: Mon Mar 06, 2006 5:50 pm
Location: UK

Post by Tom » Fri Feb 01, 2008 5:35 pm

graeme wrote:It's no coincidence that Tom and I (dare I say two reasonably intelligent individuals) both needed help with the same module at the same uni; it's a very poor introduction to programming. The CompSci crowd get to focus on learning C all week, but the engineers are supposed to pick it up along side their main subject with a teeeeny fraction of the tutoring and time. It turns programming into a frightening distraction instead of the fascinating subject it should be.
Nail on head there Graeme, this is exactly how it feels..
(thanks for the compliment too :D :thumbsup)
graeme wrote:My posted solution works, but is not hand-inable. There's more to be done. Validation of input, error handling, commenting, increasing the precision, renaming everything....
:shock: I'm sure Robin will explain what this bit means...... :shock: :lol:
graeme wrote:A wee bit of SE tutoring will go a long way, so Tom, do get in touch with Robin or I or anyone else who has offered.
Thanks a lot to SE.. :D I'll be getting some help on Sunday from Robin which will hopefully clear things up for me. Not just a car forum, but is useful for all sorts things..(the perfect excuse for being on the forum :roll: :roll: "just checking up on my C++ post dear" :roll: :lol: )
1995 Volvo 940SE Estate

User avatar
robin
Jedi Master
Posts: 10544
Joined: Mon Mar 27, 2006 1:39 pm

Post by robin » Sun Feb 03, 2008 3:09 pm

graeme wrote:

Code: Select all

#include <math>
#include <stdio>

int main (void)
{
        int iterations = 0;
        double estimate = 1, accumulator=0, const_part=0;
        int pow3=1,k;

        printf("Enter the number of iterations: ");
        scanf("%d", &iterations);

        /*
        ** [pi/6] = [square root 1/3] . [1/(3^0 x 1) - 1/(3^1 x 3) + 1/(3^2 x 5)]
        */

        const_part = 6* sqrt(1.0/3.0);

        for(k=1; k<=iterations; k++){

                if(k%2==0){
                        accumulator = accumulator - (1.0/(pow3 * (k+(k-1))));
                } else {
                        accumulator = accumulator + (1.0/(pow3 * (k+(k-1))));
                }

                estimate =  const_part * accumulator;

                printf("After iteration %d  our estimate is %f\n", k, estimate);
                printf("The difference between our estimate and pi is %f\n", fabs(estimate - M_PI));

                pow3 = pow3*3;

        }

        return 0;
}
Unless this was a deliberate entry to the C obfuscation competition, I am so glad you don't write code for me ;-) :-) :-)

Robin
I is in your loomz nibblin ur wirez
#bemoretut

User avatar
graeme
Posts: 3528
Joined: Tue Mar 15, 2005 11:29 am
Location: Kintore

Post by graeme » Sun Feb 03, 2008 6:36 pm

Oi! :)

I refer my right honourable friend to the posting I made some moments ago, where I clearly stated it needed commenting, tidying up, all the variables renamed, optimised etc etc etc...

It wurks, dunnit? Moon on a bloody stick you lot!

Actually, yeah, it's a good job I don't write C for anyone any more...

Graeme
(senior management and a bit rusty faction)
211
958

User avatar
robin
Jedi Master
Posts: 10544
Joined: Mon Mar 27, 2006 1:39 pm

Post by robin » Sun Feb 03, 2008 6:46 pm

Well, if you really must know:

The killer for me is the use of k=1 to N rather than k=0 to N-1 (the integral in the original algorithm is clearly from 0 not 1). This causes the sign to be inverted (-1 to the power k should be +ve for even values of k and -ve for odd values of k; in the above implementation you use the opposite, but that's because your k is one step more advanced than that of the algorithm; then you have the expression (2k+1) which becomes (k+(k-1)) when k is one step further along than it should be ...

The conversion from 2*sqrt(3) to 6*sqrt(1/3) is novel but it isn't clear it does anything other than obfuscate the original algorithm.

Not saying it doesn't work - of course it does - but it took me 30 seconds to understand how it worked and that didn't seem necessary :-)

Cheers,
Robin
I is in your loomz nibblin ur wirez
#bemoretut

User avatar
graeme
Posts: 3528
Joined: Tue Mar 15, 2005 11:29 am
Location: Kintore

Post by graeme » Sun Feb 03, 2008 7:28 pm

If you look back to the assignment Tom posted, the pseudocode says k=1 to N so I tried to stick with that. I flipped the if/else to make it work. I'd have done it zero-indexed too. I suspect that the series I used (the one in the comment) may not be identically written to the one on the assignment (which failed to paste properly when Tom posted it and left me guessing).

The sqrt(1/3) was deliberately left as 1/3 as an attempt to make things clearer (obviously didn't work!) as that is how it was written in the series in the comment included in the code which is how I found it on the web.

How is the tutoring going?
211
958

User avatar
Tom
Posts: 3220
Joined: Mon Mar 06, 2006 5:50 pm
Location: UK

Post by Tom » Sun Feb 03, 2008 10:51 pm

really well. spent 4 hours on it with Robin today (cheers for that Robin - How's the red?). It all seems a lot clearer now that I had it explained to me. \n and %d and & all make sense now :D . I actually quite enjoyed it :oops: :oops: , I can see what you mean....
1995 Volvo 940SE Estate

User avatar
robin
Jedi Master
Posts: 10544
Joined: Mon Mar 27, 2006 1:39 pm

Post by robin » Sun Feb 03, 2008 11:04 pm

graeme wrote:If you look back to the assignment Tom posted, the pseudocode says k=1 to N so I tried to stick with that. I flipped the if/else to make it work. I'd have done it zero-indexed too. I suspect that the series I used (the one in the comment) may not be identically written to the one on the assignment (which failed to paste properly when Tom posted it and left me guessing).
Ah, well they did, but their code started with the term for k=1 too; they initialised estimate to 1 (the value of term where k=0). So their signs work the right way around ... not that I think their code is particularly sensible either (it would have been clearer in that case to init pow3=3 and then multiply by 3 after the accumulator update).

I'm not having a go, honest - just trying to point out some of the pitfalls of C - a truly awful language! Although you can write a less than obvious program in any language, C lends itself to it better than most. I don't understand why they don't teach them Java instead - much better teaching language and actually useful too :-)
How is the tutoring going?
Tom's hooked :-) I'm just about ready to drink my earnings!

Cheers,
Robin
I is in your loomz nibblin ur wirez
#bemoretut

User avatar
Tom
Posts: 3220
Joined: Mon Mar 06, 2006 5:50 pm
Location: UK

Post by Tom » Sun Feb 03, 2008 11:30 pm

why they don't teach them Java instead
might just put this question to Boris and see what he says.... :roll:
1995 Volvo 940SE Estate

User avatar
graeme
Posts: 3528
Joined: Tue Mar 15, 2005 11:29 am
Location: Kintore

Post by graeme » Tue Feb 05, 2008 9:40 am

Can't argue with this...

http://www.osnews.com/images/comics/wtfm.jpg

If it takes Robin 30 seconds to figure out my obfuscations, and he finds 2 majors wtfs, that gives me a score of 4wtf/m.

:)

I shall measure all code this way from now on...
211
958

User avatar
robin
Jedi Master
Posts: 10544
Joined: Mon Mar 27, 2006 1:39 pm

Post by robin » Tue Feb 05, 2008 8:54 pm

graeme wrote:Can't argue with this...

http://www.osnews.com/images/comics/wtfm.jpg

If it takes Robin 30 seconds to figure out my obfuscations, and he finds 2 majors wtfs, that gives me a score of 4wtf/m.

:)

I shall measure all code this way from now on...
That, my friend, is a new international standard.

WTF.

:-)

Robin
I is in your loomz nibblin ur wirez
#bemoretut

Post Reply