Czech version
logolink

< Back to the list of lessons

Solving Quadratic Equations (y = ax2 + bx + c)

AlgortimyContent of the lesson:

  • Used Libraries and Variables
  • Inserting Parameters
  • Improving Values Input Using Try-Except
  • List of Equation Types Which Can Be Formed
  • The End of Program
  • The Whole Source Code

Used Libraries and Variables

You have to link the header file math.h because you will use the square root (sqrt function) which is defined in this library. Use variables for a, b and c parameters (whole numbers), discriminant (a whole number because of the parameters) and finally x1 and x2 as the roots of the quadratic equation. The x1 and x2 variables should be defined as float data type because of the square root.

Inserting Parameters

Let user insert values at the beginning of your program to a, b and c variables.

Entering parameters
printf("a = ");
scanf("%f", &a);

Use the same procedure for reading b and c parameters.

Improving Values Input

Current input can be improved by adding a check whether the input parameters were successfully loaded. The function is simple. We will check the result which is returned by the scanf function. In case that everything is correct, the function returns the number of loaded variables. In case we load only one value, it should return the value 1. The result can be saved inside a variable and then this variable can be checked using a condition. In case that the variable contains other value than 1, we will display information about an error.

Handling input values
    stav = scanf("%d", &a);
	if (stav != 1)
	{
		printf("You have entered an invalid parameter, the zero value will be used instead - a = 0\n");
		a = 0;
	}

List of Equation Types Which Can Be Formed

All you have to do to finish your program is to add conditions for all types of equations. You can get 7 equations according to input parameters so you have to solve all of them. Their list is available here; number 1 means a positive value, -1 means a negative value and 0 means zero.

equationabc
1000
200-1/1
30-1/1-1/0/1
4-1/100
5-1/101
6-1/10-1
7-1/1-1/0/1-1/1

The program always writes which parameters were inserted and also the prescription of the equation. In case the equation cannot be solved, a warning is displayed. The source code may appear to be long and complex but there should be no equation in form y=ax^2+bx+c which cannot be solved by this program.

The End of Program

The end of the program follows:
printf("\n");
printf("The program will be terminated after pressing ENTER key");
getchar();

The Whole Source Code

The whole source code is here:
#include "stdafx.h"
#include &lt;math.h&gt;

int _tmain(int argc, _TCHAR* argv[])
{
	int a, b, c;
	int diskriminant;
	int stav;
	float x1, x2;
	printf("Welcome to the program to compute quadratic equations formed ax^2 + bx + c = 0\n");
	printf("\n");
	printf("Insert all parameters please...\n");
	printf("a = ");
	stav = scanf("%d", &a);
	if (stav != 1)
	{
		printf("You have entered an invalid parameter, the zero value will be used instead - a=0\n");
		a = 0;
	}
	printf("b = ");
	stav = scanf("%d", &b);
	if (stav != 1)
	{
		printf("You have entered an invalid parameter, the zero value will be used instead b = 0\n");
		b = 0;
	}
	printf("c = ");
	stav = scanf("%d", &c);
	if (stav != 1)
	{
		printf("You have entered an invalid parameter, the zero value will be used instead c = 0\n");
		c = 0;
	}
	if ((a == 0) && (b == 0) && (c == 0))
	{
		printf("You entered parameters for the equation 0=0\n"); 
		printf("X belongs to the set of real numbers\n"); 
	}
	else if ((a == 0)&& (b == 0))
	{
		printf("Byly zadany parametry pro rovnici 0=%d\n", c); 
		printf("Rovnice nema reseni pro realna cisla\n");
	}
	else if ((a != 0)&& (b != 0))
	{ 
		printf("You entered parameters for the linear equation 0= %dx + %d\n", b, c); 
		printf("X = %.2f\n", -(float)c/b); 
	}
	else if ((a != 0)&& (b == 0) && (c == 0))
	{
		printf("You entered parameters for the quadratic equation 0=%dx^2\n", a); 
		printf("X1,2 = 0\n"); 
	}
	else if ((a != 0) && (b == 0) && (c > 0)) 
	{
		printf("You entered parameters for the quadratic equation 0= %dx^2+%d\n", a, c); 
		printf("There is no solution for the set of real numbers"); 
	}
	else if ((a != 0) && (b == 0) && (c < 0)) 
	{
		printf("You entered parameters for the quadratic equation 0= %dx^2-%d\n",a,-c); 
		x1 = sqrt(-(float)c/a); 
		x2 = -x1; 
		printf("X1 = %.2f\n", x1); 
		printf("X2 = %.2f\n", x2); 
	} 
	else if ((a != 0) && (b != 0))
	{ 
		printf("You entered parameters for the quadratic equation 0= %dx^2+%dx+%d\n", a, b, c); 
		diskriminant = b*b-4*a*c; 
		if (diskriminant>=0) 
		{
			x1 = (-b+sqrt((float)diskriminant))/(2*a); 
			x2 = (-b-sqrt((float)diskriminant))/(2*a); 
			printf("X1= %.2f\n", x1); 
			printf("X2= %.2f\n", x2); 
		}	 
		else 
		{
		printf("There is no solution for the set of real numbers\n"); 
		}
	} 
	printf("\n");
	printf("The program will be terminated after pressing ENTER key"); 
	fflush(stdin);
	getchar(); 
	return 0;
}

Questions

  1. What is the meaning of the return value of function scanf?
  2. Try to find a logical mistake inside the program and correct it.

Individual Task

Improve the program to be able to accept decimal values of parameters and to be able to compute the results without converting them to whole numbers.

webdesign, xhtml, css, php - Mgr. Michal Mikláš