Define a function called display_expression

Assignment Help Computer Engineering
Reference no: EM133368949

Write 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

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

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

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 * 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.
For example: for the following randomly generated expression, the 2d-array below is filled in this function:

x + 14x2 - 6 + x2 - 2x - 8x2 - 4

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

Reference no: EM133368949

Questions Cloud

Critically discuss how big data and data science can assist : critically discuss how "Big Data" and "Data Science" can assist in reducing the congestion at the port.
West african interior to north african coast : AD 300 Trade from the West African Interior to the North African Coast
Non-retraction controversy to the filipino nation : What was the effect of Rizal's retraction or non-retraction controversy to the Filipino nation? Discuss.
How will the shareholders be affected : How will the shareholders be affected? How will this affect the future ability of the two firms to raise funds?
Define a function called display_expression : Define a function called display_expression. This function has one parameter, degree, and it does not return any value. The task of this function
How long has tom worked for jackson enterprise : Tom originally started to work for Jackson enterprise at an annual salary of $36500 today Tom earns $68200 Tom calculated that his average annual pay raises
Concluded from archaeological ruin : Because many early African societies did not leave behind written records, much of what we know about these societies must be concluded from archaeological ruin
Describe thoughts on the rise of infectious diseases : Describe your thoughts on the rise of infectious diseases and the threat of more frequent pandemics. What might we do to prevent these diseases?
How much do you need to contribute this year to afford : You will withdraw your retirement spending at the end of each year. So your first retirement withdrawal will be at the end of year 31. Assume you will live for

Reviews

Write a Review

Computer Engineering Questions & Answers

  Mathematics in computing

Binary search tree, and postorder and preorder traversal Determine the shortest path in Graph

  Ict governance

ICT is defined as the term of Information and communication technologies, it is diverse set of technical tools and resources used by the government agencies to communicate and produce, circulate, store, and manage all information.

  Implementation of memory management

Assignment covers the following eight topics and explore the implementation of memory management, processes and threads.

  Realize business and organizational data storage

Realize business and organizational data storage and fast access times are much more important than they have ever been. Compare and contrast magnetic tapes, magnetic disks, optical discs

  What is the protocol overhead

What are the advantages of using a compiled language over an interpreted one? Under what circumstances would you select to use an interpreted language?

  Implementation of memory management

Paper describes about memory management. How memory is used in executing programs and its critical support for applications.

  Define open and closed loop control systems

Define open and closed loop cotrol systems.Explain difference between time varying and time invariant control system wth suitable example.

  Prepare a proposal to deploy windows server

Prepare a proposal to deploy Windows Server onto an existing network based on the provided scenario.

  Security policy document project

Analyze security requirements and develop a security policy

  Write a procedure that produces independent stack objects

Write a procedure (make-stack) that produces independent stack objects, using a message-passing style, e.g.

  Define a suitable functional unit

Define a suitable functional unit for a comparative study between two different types of paint.

  Calculate yield to maturity and bond prices

Calculate yield to maturity (YTM) and bond prices

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd