Anyone know C++ ? (NLC)

Anything goes in here.....
fd
Posts: 883
Joined: Mon Apr 23, 2007 4:56 pm

Post by fd » Thu Jan 31, 2008 4:50 pm

Ok . . .

Forget c++ and research how to do it in c . . . the compiler should manage to get that working just fine . . . with minimal changes . . .

c++ is like c with a load of extra stuff that allows you to write code in an object orientated manner (a very common way of thinking about software design), Java is another OO (Object Orientated) language for example . . . I guess the vast majority of modern languages are in fact . . .

However . . . for you . . . object orientation, unless specifically part of the assignment, is more or less completely irrelevant . . . as it brings nothing apart from unecessary complexity . . . especially in terms of doing some maths . . . which is all I think you are trying to do . . .

c++ has it's place, personally I like it - it's C (my original language) with bells on to help you (perhaps blow your leg off), some hate it and some would say it's had it's day, regardlessit is a dangerous tool . . . kinda like a fission reactor . . . very useful in the right hands . . . somewhat of a liability in the wrong ones . . .

I've just googled for C code to calculate PI, plenty of exmples out there . . .

As I recall I had a coursework to do this years ago . . . I think just after PI was invented . . . but computers were so slow at the time we probably only got as far as 3.14159 . . . ;-)

Go speak to the russian, take him some uranium or some old credit card reciepts and tell him you need some help . . .

Fd

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

Post by Tom » Thu Jan 31, 2008 4:53 pm

fd wrote:Ok . . .

Forget c++ and research how to do it in c . . . the compiler should manage to get that working just fine . . . with minimal changes . . .

c++ is like c with a load of extra stuff that allows you to write code in an object orientated manner (a very common way of thinking about software design), Java is another OO (Object Orientated) language for example . . . I guess the vast majority of modern languages are in fact . . .

However . . . for you . . . object orientation, unless specifically part of the assignment, is more or less completely irrelevant . . . as it brings nothing apart from unecessary complexity . . . especially in terms of doing some maths . . . which is all I think you are trying to do . . .

c++ has it's place, personally I like it - it's C (my original language) with bells on to help you (perhaps blow your leg off), some hate it and some would say it's had it's day, regardlessit is a dangerous tool . . . kinda like a fission reactor . . . very useful in the right hands . . . somewhat of a liability in the wrong ones . . .

I've just googled for C code to calculate PI, plenty of exmples out there . . .

As I recall I had a coursework to do this years ago . . . I think just after PI was invented . . . but computers were so slow at the time we probably only got as far as 3.14159 . . . ;-)

Go speak to the russian, take him some uranium or some old credit card reciepts and tell him you need some help . . .

Fd
thanks fergus.

i'd googled for c++ and borland failed to run all of them. Will give it a go with C instead.

Incidentally, are all compilers essentially the same?? ie is Borland any good compared to the others...
1995 Volvo 940SE Estate

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

Post by graeme » Thu Jan 31, 2008 5:00 pm

I've got a working app for you apart from the actual logic bit, but am now googling to find the missing infinite series that didn't paste properly and I'm ashamed to say I don't know how it works...

Robin? :oops:

Edit: Got it. Won't be long....

Edit: No I don't... there are loads.

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

Which one is it Tom? I'm guessing from the pow3 variable that it might be number 3.
Last edited by graeme on Thu Jan 31, 2008 6:39 pm, edited 2 times in total.
211
958

fd
Posts: 883
Joined: Mon Apr 23, 2007 4:56 pm

Post by fd » Thu Jan 31, 2008 5:04 pm

Compilers - depends . . .

I'd use GNU c/c++ because it's free and highly available on many operating systems . . . same code compiles on my mac, my linux box and a pc . . . I used Borland in the past and my recollection is that it's probably slightly less standards compliant than I'd like . . . but that's probably configurable . . .

to start . . . get this compiling and running, this will prove you can get anything to work . . . than all you have to do it do the maths :-

#include <stdio>

int main(int argc, char *argv[])
{
int counter=0;

for(counter=0;counter<10;counter++)
{
printf("My first C program : %d\n",counter);
}
}


When run it will print (the bits in bold below) :-

Macintosh:~ fergusd$ !v
vi tom.c
Macintosh:~ fergusd$ !gcc
gcc tom.c
Macintosh:~ fergusd$ ./a.out
My first C program : 0
My first C program : 1
My first C program : 2
My first C program : 3
My first C program : 4
My first C program : 5
My first C program : 6
My first C program : 7
My first C program : 8
My first C program : 9

Macintosh:~ fergusd$

fd
Posts: 883
Joined: Mon Apr 23, 2007 4:56 pm

Post by fd » Thu Jan 31, 2008 5:25 pm

And to demonstrate (because I'm bored and wasting time waiting on somebody doing something . . .) that the same code works on another machine and even a c++ compiler without change . . . here it is copied to a linux machine, compiled and run using the gcc (GNU C) compiler then the g++ (GNU C++) compiler . . .

[fd@fdlinux ~]$ scp fergusd@192.168.0.102:~/tom.c .
[fd@fdlinux ~]$ gcc tom.c
[fd@fdlinux ~]$ ./a.out
My first C program : 0
My first C program : 1
My first C program : 2
My first C program : 3
My first C program : 4
My first C program : 5
My first C program : 6
My first C program : 7
My first C program : 8
My first C program : 9
[fd@fdlinux ~]$ g++ tom.c
[fd@fdlinux ~]$ ./a.out
My first C program : 0
My first C program : 1
My first C program : 2
My first C program : 3
My first C program : 4
My first C program : 5
My first C program : 6
My first C program : 7
My first C program : 8
My first C program : 9
[fd@fdlinux ~]$


easy peasy . . . but I think you should do the coursework . . . engineering moves further and further into software every day . . .

Fd

User avatar
mac
Posts: 6880
Joined: Mon Sep 19, 2005 4:36 pm

Post by mac » Thu Jan 31, 2008 5:27 pm

fd wrote:And to demonstrate (because I'm bored and wasting time waiting on somebody doing something . . .) that the same code works on another machine and even a c++ compiler without change . . . here it is copied to a linux machine, compiled and run using the gcc (GNU C) compiler then the g++ (GNU C++) compiler . . .

[fd@fdlinux ~]$ scp fergusd@192.168.0.102:~/tom.c .
[fd@fdlinux ~]$ gcc tom.c
[fd@fdlinux ~]$ ./a.out
My first C program : 0
My first C program : 1
My first C program : 2
My first C program : 3
My first C program : 4
My first C program : 5
My first C program : 6
My first C program : 7
My first C program : 8
My first C program : 9
[fd@fdlinux ~]$ g++ tom.c
[fd@fdlinux ~]$ ./a.out
My first C program : 0
My first C program : 1
My first C program : 2
My first C program : 3
My first C program : 4
My first C program : 5
My first C program : 6
My first C program : 7
My first C program : 8
My first C program : 9
[fd@fdlinux ~]$


easy peasy . . . but I think you should do the coursework . . . engineering moves further and further into software every day . . .

Fd

Yup - even our signals are moving from wires and relays to the odd SSI unit (solid state interlocking)


Mac
S2 Elise (cobalt blue with stripes) - toy spec
Caterham 7 - hillclimb spec
Yamaha Thundercat - 2 wheeled toy spec

User avatar
Ardiem
Posts: 70
Joined: Fri Jan 11, 2008 5:20 pm

Post by Ardiem » Thu Jan 31, 2008 5:32 pm

Not sure if the the algorithm is the problem - the variables and method have all been provided in the brief. I'm guessing that the main problem you have is with the syntax? As in what code do you actually write - the grammar of the code?

Too many years of writing visual basic and having my hand held by wizards means I have absolutely no idea how to help you with C++.

I would probably work out the formula so you know what you want to program before you start trying to write the code to do it...
Lotus Elise S2 - Bog standard spec
Aprilia Tuono 1000 - Nutter spec
Mazda Mx5 - Hairdresser spec

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

Post by graeme » Thu Jan 31, 2008 6:04 pm

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;
}
Last edited by graeme on Thu Jan 31, 2008 7:33 pm, edited 2 times in total.
211
958

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

Post by graeme » Thu Jan 31, 2008 6:05 pm

Output looks like:


gfraser@exige pi $ ./a.out
Enter the number of iterations: 15
After iteration 1 our estimate is 3.464102
The difference between our estimate and pi is 0.322509
After iteration 2 our estimate is 3.079201
The difference between our estimate and pi is 0.062391
After iteration 3 our estimate is 3.156181
The difference between our estimate and pi is 0.014589
After iteration 4 our estimate is 3.137853
The difference between our estimate and pi is 0.003740
After iteration 5 our estimate is 3.142605
The difference between our estimate and pi is 0.001012
After iteration 6 our estimate is 3.141309
The difference between our estimate and pi is 0.000284
After iteration 7 our estimate is 3.141674
The difference between our estimate and pi is 0.000082
After iteration 8 our estimate is 3.141569
The difference between our estimate and pi is 0.000024
After iteration 9 our estimate is 3.141600
The difference between our estimate and pi is 0.000007
After iteration 10 our estimate is 3.141591
The difference between our estimate and pi is 0.000002
After iteration 11 our estimate is 3.141593
The difference between our estimate and pi is 0.000001
After iteration 12 our estimate is 3.141592
The difference between our estimate and pi is 0.000000
After iteration 13 our estimate is 3.141593
The difference between our estimate and pi is 0.000000
After iteration 14 our estimate is 3.141593
The difference between our estimate and pi is 0.000000
After iteration 15 our estimate is 3.141593
The difference between our estimate and pi is 0.000000


Yes, my laptop is called 'exige' :)
211
958

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

