Solution For C Program -
Write overloaded function templates for finding the roots of the linear (a * x + b = 0) and square (a * x2 + b * x + c = 0) uravneniy.Zamechanie: in function to send coefficients of the equations.
#include <iostream>
#include <math.h>
#include <windows.h>
using namespace std;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
double findUnknown(int, int);
double findUnknown(double, double);
double findUnknown(int, int, int);
double findUnknown(double, double, double);
void testFindUnknow2IntType(int test, int a, int b , double expected)
{
double result = findUnknown(a,b);
if (expected == result)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << test << " passed\n";
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << test << " failed\n";
cout << result << " actual\n";
cout << expected << " expected\n\n";
}
}
void testFindUnknow2DoubleType(int test, double a, double b, double expected)
{
double result = findUnknown(a, b);
if (expected == result)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << test << " passed\n";
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << test << " failed\n";
cout << result << " actual\n";
cout << expected << " expected\n\n";
}
}
void testFindUnknow3IntType(int test, int a, int b, int c, double expected)
{
double result = findUnknown(a, b, c);
if (expected == result)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << test << " passed\n";
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << test << " failed\n";
cout << result << " actual\n";
cout << expected << " expected\n\n";
}
}
void testFindUnknow3DoubleType(int test, double a, double b, double c, double expected)
{
double result = findUnknown(a, b, c);
if (expected == result)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << test << " passed\n";
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << test << " failed\n";
cout << result << " actual\n";
cout << expected << " expected\n\n";
}
}
void main()
{
int menuButton = 0;
int show = 0;
int a = 0;
int b = 0;
int c = 0;
cout << "1.Line\n2.Square\n";
cin >> menuButton;
if (menuButton == 1)
{
/*cout << "1. a*x + b = 0" << endl;
cin >> a >> b;
cout << findUnknown(a, b) << endl;*/
testFindUnknow2IntType(1, 2, 4, -2);
testFindUnknow2IntType(2, 1, 5, -5);
testFindUnknow2DoubleType(3, 4, 2, 0.5);
testFindUnknow2DoubleType(4, 5, 2.5 , 0.5);
}
if (menuButton == 2)
{
/*cout << "2. a*x^2 + b*x + c = 0" << endl;
cin >> a >> b >> c;
show = findUnknown(a, b, c);
cout << "X2 = " << show << endl;*/
testFindUnknow3IntType(1, 1, -2, -3, -1);
testFindUnknow3IntType(2, 1, 5, 6, -3);
testFindUnknow3DoubleType(3, 0.5, 6, 0, -12);
testFindUnknow3DoubleType(4, 12, 5, -0.5, -0.5);
}
}
double findUnknown(int a, int b)
{
double x = b * (-1) / a;
return x;
}
double findUnknown(double a, double b)
{
double x = b / a;
return x;
}
double findUnknown(int a, int b, int c)
{
int discriminant = b*b - 4 * a * c;
double x1 = (b*(-1) - sqrt(discriminant)) / 2 * a;
//double x2 = (b*(-1) - sqrt(discriminant)) / 2 * a;
return x1;
}
double findUnknown(double a, double b, double c)
{
double discriminant = b*b - 4 * a * c;
double x1 = (b*(-1) - sqrt(discriminant)) / ( 2 * a );
//double x2 = (b*(-1) - sqrt(discriminant)) / ( 2 * a );
return x1;
}
Learn More :
Equation
Find
- Find out the perfect number using c program
- Find g.c.d of two number using c program.
- Write a c program to find out NCR factor of given number
- How to Write a C program to find maximum between two numbers ?
- Write a C program to find maximum between three numbers ?
- C or C++ program to read marks of 4 subjects and find how many students are pass and fail with division
- C Program turnLEDOn to Drives Forward Until It Finds The Line
- C Program to Find Random Number
- C Program To Find LCM and HCF Of Two Number Using Function - 2
- C Program to find LCM and HCF Of Two Number Using Recursion - 3
- C Program To Find LCM and HCF Of Two Number -1
- C Program To Find Length Of A String Including Blank Spaces, Tabs, And Other Special Characters
- C Program To Find Length Of String And Concatenate Two Strings
- C Program To Find Product Of Two No Using MACRO
- C Program To Find Reverse Of Any Digit Number
- C Program To Find Sum and Difference Of Two Matrices
- C Program To Find The Frequency Of A Number
- C Program To Find The Length, Reverse Of A String And To Check It Is Palindrome or Not
- C Program To Find The Maximum And Minimum Value In An Array
- C Program To Find Transpose Of A Matrix
- C Program To Find Union & Intersection Of Two Array
- C Program Finding the sum of Squares using Recursion
- C Program to Find NCR Of A Given Number Program
- C Program To Find Maximum Of Given Numbers
Overloaded
Coefficients
Function
- How to pass one dimensional array to function in c.
- Write a c program which passes two dimension array to function.
- C Program Character toupper() Example
- C Program Function Example
- Napisać funkcję obliczającą funkcję geometryczną w tablicy NxM elementowej z elementów o wartościach parzystych znajdujących się pod główną i ponad przeciwną przekątną.
- C Program To Find LCM and HCF Of Two Number Using Function - 2
- C Program To Convert Temperature In Celsius To Fahrenheit, Using Function
- C Program To Find Simple Interest
- C Function to Check Vowel
- Factorial Program In C Using Function
- C Program For Prime Number Using Function
- C Function to xorSwap and addSwap in C Programming
- C Program to concatenate two strings without using string functions
- C Function to read instructions from the file and obey them
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- C Program to Implements a dictionary's functionality.
- = (int*)malloc(sizeof(int)) ??
- Design a C function that shortens a string to 8 characters
- C Program to Implements a dictionary's functionality
- C Program that prompts the user to input a string, (whitespaces allowed)
- Local sounds functions in C Program
- Function Get the resource that will be gathered from the zone name
- C Program to Find the Size of File using File Handling Function
- Average function in C
Linear
Roots