Jump to content

Pseudocodes in PHP


fayerie13

Recommended Posts

Please kindly help with pseudocodes for the following - I do not know where to start:

 

- An algorithm to calculate the sum of even and odd numbers less than 10 and store the values in EVEN and ODD respectively:

 

- An algorithm to calculate the product of all the numbers greater than 0 and less than 5.

 

- An algorithm to calculate how many days are in a given number of years. ie. for 2 years, the number of days equals to 730

Link to comment
Share on other sites

Do you understand what pseudo-code is? What have you tried? Where did you get stuck?

@Guru  I understand that pseudo code is a notation resembling a simplified programming language, used in program design.this is what I have so far - I am not sure if I am going the right direction though:

 

//an algorithm to calculate the average of any number of integers
$array = set of numbers
average = sum $array / count numbers $array
print average result
 
//calculate sum of even and odd numbers less than 10 and store values into even and odd respectively
$number = 0
 
for function (number = 0; work with numbers <10; increment number){
if ($number is divisible by 2)
print 'number is even'
}
else
print 'number is odd'
 
number = range between 0 - 10
print sum of numbers
 
//calculate the product of all the numbers greater than 0 and less than 5
I don't know whether to use the while loop or the for loop for this?
 
 
//calculate how many days are given in number of years
function to get how many days in (certain year)
 
number of days = array of numbers
total month = 12
if ($year) is equal to date (y)
total month = date (m)
else
total month is = 12
 
use for loop function (month = 1; m is less than and equal to total month; increment m)
number of days = calculate days in a month using cal_gregorian function, m, $year
 
return number of days
 
number of days = get days for (certain year)
use print_r(number of days)
Link to comment
Share on other sites

I think some of your solutions miss the point, and some are simply wrong.

 

The point of your exercise is to write a concrete algorithm which does exactly what the description says (and nothing more). So when you're asked to store the sum of odd numbers in ODD and the sum of even numbers in EVEN, they clearly don't want you to write print statements or vague “sum of numbers” commands. They want the specific steps:

// initialize variables for sums
odd := 0
even := 0

// calculate sums
for i from 1 to 9:
  if i is odd:
    odd := odd + i
  else:
    even := even + i
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.