Am interested in IT but am not a computer scientist, am only teaching my self. Please i have a problem with algorithm, can you give me tips on how to go about it. here is th problem am trying to solve:
A quadratic equation can be written in the form, Ax2+Bx+C=0. Develop an algorithm and draw a flowchart to read the coefficients of A, B, and C. Calculate the roots and print out.

This is a source code example to calculate that expression with any value, you can test it at http://agec.ath.cx/eq, two functions to calculate each ± result, and one condition to put the two values (the real one and the imaginary) in the imaginary case (b < 4ac), tomorrow I'll inspect the code for errors and I'll add the graph.

``````<?php

/**
* @author Martin Caminoa
*/

function eq_plus(\$a, \$b, \$c){
\$value=array();

if (\$b < (4*a*c)){

\$value[0] = (-1*-\$b) / (2*\$a);
\$value[1] = ((sqr(\$b + (4*\$a*\$c))) / (2*\$a));
}
else
{
\$value[0] = ((-\$b + sqr(\$b - (4*\$a*\$c))) / (2*\$a));
}

return \$value;
}

function eq_minus(\$a, \$b, \$c){
\$value=array();

if (\$b < (4*a*c)){

\$value[0] = ((-1*-\$b) / (2*\$a));
\$value[1] = ((sqr(\$b - (4*\$a*\$c))) / (2*\$a));
}
else
{
\$value[0] = ((-\$b - sqr(\$b - (4*\$a*\$c))) / (2*\$a));
}

return \$value;
}

// get the square of a number
function sqr(\$value, \$power = 2){      // \$power = 2 default value

for(\$i = 1; \$i < \$power; \$i++){
\$value = \$value * \$value;
}
return \$value;
}

\$a=\$_GET['a'];
\$b=\$_GET['b'];
\$c=\$_GET['c'];

// display results
if(\$b < (4*a*c)){
echo "Results in imaginary numbers<br />";
\$result = eq_plus(\$a,\$b,\$c);
echo "+ = ". number_format(\$result[0], 2);
echo " i". number_format(\$result[1], 2) ."<br />";
\$result = eq_minus(\$a,\$b,\$c);
echo "- = ". number_format(\$result[0], 2);
echo " i". number_format(\$result[1], 2) ."<br /><br />";
}

if(\$b > (4*a*c)){
echo "Results in real numbers<br />";
\$result = eq_plus(\$a, \$b, \$c);
echo "+ = ". number_format(\$result[0], 2) ."<br />";
\$result = eq_minus(\$a, \$b, \$c);
echo "- = ". number_format(\$result[0], 2) ."<br /><br />";

}

// show input form
\$form = "<form action=\"index.php\" method=\"GET\">";
\$form .= "a <input type=\"text\" name=\"a\" />&nbsp;";
\$form .= "b <input type=\"text\" name=\"b\" />&nbsp;";
\$form .= "c <input type=\"text\" name=\"c\" /><br /><br />";
\$form .= "<input type=\"submit\" /></form>";

echo \$form;

?>``````

Yeah the only real problem is with complex numbers.

Thanks for your help but i just got started and cant really understand all those jargons. I've not really entered into programming am still an introduction on how to write algorithm not program. if you don't mind please use simply language, i can draw the chart but i don't understand algorithm.

Pseudo language reminds me to learn another language :) The code have some errors, like the absence of the square root and another condition to put: (sqr(b) - (4ac)) have negative value (nan if I do the square root). Basically, there three functions, one to calculate the square because I haven't a built-in function in PHP to do this, and two functions with the same resemblance to calculate with the quadratic formula ((-b ± sqrt( sqr(b) - (4ac) )) / (2a)).

I use parenthesis to make the order of precedence of the operations and yes, the logic will be more complex dealing with complex numbers.
Each function return the values in a array. An array is a container of data. So, in this way, I can save the two terms of the imaginary result and display then.

The part below the functions there are to display the results. First, to get the parameters sended by the form and store in variables. Then, execute the functions with the parameters obtained. Now then, a condition to show imaginary or real results and finally, the form to input the data. I'm assigning strings to a variable and concatening another strings, then I show the variable with all strings using HTML code.

I have enclosed the new source code, maybe it is more clear.

Attachments
``````<?php

/**
* @author Martin Caminoa
*/

