Jump to content

Newbie here and need help checking basic PHP work


zatox123

Recommended Posts

Hey so I am a complete newbie and am taking a course to teach me php. I was given some practice problems to do but I was not given any answers to see if my work was correct. Can anyone check my work and/or give me some tips or answers to these problems? I would greatly appreciate it. 

 

Assume that an array named $list is given to you. It contains ten numbers in cells 0 through 9. Some of the numbers are negative. You don't need to waste time writing the code to create $list. Assume it exists already. It might not contain the same numbers as the example below. Write a small piece of code including a  'for' loop that would add up all the numbers in the $list which are greater than or equal to 4, and then print "The sum of biggies is ", followed by the sum. For instance, if the list is (8, 1, 7, -7, 2, 4, 5, -14, 0, 1) then the sum of the biggies is 24. Your code will also print out "There were $n zeros and $m biggies.", where $n is replaced by the number of zeros found in the list, and $m is the number of values greater than or equal to 4.  In our example, it would print “There were 1 zeros and 4 biggies.”

#list=array(8,1,7,-7,2,4,5,-14,0,1)
$for ($i=0;$i<10;$i++)
   {
     $x=$list[$i];
     if ($x>4)
   {
     $total=$total+$x;
     $biggies++;//biggies=$biggies+1;
   }
     else if ($x==0)
   {
     $zeros++;
   ]
     print "The sum of biggies is $biggies


That is where I got stuck, I didn't really get the second part >< 

Edited by zatox123
Link to comment
Share on other sites

1) You have your $list commented out. Uncomment it (remove the #). Also it should be $list not list also you are missing the semicolon at the end of it.

 

2) it should be for not $for (no dollar sign)

 

3) line 5: you need to use >= the instructions say greater than or equal to 4

 

4) you are using a ] instead of } on line 13

 

5) line 14 should use $total not $biggies

 

6) line 14 missing a closing quote and semicolon

 

7) you need another print that shows how many zeros and biggies. You already have the vars incremented..just print them out as a message

 

8 )you are missing a closing } at the end, for your for loop. this should come before your print statements

Link to comment
Share on other sites

Also, your indentation is off. It's not necessary but would make the code clearer. For example, you probably would have caught the missing closing bracket if it was indented properly. This is what it should look like:

 

$list=array(8,1,7,-7,2,4,5,-14,0,1);
for ($i=0;$i<10;$i++)
{
  $x=$list[$i];
  if ($x=>4)
  {
    $total=$total+$x;
    $biggies++;//biggies=$biggies+1;
  }
  else if ($x==0)
  {
    $zeros++;
  }
}

print "The sum of biggies is $total . ";
print "There were $zeros zeros and $biggies biggies.";
A better way to write this:

 

 

// good practice to initialize your vars
$list=array(8,1,7,-7,2,4,5,-14,0,1);
$total=0;
$biggies=0;
$zeros=0;

// a foreach will iterate through the whole array without having to code in the array size
// alternatively, you can use count($list) instead of hardcoding 10 if you want to stick to the for loop
foreach ($list as $num) {
  // switch is more cleaner looking than if..elseif..elseif..etc..
  switch (true) {
    case ($num>=4):
      $total+=$num; // shorthand operator for adding to itself
      $biggies++;
      break;
    case ($num==0):
      $zeros++;
      break;
  } // end switch
}

print "The sum of biggies is $total .";
print "There were $zeros zeros and $biggies biggies.";
Link to comment
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.