Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 28th, 2008, 3:53 AM   #1
dark06
Newbie
 
Join Date: Mar 2008
Posts: 1
Rep Power: 0 dark06 is on a distinguished road
help for (for)

<?
$c = $_POST['col'];
$r = $_POST['rw'];
?>

<?
for($cvchk = 1; $cchk<= $c; $cchk ++)
{
echo $cchk."<br>";

for($rchk = 1; $rchk<= $r; $rchk ++)
{
echo $rchk." ";
}
}

?>

i don't know what is wrong with this one
i want the output to become this one

1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3

i really kinda new with this thanks a lot guys

guys can you help me out with this one..
dark06 is offline   Reply With Quote
Old Mar 28th, 2008, 6:15 AM   #2
Freaky Chris
Hobbyist Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 130
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: help for (for)

Couple of things, firtsly could you use code tags to place code in a post.

Secondly welcome to the forums.

OK one of your first problems was your initial for statement had seom variables named wrongly

PHP Syntax (Toggle Plain Text)
  1. for($cvchk = 1; $cchk <= $c; $cchk ++)

then there were a few other problems to such as at no point were you specifying to start from any number other than 1, so it would always count from 1-4.

you were also echoing the value of cchk for no reason.

here is the code i come up with not the best for many reasons, such as im not a great programmer/scripter plus i don't know the PHP language but here it is anyway

PHP Syntax (Toggle Plain Text)
  1. <?
  2. $c = $_POST['col'];
  3. $r = $_POST['rw'];
  4. ?>
  5.  
  6. <?
  7. for($cvchk = 1; $cvchk <= $c; $cvchk ++)
  8. {
  9. $x = $cvchk;
  10. for($rchk = 1; $rchk <= $r; $rchk ++)
  11. {
  12. if ($x > 4)
  13. {
  14. $x = 1;
  15. }
  16. echo $x." ";
  17. $x += 1;
  18. }
  19. echo "<br />";
  20. }
  21. ?>

As you can see i also check to see if x goes above 4 if so it returns it to 1, this ensure that it doesn't produce this outcome

1234
2345
3456
4567

Also this is unchecked but to make this work for any number of rows, so the highest number = the number of ros simple change this

PHP Syntax (Toggle Plain Text)
  1. if ($x > 4)
  2. {
  3. $x = 1;
  4. }
  5. .....
  6. if ($x > $r)
  7. {
  8. $x = 1;
  9. }


Hope this helped if you need anything else just ask.
__________________
Who said i couldn't program
sarcasm = raw_input('Type in a sarcastic remark: ')
print sarcasm

Last edited by Freaky Chris; Mar 28th, 2008 at 6:30 AM.
Freaky Chris is offline   Reply With Quote
Old Mar 28th, 2008, 12:07 PM   #3
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 365
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Re: help for (for)

This is a bit more memory intensive and slower as well, but I like how it works:
php Syntax (Toggle Plain Text)
  1. <?php
  2. // Get matrix size
  3. $rowSize = $_POST['rw'];
  4. $colSize = $_POST['col'];
  5. ?>
  6. <?php
  7. // Construct matrix
  8. $shiftable = array();
  9. for($i = 0; $i < $rowSize; $i++){
  10. $temp = array();
  11. for($j = 0; $j < $colSize; $j++){
  12. array_push($temp, $j);
  13. }
  14. array_push($shiftable, $temp);
  15. shiftRow($shiftable, $i, $i); // Shift matrix accordingly
  16. }
  17. ?>
  18. <pre>
  19. <?php
  20. // Print matrix
  21. foreach($shiftable as $row){
  22. foreach($row as $j => $value){
  23. echo str_pad($value, 2, ' ', STR_PAD_LEFT);
  24. }
  25. echo "\n";
  26. }
  27. ?>
  28. </pre>
  29. <?php
  30. // Row shifting function
  31. function shiftRow(&$dArray, $shiftRow, $shiftAmount){
  32. for($i = 0; $i < $shiftAmount; $i++){
  33. array_push($dArray[$shiftRow], array_shift($dArray[$shiftRow]));
  34. }
  35. }
  36. ?>
__________________
You are here to serve the board. So post well... and live. SPAMMING SPEED! GrimBB
grimpirate is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:27 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC