Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/228011-php-loops/
Share on other sites

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;

Link to comment
https://forums.phpfreaks.com/topic/228011-php-loops/#findComment-1175768
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/228011-php-loops/#findComment-1175797
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/228011-php-loops/#findComment-1175815
Share on other sites

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;

Link to comment
https://forums.phpfreaks.com/topic/228011-php-loops/#findComment-1175816
Share on other sites

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/228011-php-loops/#findComment-1175831
Share on other sites

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. :)

Link to comment
https://forums.phpfreaks.com/topic/228011-php-loops/#findComment-1176137
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.