function eq_plus(\$a, \$b, \$c)
{
\$value = array();

if (\$b < (4*a*c))
{
\$value[0] = (-\$b) / (2*\$a);
\$value[1] = (sqrt(sqr(\$b) - (4*\$a*\$c)) / (2*\$a));
}
else
{
if ((sqr(\$b) - (4*\$a*\$c)) < 0)			// if it is a negative number
{
\$value[0] = ((-\$b + sqrt( -1*(sqr(\$b) - (4*\$a*\$c)) )) / (2*\$a));
}
else
{
\$value[0] = ((-\$b + sqrt( sqr(\$b) - (4*\$a*\$c) )) / (2*\$a));
}
}

return \$value;
}

function eq_minus(\$a, \$b, \$c)
{
\$value = array();

if (\$b < (4*a*c))
{
\$value[0] = ((-\$b) / (2*\$a));
\$value[1] = (sqrt(-1*sqr(\$b) - (4*\$a*\$c)) / (2*\$a));
}
else
{
if ((sqr(\$b) - (4*\$a*\$c)) < 0)
{
\$value[0] = ((-\$b - sqrt( -1*(sqr(\$b) - (4*\$a*\$c)) )) / (2*\$a));
}
else
{
\$value[0] = ((-\$b - sqrt( sqr(\$b) - (4*\$a*\$c) )) / (2*\$a));
}
}

return \$value;
}

// get the square of a number
function sqr(\$value, \$power = 2)			// \$power = 2 default value
{
for(\$i = 1; \$i < \$power; \$i++)
{
\$value = \$value * \$value;
}
return \$value;
}

\$a=\$_GET['a'];
\$b=\$_GET['b'];
\$c=\$_GET['c'];

// display results
\$result_plus = eq_plus(\$a, \$b, \$c);
\$result_minus = eq_minus(\$a,\$b,\$c);

if(\$b < (4*a*c))
{
\$result = "Results in imaginary numbers<br />";
\$result .= "+ = ". number_format(\$result_plus[0], 3) ."&nbsp;";
\$result .= number_format(\$result[1], 2) ."i<br />";
\$result .= "- = ". number_format(\$result_minus[0], 3) ."&nbsp;";
\$result .= number_format(\$result[1], 2) ."i<br /><br />";
}
else
{
\$result = "Results in real numbers<br />";
\$result .= "+ = ". number_format(\$result_plus[0], 3) ."<br />";
\$result .= "- = ". number_format(\$result_minus[0], 3) ."<br /><br />";
}

echo \$result;

// show input form
\$form = "<form action=\"index.php\" method=\"GET\">";
\$form .= "a <input type=\"text\" name=\"a\" value=\"".\$_GET['a']."\" />&nbsp;";
\$form .= "b <input type=\"text\" name=\"b\" value=\"".\$_GET['b']."\" />&nbsp;";
\$form .= "c <input type=\"text\" name=\"c\" value=\"".\$_GET['c']."\" /><br /><br />";
\$form .= "<input type=\"submit\" /></form>";

echo \$form;

?>``````

This is the last corrected version, tested the results with Mathematica from Wolfram Research.

Attachments
``````<?php

/**
* @author Martin Caminoa
*/

function eq_plus(\$a, \$b, \$c)
{
\$value = array();

if ((sqr(\$b) - (4*\$a*\$c)) < 0)					// two imaginary square roots
{
\$value[0] = (-\$b) / (2*\$a);					// real term
\$value[1] = ((sqrt(- sqr(\$b) + (4*\$a*\$c) )) / (2*\$a));	// imaginary term
}
elseif ((sqr(\$b) - (4*\$a*\$c)) == 0)				// two real and equal square roots
{
\$value[0] = (-\$b) / (2*\$a);
}
else											// two real square roots
{
\$value[0] = ((-\$b + sqrt( sqr(\$b) - (4*\$a*\$c) )) / (2*\$a));
}

return \$value;
}

function eq_minus(\$a, \$b, \$c)
{
\$value = array();

if ((sqr(\$b) - (4*\$a*\$c)) < 0)					// two imaginary square roots
{
\$value[0] = (-\$b) / (2*\$a);					// real term
\$value[1] = ((sqrt(- sqr(\$b) + (4*\$a*\$c) )) / (2*\$a));	// imaginary term
}
elseif ((sqr(\$b) - (4*a*c)) == 0)				// two real and equal square roots
{
\$value[0] = (-\$b) / (2*\$a);
}
else											// two real square roots
{
\$value[0] = ((-\$b - sqrt( sqr(\$b) - (4*\$a*\$c) )) / (2*\$a));
}

return \$value;
}

// get the square of a number
function sqr(\$value, \$power = 2)			// \$power = 2 default value
{
for(\$i = 1; \$i < \$power; \$i++)
{
\$value = \$value * \$value;
}
return \$value;
}

\$a=\$_GET['a'];
\$b=\$_GET['b'];
\$c=\$_GET['c'];

if (\$_GET['a'] != ""){
// display results
\$result_plus = eq_plus(\$a, \$b, \$c);
\$result_minus = eq_minus(\$a,\$b,\$c);

if ((sqr(\$b) - (4*\$a*\$c)) < 0)
{
\$result = "Results in imaginary numbers<br />";
\$result .= "x1 = ". number_format(\$result_plus[0], 5) ."&nbsp;";
\$result .= " + i ". number_format(\$result_plus[1], 5) ."<br />";
\$result .= "x2 = ". number_format(\$result_minus[0], 5) ."&nbsp;";
\$result .= " - i ". number_format(\$result_minus[1], 5) ."<br /><br />";
}
else
{
\$result = "Results in real numbers<br />";
\$result .= "x1 = ". number_format(\$result_plus[0], 5) ."<br />";
\$result .= "x2 = ". number_format(\$result_minus[0], 5) ."<br /><br />";
}

echo \$result;
}

// show input form
\$form = "<form action=\"index.php\" method=\"GET\">";
\$form .= "a <input type=\"text\" name=\"a\" value=\"".\$_GET['a']."\" />&nbsp;";
\$form .= "b <input type=\"text\" name=\"b\" value=\"".\$_GET['b']."\" />&nbsp;";
\$form .= "c <input type=\"text\" name=\"c\" value=\"".\$_GET['c']."\" /><br /><br />";
\$form .= "<input type=\"submit\" /></form>";

echo \$form;

?>``````

This is the last corrected version, tested the results with Mathematica from Wolfram Research. The first version is like unkempt - very ugly.

First, if you have to deal with an algorithm, build one, you will have to separate the business logic (like functions to do math operations in this occasion) from the user interface. We have two problems. I recommend start with the business logic always.

We use variables or arrays to contain the values. Both are the same, array can be defined as a variable conjunct, each one with an index to make more easy the location. I'm using the symbol equal to assign the result of the operation (with variables) to unique array index, separating the terms.

Given the parameters by the function, I use conditions to define an action in accordance with an event. In this case, we know that I can't apply the same formula with any given value.

In the square function of a number, I'm using a controlled loop to make a repetitive task of multiply a parameter by itself given the number of times by another parameter too (in this case, parameter can be omitted, have a default value, two times).

I don't know how to put simpler, but in almost every language you can do math operations easily like with a calculator. The problem growth when you don't have built-in functions in the language to make operations, you have the possibility to use more specialized libraries and collection of objects from third-party sometimes. But if it doesn't exist you will need to create one using only the existent functions like these.

Display the result is easier, you will have to put format to the given data, like carriage returns (or line feed). I'm assigning the array results and format properties to a variable and then I use the echo function to display the variable content.

I hope that you have a better vision of an algorithm. I do not use a simple language in my life, I think in minimal or by part, then I apply the interpretation directly using the final language.

Hi- can anybody help
Write an algorithm using a procedure called quadratic for a program that solve a quadratic equation of the form ax(2) + bx + c = 0

Thx
Jessika

An array called MARK stores the marks of 20 pupils in mathematics.
(i) Write an algorithm that uses a while loop to input marks of 20 pupils, in the array.
(ii) Assuming that no two pupils are allocated equal marks, search the array for a mark of
50 and print it.
(iii)Modify a mark of 65 from the array, so that the new mark is 75.

i am just in the 10th grade. i have no idea of what is written on this page. please let's start from basics. thank you for trying.

It seems like this topic has veered way off.

#1: What is an algorithm?

An 'algorithm' is an effective method for solving a problem expressed as a finite sequence of instructions

An algorithm can be expressed by: natural language (see example below), flow charts, psuedocode, control tables, etcetera.

#2 How do I go about writing an algorithm?
Let's go about writing a basic algorithm for the problem you wrote:

A quadratic equation can be written in the form, Ax2+Bx+C=0. Develop an algorithm and draw a flowchart to read the coefficients of A, B, and C. Calculate the roots and print out.

** You should have an understanding of the quadratic equation before beginning this.

