Created
April 28, 2015 13:41
-
-
Save sneznaovca/2f070dfb16e520bfba4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* usage: php matrix.php 4 2 3 | |
*/ | |
$m=$argv[1]; | |
$n=$argv[2]; | |
$k=$argv[3]; | |
$f1 = fopen(__DIR__ . '/mat1', 'w'); | |
fwrite($f1, $m . "\n"); | |
$f2 = fopen(__DIR__ . '/mat2', 'w'); | |
fwrite($f2, $k . "\n"); | |
saveMatrix($f1, generateMatrix($m, $n)); | |
saveMatrix($f2, generateMatrix($n, $k)); | |
fclose($f1); | |
fclose($f2); | |
function generateMatrix ($rows, $cols) | |
{ | |
$matrix = array(); | |
for ($row=0; $row < $rows; $row++) { | |
for ($col=0; $col < $cols; $col++) { | |
$matrix [$row][$col] = mt_rand($min = -150, $max = 1024); | |
} | |
} | |
return $matrix; | |
} | |
function saveMatrix ($fp, $matrix) | |
{ | |
foreach ($matrix as $row) { | |
fwrite($fp, implode(' ', $row) . "\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment