Starting from:

$30

CSIS3280 - Assignment 1 - Solved

You are asked to write a PHP program to generate randomly an algebraic expression. If you forgot what an algebraic expression is, this is an example of an algebraic expression: 6x -5 + 7x – 3x2 . You can read more about the algebraic expressions at Wikipedia.

The main idea is to generate randomly an algebraic exprssion with mutiple terms, then a student will simplify that expression. For example, a student can simplify 6x -5 + 7x – 3x2    to     -3x2 + 13x – 5 

In fact, the goal is to write a web app that students can use to practice simiplification of algebraic expressions.

In assignment1, your taks is only to generate the expression. You do not need to work on the checking whether the answer of the user is correct or not.

Requirement: 

1.       Create a php file and call it lib.php. In this file, define two functions:

a.       display_html_head: this function has 1 input parameter which is the title of the HTML file. The function does not return any value. In this function, use HEREdoc to print doctype, <html>, <head>, <title>, and <body> of a HTML file. Note that when you call this function, you must pass the title of the document that you want to display inside <title> and </title> tags

b.       display_html_foot: this function does not have any input parameter and it does not return any value. Using a HEREdoc, print end tags for </body> and </html>

2.       Create another php file, as your main php code, and use require to add lib.php to this main code.

3.       Define a global variable called degree. This variable stores the degree of the algebraic expression that we are going to generate (the greatest exponent in an algebraic expression is the degree of that expression. For example, degree of x+3 is 1 and the degree of x2 + 3x - 1 is 2). We want to write a program that by changing the value of this variable, the program automatically generates an expression of that degree. Therefore, the final program will be fully parametric based on the degree parameter.

4.       Define another global variable called num_of_terms. This variable stores the number of the terms that we want to generate in the expression. Num_of_terms depends on the degree of the expression. We use the following formula:

Num_of_terms = 2 x degree + 3 

It means that if the degree is 1, we will generate 5 terms (e.g.  9x-1+x-5x+1). If the degree is  2, we generate 7 terms (e.g.  x+14x2-6+x2-2x-8x2-4)

5.       Define a function called generate_expression. This function has only 1 input parameter, degree, and it returns a two-dimensional array called terms: in the first row of this 2D array we store coefficients and in the second row we store exponents.

In this function use rand() pre-defined function of PHP to generate a random number for coefficients and exponents of the terms. Coefficient can be between -20 and 20 excluding 0 (a coefficient cannot be zero). Exponents can be from 0 to degree value. For example, if the degree is 2, exponents can be from 0 to 2 (i.e., 0, 1, and 2).

6.       Define a function called meets_requirements. This function checks if the generated expression meets some requirements. This function has 2 input parameters, 2D-array terms, and degree, and it returns a Boolean value. To make the generated expression interesting for practice of the student, we require that there must be at least 2 terms from each degree in our expression. For example, the expression 2x + 3 – 1 + 5 – 2 does not meet the requirement because there is 1 term of degree 1 (2x) and there are 4 terms of degree 0 (3, -1, 5, and -2). But 2x + 3 – 1 + 5x – 2 meets the requirement because there are at least two terms of degree 1, (2x and 5x) and three terms of degree 0 (3, -1, and -2). Note that your function must work for all degrees. It means if the current value of the global variable degree is 3, the generated expression meets requirements if there are at least 2 terms of degree 3, at least 2 terms of degree 2,  at least 2 terms of degree 1 and at least 2 terms of degree 0. For example, 2x3-5x+3-7x2+x-4x3-6+3x2+5  meets requirements, and your function must return true for this expression.

7.       Define a function called print_expression. This function receives the 2D array of terms as input and prints the formatted expression on the HTML file. This function does not return any value. Some point to implement in your print_expression function:

 

a.       If the first term is positive, we do not put a plus sign before it. For example, we do not print +3 – 5x + 7 instead we print 3 – 5x + 7, however if the first term is negative, we display minus sign: -6x + 6 - 2x

b.       When a coefficient is one, it is omitted (e.g.  1x2 is written x2).

c.       When the exponent (power) is one, it is omitted (e.g., x1 is written x)

d.       When the exponent is zero, the result is always 1 (e.g., 3x0 is written 3, since x0 is always 1).

e.       You can use <sup> HTML tag for displaying an exponent.

f.        You can use abs predefined function for finding absolute value of a coefficient.

8.       Define a function called display_expression. This function has one parameter, degree, and it does not return any value. The task of this function is to generate an expression and then verify if it meets the requirements. If it meets, display it, else repeat until an expression that meets the requirement is generated. In this function, you call generate_expression, meets_requirements, and print_expression inside a loop.

9.       On top of your code, after the global variables, call display_html_head, display_expression, and display_html_foot functions. The details of how to call these functions with passing values to them is left to you.

10.   IMPORANT: you must write your program such that by changing ONLY THE VALUE of degree global variable, your program generates a random expression of that degree. (You must not change any other part of your code.)

Screenshots 

Some screenshots for degree=0:

  

Some screenshots for degree=1:

  

Some screenshots for degree=2:

 

Some screenshots for degree=3:

  

 

More products