Post by graeme » Thu Jan 31, 2008 6:14 pm

fd wrote: easy peasy . . . but I think you should do the coursework . . . engineering moves further and further into software every day . . .
:withstupid

I'd be a hypocrite not to help, as I know how much Simon helped me kick of with my programming. PM me Tom with your schedule and we'll have a phone-chat one evening before it's due in and I'll talk you through it in depth until you totally 'get it'.

:)
211
958

User avatar
pfw
Posts: 47
Joined: Sun Apr 02, 2006 8:08 pm
Location: Dunfermline

Post by pfw » Thu Jan 31, 2008 7:27 pm

Just one thing to add - C++ isn't just an object oriented add-on to C (although that's sort of how it started). There's loads more stuff - a separate standard library, and support for generic programming for example. It's a very powerful language, but very difficult (too difficult IMO) and has far more portability problems than straight C. If it's just an algorithmic problem and you're not familiar with C++ I agree with what's been said - stick with C.

FWIW the "C++ style" equivalent of the simple program would be something like this (I haven't checked this so caveat typos):

Code: Select all

#include <iostream>

int main(int argc, char*argv[])
{
  for (int counter=0;counter<10;counter++)
  {
    std::cout << "My first C++ program: " << counter << std::endl;
  }
}
I'm now trying to decide if posting code on a car forum is cool or sad :D

fd
Posts: 883
Joined: Mon Apr 23, 2007 4:56 pm

Post by fd » Thu Jan 31, 2008 8:12 pm

Sure, c++ has, like many other languages, bloated into many other things . . . but fundamentally, for someone who cannot code in c, it's an OO version of C.

Fd

User avatar
campbell
Posts: 17331
Joined: Sat Mar 25, 2006 12:42 pm
Location: West Lothian
Contact:

Post by campbell » Thu Jan 31, 2008 11:54 pm

Tom,

All the top brains of SE have done your homework for you above...I wondered how long that would take!

Also as someone not very keen on C never mind C++, I was going to re-iterate the point that Borland C++ will be just fine to do a regular ordinary C assignment.

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.

But consider this. If Fd, Robin, Lawrence, and various others who helped me with my car had just "done it" for me, I'd have missed out on a lot of learning. By encouraging, goading, shaming or otherwise finding any way possible to get me to do at least some of the stuff...half the time I was petrified either for my own safety, the car, or that of innocent bystanders!...these fellas got me to build the knowledge and confidence for myself.

OK so I won't change a cylinder head on my own, but I WILL tackle a lot more than I would have done.

So get those sleeves rolled up, play with the "my first C program" and if needs be, get "C for Dummies" or summat and enjoy yourself. Who knows, next thing you'll be writing your own engine management unit...

Campbell

PS - don't have any unsaved docs open on your PC while you test run the program...C is terribly powerful and quite literally will let you do anything to any part of your computer. It won't blow it up...but it could easily blitz that 10,000 word assignment you were nearly finished writing on "the vagaries of plagiarism in modern university courses" ;-)
http://www.rathmhor.com | Coaching, training, consultancy

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

Post by Tom » Fri Feb 01, 2008 12:44 am

Thanks everyone (esp Graeme and Fergus) for your help. I've been trying all evening to download Borland to my home PC but i can't get it to work so I haven't actually been able to see if i can get either of Fd or Graeme's programmes to work. Will try them at uni tomorrow.

In reference to Campbell's post I wholeheartedly agree that the best way to teach someone something is to have them do it themselves. It seems to me that the best way to teach this sort of thing would be to have everyone in a big lecture hall with a computer each, and get them to try stuff as they're going along, rather than having some Russian genius who probably knows more about C than he does about English stand at the front going through it as if it's as easy as say walking, or breathing, or some other very easy thing... I've never (well nearly never) handed in/done anything without understanding it. Getting someone else to do it now would mean that the next time I'm presented with a more complicated problem I'll be in even deeper poop. I want to get my head round this, but it's like I'm reading Russian. I don't think at this rate that it would be posible for me to produce this in the next week.

So, Fd and Graeme have one (poss. even two) in the wood from me. Your help is greatly appreciated :thumbsup
1995 Volvo 940SE Estate

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

Post by robin » Fri Feb 01, 2008 9:18 am

Tom,

PM sent re: talking you through the assignment and how to craft your own solution.

Just imagine how much fun they had implementing this algorithm back in 1699 :-)

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

Post Reply