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

Posts: 922
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
 
 
When You Register, These Ads Go Away!
Old 01-02-2009, 08:07 AM Re: C++ question - managing basic string arrays
chrishirst's Avatar
Super Moderator

Posts: 22,281
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!! <<-
Growing old is mandatory - Growing up is optional
Code Samples | People Counting System | Bits & Bobs
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: 922
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
Super Moderator

Posts: 22,281
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!! <<-
Growing old is mandatory - Growing up is optional
Code Samples | People Counting System | Bits & Bobs
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: 922
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.12698 seconds with 13 queries