Okay, first lets start by listing the inputs and outputs we need to solve our problem.

Wait!!!! What is an input and an output!?
Well, an input is information we need to gather in order to process our problem, this is information supplied by the person who wants you to solve their problem, for the purpose of this problem our inputs will be the A, B, and C coefficients. An output is the information that we give back to the person as we process the problem. For this problem our outputs will be the two roots.

Quadratic Equation Algorithm (Natural Language: English)
--------------------------------------------------------

Inputs: A coefficient, B coefficient, C coefficient
Outputs: first root, second root

Now that we have our inputs and outputs we will begin processing our problem. Remember were not actually trying to do any of the math right now, we are just writing out the steps needed to solve the problem!

Let's start by collecting our inputs:

Quadratic Equation Algorithm (Natural Language: English)
--------------------------------------------------------

Inputs: A coefficient, B coefficient, C coefficient
Outputs: first root, second root

1) Request that the user input the A coefficient.
-> Store the A coefficient for later use.

2) Request that the user input the B coefficient.
-> Store the B coefficient for later use.

3) Request that the user input the C coefficient.
-> Store the C coefficient for later use.

Now that we have all of our inputs (coefficients) stored (written down) we can begin processing our problem. Let's do this by finding our first root using the quadratic equation.

Quadratic Equation Algorithm (Natural Language: English)
--------------------------------------------------------

Inputs: A coefficient, B coefficient, C coefficient
Outputs: first root, second root

1) Request that the user input the A coefficient.
-> Store the A coefficient for later use.

2) Request that the user input the B coefficient.
-> Store the B coefficient for later use.

3) Request that the user input the C coefficient.
-> Store the C coefficient for later use.

4) Set up the first root equation: ((-B + SquareRoot(B^2 - 4AC)) / (2A))
-> Process the equation and store the result as the first root for later use.

Now to get the second root!

Quadratic Equation Algorithm (Natural Language: English)
--------------------------------------------------------

Inputs: A coefficient, B coefficient, C coefficient
Outputs: first root, second root

1) Request that the user input the A coefficient.
-> Store the A coefficient for later use.

2) Request that the user input the B coefficient.
-> Store the B coefficient for later use.

3) Request that the user input the C coefficient.
-> Store the C coefficient for later use.

4) Set up the first root equation: ((-B + SquareRoot(B^2 - 4AC)) / (2A))
-> Process the equation and store the result as the first root for later use.

5) Set up the second root equation: ((-B - SquareRoot(B^2 - 4AC)) / (2A))
-> Process the equation and store the result as the second root for later use.

Now that we have both of the roots we can "print out" our outputs to the person. Since I don't have a square root symbol on my keyboard I used the expression SquareRoot.

Quadratic Equation Algorithm (Natural Language: English)
--------------------------------------------------------

Inputs: A coefficient, B coefficient, C coefficient
Outputs: first root, second root

1) Request that the user input the A coefficient.
-> Store the A coefficient for later use.

2) Request that the user input the B coefficient.
-> Store the B coefficient for later use.

3) Request that the user input the C coefficient.
-> Store the C coefficient for later use.

4) Set up the first root equation: ((-B + SquareRoot(B^2 - 4AC)) / (2A))
-> Process the equation and store the result as the first root for later use.

5) Set up the second root equation: ((-B - SquareRoot(B^2 - 4AC)) / (2A))
-> Process the equation and store the result as the second root for later use.

6) Display both root solutions to the user.

With this type of problem you can pretty much go with a straight list of steps; however, with more complex problems come more complex algorithms. You may not always be able to go with a straight list and this is why flow charts and control tables are great. I hope that this helped you a bit.

-- Vexx

natural language (see example below),

It's called "English," <<snip>>, do you speak it? Sorry, couldn't resist. [1]

flow charts,

<<snip>>

psuedocode

<<snip>>

control tables

<<snip>>

.
.

What I don't get is you talk about horrible ways of representing algorithms. Here is the tried and true good way to represent algorithms: source code! Note that "pseudo-code" is an archaic practice from the time when programming languages all required excess book-keeping or manual memory management or other bureaucracy. Pick a friendly language (Python, or Haskell) and write source code. Or if pseudo-code suits the purpose, go with that. But Python is executable pseudo-code, and Haskell is executable mathematics.

#2 How do I go about writing an algorithm?

...

Okay, first lets start by listing the inputs and outputs we need to solve our problem.

This is the most bureaucratic piece of bullhockey that I have ever seen. [2]

