Ferrous Moon
http://www.ferrousmoon.com:80/forums/

Idea for a first project?
http://www.ferrousmoon.com:80/forums/viewtopic.php?f=45&t=1280
Page 1 of 3

Author:  cOde.ZRY [Tue Mar 11, 2008 5:03 pm ]
Post subject:  Idea for a first project?

I'm wanting to write my first complete C++ program, without veiwing any source. I know VB 6 very well and Im learning C++ to be able to edit my Uplink DevCD, and maybe create my own Game from scratch if I can catch on down the road.

What kind of program should i start with? Giving what I want to use C++ for.

Author:  Gwanky [Tue Mar 11, 2008 5:51 pm ]
Post subject:  Re: Idea for a first project?

An interesting (although rather difficult) program is to try and make a four function calculator.

Author:  eddieringle [Tue Mar 11, 2008 6:32 pm ]
Post subject:  Re: Idea for a first project?

Or if you are into game programming, make a simple guessing game or small text adventure/rp game.

Author:  Tycho [Tue Mar 11, 2008 8:06 pm ]
Post subject:  Re: Idea for a first project?

Well, I think the "Hello World" program nonsense is just bullshit. Don't bother with something so rudimentary. What I recommend is trying to write a prime number finder in whatever language you're intent on learning. I wrote a few versions of GenPrime in various languages (C, Perl, Java, Ada, PHP, Ruby). Not only is it a great start to learn the semantics of the language and so forth, but you also learn which languages generate faster-executing code.

Speaking of which, I was impressed with the speed of Ruby. It's much slower than C, but compared to PHP, Perl or Java, Ruby is fast.

Author:  cOde.ZRY [Tue Mar 11, 2008 8:35 pm ]
Post subject:  Re: Idea for a first project?

Gwanky: Thanks for the suggestion, I'm researching some ways of doing that.

cpu5594: I remembered a game I made when i was 12 with Qbasic on my 386 computer. Im writting it right now in C++, and see if i can pull it off,I will post it and the source later tonight on my site if anyone wants to take a look at it.

Tycho: Would using switches be the fastest way to approach doing that, or just plain and simple endif's? (sadly I do not have much schooling, but I think i remember 2 is a prime, and only even, I think :? ..thats a start atleast, eh?

Also, I herd someone talk about Ruby that got me into VB, dont know anything about it really. I herd C++ Turbo is a good compiler to use. But im content with VS express, im getting use to how to do stuff in it.

Author:  eddieringle [Tue Mar 11, 2008 8:37 pm ]
Post subject:  Re: Idea for a first project?

Tycho might not like this, but there's an entry in IO.IN's wiki about GenPrime.
http://www.ferrousmoon.com/wiki/index.php/GenPrime

Author:  Tycho [Tue Mar 11, 2008 9:13 pm ]
Post subject:  Re: Idea for a first project?

Quote:
Tycho might not like this, but there's an entry in IO.IN's wiki about GenPrime.
http://www.ferrousmoon.com/wiki/index.php/GenPrime
I recommend against using the algorithms that article mentions. I've since improved the algorithm I used.

Here are the current versions (Ruby, C, and PHP listed here only, for now):

