Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
C++ question - managing basic string arrays
Old 01-01-2009, 07:51 PM C++ question - managing basic string arrays
Banned

Posts: 923
Name: Geoff Vader
Location: In my dreams
Trades: 0
Here is the output of this perl script
http://www.politicsdebate.co.uk/cgi-bin/card.cgi

This is the perl code
Code:
#!/usr/bin/perl
print "Content-type:text/html\n\n";

@suits=("hearts","clubs","spades","diamonds");
@cards=("ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king");

$s=rand 40;
$c=rand 130;

$s=int $s/10;
$c=int $c/10;

print "$cards[$c] of $suits[$s]";
What's the shortest C++ translation of that code possible?

How do I do the arrays in an efficient way?

Whether or not anyone's going to help, I reckon I shall produce a version of the same script in...

C++
php
.aspx
java
hell, even javascript
plus a (bash)shell version to run on the commandline

Last edited by witnesstheday; 01-01-2009 at 08:02 PM..
witnesstheday is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-02-2009, 08:07 AM Re: C++ question - managing basic string arrays
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Javascript
Code:
<script type="text/javascript">
var suits = new Array("hearts","clubs","spades","diamonds");
var cards = new Array("ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king");

var s = Math.floor(Math.random() * 30);
var c= Math.floor(Math.random() *  120);

s = Math.round(s/10);
c = Math.round(c/10);

document.write(cards[c] + " of " + suits[s]);
</script>
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-02-2009, 08:33 AM Re: C++ question - managing basic string arrays
Banned

Posts: 923
Name: Geoff Vader
Location: In my dreams
Trades: 0
nice! but...

Are you sure it should be 30 and 120?

I took zero into account in the 40 and 130...

if it falls between 0 to 10, the integer value is still 0.

As for taking the precaution to not go too far, maybe I should have made it 39 and 129, so that it couldn't be a 5 or 14.
witnesstheday is offline
Reply With Quote
View Public Profile
 
Old 01-02-2009, 08:49 AM Re: C++ question - managing basic string arrays
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
I don't code in perl at all, it's a bit like Latin, a long dead language used by academics
But 0 to 3 is a four element array in javascript.

Math.floor will round downwards to the nearest integer so 40 could be used but the random number can be in the range 0 to 1, so if it does return 1 the value would be 40 and therefore out of range for the array.

I'll put the code in a page and test for getting full ranges later.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-02-2009, 09:34 AM Re: C++ question - managing basic string arrays
Banned

Posts: 923
Name: Geoff Vader
Location: In my dreams
Trades: 0
I think I should not have gone above 39 and 129 - I could test it by getting it to run the thing about 10,000 times...

looks like I'll have to run it 1,000,000 times to catch an error...

A perfect opportunity to finish the port while I'm waiting. And then there's still that quarter bottle of rum...

I stopped it at 200,000... not a single bad result. That's because I suppose when you do rand in perl, it isn't an integer, so rand 40 is really really really unlikely to ever hit 40 on the nose. it's still out there, a rogue likelihood. i think i should set a command running on about 10 million tests or even 100 million tests, to catch it out, but maybe not right now and certainly not on my commercial server!

Quote:
#!/usr/bin/perl
$cow=rand 40;
print $cow;


[root@server cgi-bin]# perl perly.pl
3.06820604313558[cgi-bin]# perl perly.pl
24.8692611913795[cgi-bin]# perl perly.pl
22.6440182543958[cgi-bin]# perl perly.pl
30.7815393462444[cgi-bin]# perl perly.pl
10.5075733765626[cgi-bin]# perl perly.pl
35.189733098414[cgi-bin]#
witnesstheday is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to C++ question - managing basic string arrays
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.21117 seconds with 12 queries