wazza91 Posted February 17, 2011 Share Posted February 17, 2011 i have been given this task in in uni. i was wondering it some one would be so kind to help me with it. all i have so far is this <?php for($x=1; $x<=3; $x=$x+1) { print "$x <br>"; } if ($x== "A") { } ?> the task is below Generate a number between 1 & 3 If 1 then assign A, If 2 then assign B, If 3 then assign C Add to a wordstring (using string concatenation with .) We need 3 letters generated to give a 3 letter word For loop (3) Generate a number between 1 & 3 If 1 then assign A, If 2 then assign B, If 3 then assign C Add to a wordstring (string concatenation with .) End loop Print 3 letter word Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted February 17, 2011 Share Posted February 17, 2011 Extra credit way of getting it: $output = ''; $letters = array("A", "B", "C"); for($i = 1; $i <= 3; ++$i) { $output .= $letters[$i - 1]; } echo $output; The way that won't get you in trouble for cheating: $output = ''; for($i = 1; $i <= 3; ++$i) { if ($i == 1) { $output .= 'A'; } elseif ($i == 2) { $output .= 'B'; } else { $output .= 'C'; } } echo $output; Quote Link to comment Share on other sites More sharing options...
wazza91 Posted February 17, 2011 Author Share Posted February 17, 2011 thanks very much Nightslyr, there a second part you couldn't help me with. Now generate 50 words For loop (50) For loop (3) Generate a number between 1 & 3 If 1 then assign A, If 2 then assign B, If 3 then assign C Add to a wordstring End loop Check if word is ABC If so increment ABC count End Loop Print ABC Count Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted February 17, 2011 Share Posted February 17, 2011 I'm not going to do all your homework for you. You should be able to easily make 50 'ABC' words with minimal modification to what I already gave you. In fact, it takes only an additional 2-4 lines of code. Quote Link to comment Share on other sites More sharing options...
wazza91 Posted February 17, 2011 Author Share Posted February 17, 2011 ok fair enough, but what is the function i need to use for the ABC count Quote Link to comment Share on other sites More sharing options...
EmlynK Posted February 17, 2011 Share Posted February 17, 2011 Correct me if I'm wrong, but shouldn't the number generated be random? By what Nightslyr has done, the outcome will always be "ABC". For the second task, you will need to generate a random number between 1 and 3 using mt_rand(1,3) or any other function you prefer, and then use the IF statements. And then a final IF statement to see if the string is "ABC" and increment the ABC counter. Quote Link to comment Share on other sites More sharing options...
btherl Posted February 17, 2011 Share Posted February 17, 2011 You can create a new variable for the count. And increase the count by 1 each time you find "ABC". Here is a loop which does counting only: $count = 0; $for ($i = 0; $i < 100; $i++) { $count = $count + 1; } What you'll need to do is check if $output is "ABC", and only increase the count if it is. BTW, are you sure you're not being asked to generate a RANDOM number between 1 and 3? Also note that all of these will add 1 to count (they act differently when used as an "rval", but that doesn't matter for counting) $count = $count + 1; # is the same as $count += 1; # is the same as $count++; # is the same as ++$count; Quote Link to comment Share on other sites More sharing options...
wazza91 Posted February 17, 2011 Author Share Posted February 17, 2011 sorry if the seems a bit confusing heres a more detail description of the task. Q1 a. Create an html file with a textbox to enter numbers 0 to 6 to be posted to a php file. The number represents a guess for how many times a particular sequence of letters (ABC) will be randomly created b. Create a php file to generate a random 3 letter word whose letters are made up of A, B or C 50 times and check if the guess received from the html file is correct 1. receive the guess value and check that - The value lies between 0 & 6 and is a number –(using the is_numeric( ) function) 2. create 3 random numbers (each either 1, 2 or 3) using a for loop and rand function - check the value of each number as they are created (using an if statement) to assign the letter A for 1, B for 2, C for 3 to a letterstring variable - add the letter to a wordstring variable each time (using string concatenation with . [e.g. $wholename = $firstname . “ “ . $surname] - analagous to the way of creating a running total of numbers within a for loop using $total = $total + $number; ) ending up with a wordstring variable made up of 3 letters in random order c. Now create 50 different word strings using a for loop around the code for b2. - check the wordstring to see if “ABC” has been generated [ since it is random there are 27 different combinations of 3 letters that can be created: AAA, AAB, AAC,ABA, ABB, ABC, BAC,BBA,BBB,BBC,BCA,BCB, ACA,ACB,ACC,BAA,BAB, BCC,CAA,CAB,CAC,CBA,CBB,CBC,CCA,CCB,CCC ] - if “ABC” is created print “Found ABC string” & increment a count for ABC strings - print the guess value and the number of times the “ABC” sequence occurs - compare the guess with the actual number of times the “ABC” sequence occurs - print either “You have guessed correctly” or “Incorrect guess – try again!” depending on whether the guess matches the ABC count or not Quote Link to comment Share on other sites More sharing options...
EmlynK Posted February 18, 2011 Share Posted February 18, 2011 Thanks for more info, it's a lot clearer now. For question B2, you could use: $output = ""; $total = 0; for($i = 1; $i <= 3; $i++) { $num = rand(1,3); if($num == 1) { $output .= "A"; } elseif($num == 2) { $output .= "B"; } else { $output .= "C"; } $total = $total + $num; // or you could have $total += $num; } print $output; Then the 50 word one doesn't require much edit. Put another FOR loop around the code above, but using 50 instead of three and add a check to see if the output is equal to "ABC" and increment a counter using $counter++; or $counter += 1; or $counter = $counter + 1; Include the above bit after the 2nd FOR loop. I hope that helps. Quote Link to comment Share on other sites More sharing options...
Ethan Tremblay Posted February 22, 2011 Share Posted February 22, 2011 I have a similar question and i am failing to comprehend were the second for loop should be implemented and were the count function should be added!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.