Topics in this sectionAlgorithms2 topics
- Algorithm Fundamentals
- Translating Algorithms to Programs
An algorithm is a step-by-step plan that tells the computer exactly what to do with the input data to produce the desired output. Before writing any program, a programmer must first answer two important questions:
- What data must be fed into the computer? (the input)
- What result do we want the computer to give us? (the output)
Once you know the input and output, you can design the processing steps — the instructions that change the input into the output.
There are three common ways to show an algorithm before converting it into a program:
Flowchart — A diagram that uses special shapes and arrows to show the flow of a program. Each shape has a meaning. For example, an oval represents the start or end, a parallelogram represents input or output, and a diamond represents a decision (like a yes/no question).
Nassi-Schneidermann charts — These use rectangular boxes to show different types of program statements in a structured way.
Pseudocode — A verbal shorthand that looks like a programming language but does not follow strict rules. It uses plain English mixed with programming ideas to describe the steps clearly.
When you have your algorithm ready, you can translate it into a real programming language such as C++. This process involves:
- Writing the algorithm in flowchart or pseudocode form
- Replacing each step with valid C++ statements
- Adding proper syntax (the rules of the language)
- Compiling and running the program to test it
Example: The Quadratic Formula
A quadratic equation has the form ax2+bx+c=0. To find the solutions (roots), we use the quadratic formula:
x=2a−b±b2−4ac
The value inside the square root (b2−4ac) is called the discriminant. It tells us what kind of roots we will have:
- If the discriminant is greater than 0, there are two different real roots
- If the discriminant equals 0, both roots are the same (one real root)
- If the discriminant is less than 0, the roots are complex (have imaginary parts)
Here is how we translate this algorithm into a C++ program:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a, b, c, x1, x2, discriminant, realPart, imaginaryPart;
cout << "Enter coefficients a, b and c: ";
cin >> a >> b >> c;
discriminant = b*b - 4*a*c;
if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2*a);
x2 = (-b - sqrt(discriminant)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
x1 = (-b + sqrt(discriminant)) / (2*a);
cout << "x1 = x2 = " << x1 << endl;
}
else {
realPart = -b/(2*a);
imaginaryPart = sqrt(-discriminant)/(2*a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
}
return 0;
}
When you run this program and enter 1, 5, and 1 for a, b, and c, the output shows:
Enter coefficients a, b and c: 1 5 1
Roots are real and different.
x1 = -0.208712
x2 = -4.79129
Compilation — The process of converting your C++ code into machine language that the computer can understand. If your code has errors (mistakes), the compiler will tell you so you can fix them.
Execution — Running the compiled program to see the results.
Program statement — A single instruction in a program, such as asking for input, performing a calculation, or displaying output.
When you translate an algorithm into code, each step in your flowchart or pseudocode becomes one or more program statements. The key programming constructs you will use include:
- Input statements — to read data from the user (like
cin >> a;) - Variables — containers to store numbers and text (like
double a, b, c;) - Arithmetic operations — to perform calculations (like
b*b - 4*a*c) - Decision structures — the
ifandelsestatements that choose different actions based on conditions - Output statements — to display results (like
cout << x1;)
In South Sudan, understanding how to translate algorithms into programs is useful even for small-scale businesses. For example, a market trader in Juba who sells sorghum and millet could use a simple program to calculate profit margins — input the purchase price and selling price, let the program calculate the difference, and display the profit. This same skill is used by technicians at the Nile Petroleum Corporation in Juba to write programs that monitor equipment performance and predict when maintenance is needed.
Question
What is an algorithm?
Sign in to submit your answer and have it count toward your mastery.
Sign in to practiceTutor
Stuck? Ask me anything about this topic.
Sign in to ask the Sonza AI Tutor about this question.
Sign in to askDiscussions
No questions yet