C/C++ Program for Polynomial Solution without Formula, Numerical Analysis
Determine whether the following quadratic equation has a root between -1.0 and +1.0. If it exists, print that root to an accuracy of two decimal places. Do not use the quadratic formula. The equation is: 4x2 + 10x - 9 = 0
#include <iostream> using namespace std; int main() { int counter = 0; // start from x = -1.0f float x = -1.0f; while(true) { // increment by 0.001 // small steps -1.0, -.999, -.998, and so on x += 0.001f; // check after 100 increments if(counter++ == 100) { // if x has become larger than // 1 quit if(x > 1.0f) { cout << "No solution in -1 and +1"; return 0; } // set increment counter to zero counter = 0; } cout << "x = " << x << endl; // caculate the equation expression float equation = (4 * x * x) + (10 * x) - 9; // make absolute, positive if(equation < 0) { equation *= -1; } // if it is almost zero // we have the solution if(equation < 0.01) { cout << "root = " << x << endl; break; } } return 0; }
My C/C++ Videos on Youtube
Here is the complete playlist for video lectures and tutorials for the absolute beginners. The language has been kept simple so that anybody can easily understand them. I have avoided complex jargon in these videos.
This Blog Post/Article "Program for Polynomial Solution without Formula, Numerical Analysis" by Parveen (Hoven) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Updated on 2020-02-07. Published on: 2015-12-08