Wait!!!! What is an input and an output!?

I am in 10th grade and what is this.

With this type of problem you can pretty much go with a straight list of steps;

Or he could kill himself. This is the fallacy of the excluded middle. There is a third alternative! Read on to read it!

however, with more complex problems come more complex algorithms. You may not always be able to go with a straight list and this is why flow charts and control tables are great.

Flow charts and control tables are not great. Stop smoking the pot.

.

.

Now. How do we specify an algorithm for solving the quadratic equation?

``````-- finds solutions to ax^2 + bx + c = 0
solveQuadratic(a, b, c) = let d = sqrt(b^2 - 4*a*c)
in ((-b + d) / (2 * a), (-b - d) / (2 * a))``````

That's how!

But wait! I am confused. We didn't tell the user to input values!

That's because that's retarded. We're just describing an algorithm for computing the roots of a quadratic equation. There's no reason to muddle that up with some console-based user interface.

But wait! I am confused. We didn't tell the computer to set up our equations!

That's because that's retarded. We never tell the computer to set up our equations. We just say what they are.

-> Process the equation and store the result as the first root for later use.

Why would you even write a sentence like that?

-- Vexx

[1] I'm a superconductor!

[2] I am *hitting on you because you are giving a complete misrepresentation of what programming is like.

Samuel, do you have a sister? I would make love to her if her writings are adept as yours. Mwah x
Completely useless post, only trolling

Wow Mr. Troll,
Way to be a complete ###. I did my best to show him how to express an algorithm in English because it seems that psuedocode and source it out of the question, learn to read:

Thanks for your help but i just got started and cant really understand all those jargons. I've not really entered into programming am still an introduction on how to write algorithm not program. if you don't mind please use simply language, i can draw the chart but i don't understand algorithm.

How else would you have it expressed in text? Here's a thought: NOT everyone has the same level of experience as you so you can't just drop a source bomb on someone who doesn't know programming and expect them to know whats going on. In response to your rant on flow charts and control tables: You are a moron. Flow charts are a huge deal. They provide a detailed explanation of what the program is trying to accomplish and how it accomplishes it by mapping everything out, which allows someone who is unfamiliar with programming to have a complete understanding of the algorithm. I'm not even going to go any further into this because you probably won't bother reading it (which is why you thought it was okay to drop source on the OP anyhow). That big wall of text you typed out didn't have one single useful statement in it, congrats.

--Vexx

I like flowcharts. Flowcharts are gooood.

Wow Mr. Troll,

I am not a troll. There is a more reasonable explanation for my response, the possibility of which you have not allowed yourself to conceive, which is that your post was a bad post, bad for the reader and inappropriate for the situation.

How else would you have it expressed in text?

You didn't express it in text. You used equations. You showed yourself that the most easy way for you to express the algorithm was with equations.

NOT everyone has the same level of experience as you so you can't just drop a source bomb on someone who doesn't know programming and expect them to know whats going on.

Agreed. That's why I didn't drop a source bomb. You'll notice that it was a few lines. You might also notice that I was responding to you. You didn't need a bunch of exposition.

Flow charts are a huge deal.

No, they are not. Open up any textbook related to programming or computer science. How many flow charts do you see? They never use flow charts. They specify algorithms using pseudocode or real code, and they describe what the algorithms do using prose. Let's pull out some books at random, and see what they use for describing algorithms.

Cryptography Engineering uses lots of pseudocode, mathematical notation, and informal sequence diagrams for describing protocols.

Programming Language Pragmatics uses tons of pseudocode, tons real code, and has state machine diagrams, expression grammars, and, hmm, a couple of flow charts showing the phases of compilation.

The D Programming Language has no flow charts. But it does have a few state machine diagrams, and tons of D.

Transactional Information Systems has plenty of code and diagrams, and, hmm, there's a "stochastic state-transition model." I see something else that might be called a flow chart, superficially, but it's not used to describe an algorithm.

Cryptonomicon has no flow charts, and one Perl code snippet.

They provide a detailed explanation of what the program is trying to accomplish and how it accomplishes it by mapping everything out, which allows someone who is unfamiliar with programming to have a complete understanding of the algorithm.

That's only the case when an algorithm is so simplistic that a flow chart is capable of giving a complete understanding. The vast majority of flow charts are there to give a general description of the order in which stuff happens, not a complete description.

--Vexx

Don't self-sign your posts. People will think you're carrying your ego into the discussion.

You