C
Code:
/* * GenPrime * * (c) 2006-2008 Steven Noonan. * Licensed under the New BSD License. * */ #include <stdio.h> #include <stdlib.h> #include <math.h> #include <memory.h> #include <sys/time.h> int isPrime ( unsigned long _candidate ) { unsigned long i, limit; /* 0 and 1 aren't prime. */ if ( _candidate < 2 ) return 0; /* All numbers less than 4 are prime, except '1' */ if ( _candidate < 4 ) return -1; /* Other than 2, even numbers are not prime */ if ( _candidate % 2 == 0 ) return 0; /* All primes are of the form 6k+i (where i = -1 or 1), except for 2 or 3. */ if ( ( _candidate + 1 ) % 6 != 0 && ( _candidate - 1 ) % 6 != 0 ) return 0; /* if n is composite then it can be factored into two values, at least one of which is less than or equal to sqrt(n) */ limit = (unsigned long)sqrt ((double)_candidate); /* Now test all other odd numbers up to sqrt(n) */ for ( i = 3; i <= limit; i += 2 ) if ( _candidate % i == 0 ) return 0; return 1; } unsigned long genPrime ( unsigned long _maxToFind ) { unsigned long count = 0, num; for ( num = 1; count < _maxToFind; num++ ) { if ( isPrime ( num ) ) { count++; } } /* Returning 'count' prevents the compiler from optimizing this function out as worthless */ return count; } int main ( int argc, char **argv ) { struct timeval start; struct timeval finish; unsigned long i; for ( i = 100000; i <= 500000; i += 100000 ) { gettimeofday ( &start, NULL ); genPrime ( i ); gettimeofday ( &finish, NULL ); double secondsElapsed = (double)( finish.tv_sec - start.tv_sec ) + ( (double)finish.tv_usec - (double)start.tv_usec ) / 1000000.0; printf ( "Time for %9lu primes: %6.3lf seconds (%lu PPS)\n", i, secondsElapsed, (unsigned long)((double)i / secondsElapsed) ); } return 0; }
Ruby
Code:
#!/usr/bin/ruby -w # # GenPrime # # (c) 2006-2008 Steven Noonan. # Licensed under the New BSD License. # # def isPrime ( _candidate ) # 0 and 1 aren't prime. if _candidate < 2 return 0 end # All numbers less than 4 are prime, except '1' if _candidate < 4 return -1 end # Other than 2, even numbers are not prime if _candidate % 2 == 0 return 0 end # All primes are of the form 6k+i (where i = -1 or 1), except for 2 or 3. if ( _candidate + 1 ) % 6 != 0 and ( _candidate - 1 ) % 6 != 0 return 0 end # if n is composite then it can be factored into two values, # at least one of which is less than or equal to sqrt(n) limit = Math.sqrt( _candidate ) # Now test all other odd numbers up to sqrt(n) i = 3 while i <= limit if ( _candidate % i == 0 ) return 0 end i += 2 end return 1 end def genPrime ( _maxToFind ) num = 1 count = 0 while count < _maxToFind if isPrime( num ) count += 1 end num += 1 end # Returning 'count' prevents the compiler from optimizing this function out as worthless return count end i = 100000 while i <= 500000 start = Time.now genPrime( i ) finish = Time.now secondsElapsed = finish.to_f - start.to_f printf( "Time for %9u primes: %6.3f seconds (%u PPS)\n", i, secondsElapsed, i / secondsElapsed ); i += 100000 end
PHP
Code:
<?php /* * GenPrime * * (c) 2006-2008 Steven Noonan. * Licensed under the New BSD License. * */ function isPrime ( $_candidate ) { /* 0 and 1 aren't prime. */ if ( $_candidate < 2 ) return 0; /* All numbers less than 4 are prime, except '1' */ if ( $_candidate < 4 ) return -1; /* Other than 2, even numbers are not prime */ if ( $_candidate % 2 == 0 ) return 0; /* All primes are of the form 6k+i (where i = -1 or 1), except for 2 or 3. */ if ( ( $_candidate + 1 ) % 6 != 0 && ( $_candidate - 1 ) % 6 != 0 ) return 0; /* if n is composite then it can be factored into two values, at least one of which is less than or equal to sqrt(n) */ $limit = sqrt ( $_candidate ); /* Now test all other odd numbers up to sqrt(n) */ for ( $i = 3; $i <= $limit; $i += 2 ) if ( $_candidate % $i == 0 ) return 0; return 1; } function genPrime ( $_maxToFind ) { $count = 0; for ( $num = 1; $count < $_maxToFind; $num++ ) { if ( isPrime ( $num ) ) { $count++; } } return $count; } for ( $i = 100000; $i <= 500000; $i += 100000 ) { $start = microtime(true); genPrime ( $i ); $finish = microtime(true); $secondsElapsed = $finish - $start; printf ( "Time for %9lu primes: %6.3lf seconds (%lu PPS)\n", $i, $secondsElapsed, $i / $secondsElapsed ); } ?>

Author:  ChaosR [Wed Mar 12, 2008 5:25 am ]
Post subject:  Re: Idea for a first project?

yuk, printf on PHP.

Author:  Tycho [Wed Mar 12, 2008 6:08 am ]
Post subject:  Re: Idea for a first project?

Quote:
yuk, printf on PHP.
yuk, PHP.

Author:  ChaosR [Wed Mar 12, 2008 11:04 am ]
Post subject:  Re: Idea for a first project?

Also, amazingly, your C program does not compile if I don't optimize (-O[<num>]) it.

Besides that, I personally like PHP a lot. It might be really (well, about 10-50 times) slow compared to languages like C, but for most simple scripts you don't even need speed. PHP has the advantage of being much simpler than C when coding, because you don't have to worry about everything.

Author:  Tycho [Wed Mar 12, 2008 1:32 pm ]
Post subject:  Re: Idea for a first project?

Doesn't compile with what compiler? What errors do you get?

And PHP is great for web pages, but as a general use language, it's terrible.

Author:  prophile [Wed Mar 12, 2008 1:44 pm ]
Post subject:  Re: Idea for a first project?

I'd love to see how Lua performs in the GenPrime benchmarks.

Author:  ChaosR [Wed Mar 12, 2008 1:47 pm ]
Post subject:  Re: Idea for a first project?

gcc, sqrt is not a function

Also, aabout 75% of all my code here is written in PHP. This includes 3 IRC bot (one minimalist (only 1 line of code :O:O), one without classes, one with classes) and an MSN chatterbot. PHP is really good in doing one thing, processing really sketchy, ugly code. It also has perl-like regex, which I use a _LOT_.

Author:  Rickton [Wed Mar 12, 2008 2:06 pm ]
Post subject:  Re: Idea for a first project?

Make the Cloak of Darkness game.
Of course that's usually intended more for IF languages, but whatever.

(But seriously guys, does every topic in the programming section have to become a this language v. that language argument?)

Author:  cOde.ZRY [Wed Mar 12, 2008 4:59 pm ]
Post subject:  Re: Idea for a first project?

Rickton: Thanks for the link, im looking into it now. I've also obtained the source for Wolfenstien, Quake, and Doom. I thought it might be a good source to teach me about mapping and using game engines.
Quote:
And PHP is great for web pages, but as a general use language, it's terrible.
Im using XML, and some Flash for my site, im don't know much about the php language, would it be better to switch to PHP for my main site?

Page 1 of 3 All times are UTC-05:00
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/