Quadratic equation solver outputting in unknown format - c++

I'm trying to make a quadratic equation solver, but for some reason my program is giving me answers in an unknown format.
I entered the simple quadratic equation x^2 + 2x + 1 = 0, expecting my program to give x = -1 or x = -1, but instead it gave x = 0138151E or x = 0138152D. It looks like it outputs these values for x for any inputs (not recognizing unreal answers and catching them). Why is this and how can I fix it?
#include "../std_lib_facilities_revised.h"
class Imaginary {};
double square(int a)
{
return a * a;
}
double quadratic_solver_pos(int a, int b, int c)
{
double x = 0.0;
double radicand = square(b) - 4 * a * c;
if (radicand < 0) throw Imaginary{};
x = (-b + sqrt(radicand)) / (2 * a);
return x;
}
double quadratic_solver_neg(int a, int b, int c)
{
double x = 0.0;
double radicand = square(b) - 4 * a * c;
if (radicand < 0) throw Imaginary{};
x = (-b - sqrt(radicand)) / (2 * a);
return x;
}
int main()
try {
cout << "This program is a quadratic equation solver.\n";
cout << "Quadratic equations are of the form: ax^2 + bx + c = 0\n";
cout << "Enter a, b, and c, respectively:\n";
double a = 0;
double b = 0;
double c = 0;
cin >> a >> b >> c;
cout << "Your quadratic equation: " << a << "x^2 + " << b << "x + " << c << " = 0\n";
cout << "x = " << quadratic_solver_pos << " or x = " << quadratic_solver_neg << '\n';
}
catch (Imaginary) {
cout << "x is unreal\n";
}

You don't pass your variables to your functions.
You need to do it like so quadratic_solver_pos(a, b, c);.

Related

How to translate this equation to C++

I am trying to translate this equation to c++ code:
x = (10π)/(a+b)*sinC^3+3(ln a)(tan C)
Here is my attempt:
#include <iostream>
#include <iomanip>
#include <math.h> using namespace std;
int main()
{
float x, y, z, a, b, C, PI;
cout << endl << "Enter value a=";
cin >> a;
cout << "Enter value b=";
cin >> b;
cout << "Enter angle C in degrees=";
cin >> C;
PI = 3.1416;
C = C * PI / 180;
x = ((10 * PI) / (a + b)) * pow(sin(C), 3);
+3 * (log(a)) * (tan(C));
y = 0;
z = 0;
cout << fixed << setprecision(4);
cout << endl << "x = " << x;
cout << endl << "y = " << y;
cout << endl << "z = " << z;
}
Pi is defined in math.h, as M_PI
Sine is defined in math.h as double sin(double)
Tangent is defined in math.h as double tan(double)
Natural Log is defined in math.h as double ln(double)
Power is defined in math.h as double pow(double,double)
You wrote:
x = ((10 * PI) / (a + b)) * pow(sin(C), 3);
+3 * (log(a)) * (tan(C));
The second 1/2 of that (starting at +3) is not part of the original expression, which ended with a semi-colon.
Try:
x = ((10 * PI) / (a + b)) * pow(sin(C), 3) + 3 * (log(a)) * (tan(C));
Also, be sure you understand the difference between log(base10) and ln(natural log).

C++ truncation error?

I am learning C++ and came upon this problem while trying to use a formula to calculate the current.
And I got: 0.628818 where the answer should be:
f=200 Hz
R=15 Ohms
C=0.0001 (100µF)
L=0.01476 (14.76mH)
E = 15 V
Answer: I = 0.816918A (calculated)
Below is my code:
#include <iostream>
#include <cmath>
int main()
{
const double PI = 3.14159;
double r = 15;
double f = 200;
double c = 0.0001;
double l = 0.01476;
double e = 15;
double ans = e / std::sqrt(std::pow(r, 2) + (std::pow(2 * PI*f*l - (1.0 / 2.0 * PI*f*c), 2)));
std::cout << "I = " << ans << "A" << std::endl;
}
I have read about truncation errors and tried to use 1.0/2.0 but doesn't seem to work either.
Truncation error refers to using only the first N terms of an infinite series to estimate a value. So the answer to your question is "No." You might find the following to be of some interest however....
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
template<typename T>
T fsqr(T x) { return x * x; }
// Numerically stable and non-blowuppy way to calculate
// sqrt(a*a+b*b)
template<typename T>
T pythag(T a, T b) {
T absA = fabs(a);
T absB = fabs(b);
if (absA > absB)
{
return absA*sqrt(1.0 + fsqr(absB / absA));
} else if (0 == absB) {
return 0;
} else {
return absB*sqrt(1.0 + fsqr(absA / absB));
}
}
int main () {
double e, r, f, l, c, ans;
const double PI = 3.14159265358972384626433832795028841971693993751058209749445923078164062862089986280348253421170;
cout << "Insert value for resistance: " << endl;
cin >> r ;
cout << "Insert value for frequency: " << endl;
cin >> f;
cout << "Insert value for capacitance: " << endl;
cin >> c;
cout << "Insert value for inductance: " << endl;
cin >> l;
cout << "Insert value for electromotive force (voltage): " << endl;
cin >> e;
ans = e / pythag(r, 2*PI*f*l - (1/(2*PI*f*c)) );
cout << "I = " << ans << "A" << endl;
system("pause");
return 0;
}
Just kidding about all that PI.
The main problem is multiplying ½ by πfC instead of dividing, here:
(1.0 / 2.0 * PI*f*c)
This sort of problem is best avoided by using suitable named values (that also allows you to use faster and more precise x*x instead of std::pow(x,2)).
You can also remove some of that arithmetic by using the standard hypotenuse function instead of squaring and sqrting inline:
double ans = e / std::hypot(r, (2*PI*f*l - 0.5/PI/f/c));
#include <iostream>
#include <cmath>
int main()
{
static constexpr double PI = 4 * std::atan(1);
double r = 15; // ohm
double f = 200; // hertz
double c = 0.0001; // farad
double l = 0.01476; // henry
double e = 15; // volt
double current = e / std::hypot(r, (2 * PI*f*l - 0.5/PI/f/c));
std::cout << "I = " << current << "A" << std::endl;
}

Multiplying matrices c++

I want to make a program capable of rising the 2x2 matrix to k power. I know how to make one that is going to square it but once i want to reach higher powers i struggle to save the results I have and use it in the next equation. a,b,c,d are the numbers in the matrix, n is the amount of matrix I will want to do in one time, k is the power i want the matrix to be taken to and m is modulo which I want to use on the numbers. I know there is a method of making this fairly simply but I am unable to figure out a good way to use my results from the equation done before in the next equation.
#include <iostream>
using namespace std;
int mnoz(int a, int b, int c,int d,int m){
int a2 = (a*a + c*b) % m;
int b2 = (a*b + b*d) % m;
int c2 = (c*a + d*c) % m;
int d2 = (c*b + d*d) % m;
return a2, b2, c2, d2;
}
int main()
{
int a, b, c, d, k, m, n;
int e, f, g, h;
int e2, f2, g2, h2;
cin >> n;
// a^k = (a^2)^k/2
for (int i = 0; i<n; i++){
cin >> a >> b >> c >> d >> k >> m;
if (k == 1){
cout << a << " " << b << " " << c << " " << d << endl;
}
else if (k == 2){
e = (a*a + c*b) % m;
f = (a*b + b*d) % m;
g = (c*a + d*c) % m;
h = (c*b + d*d) % m;
cout << e << " " << f << " " << g << " " << h << endl;
}
else{
if (k % 2 == 0){
e = (a*a + c*b) % m;
f = (a*b + b*d) % m;
g = (c*a + d*c) % m;
h = (c*b + d*d) % m;
int z = (k/2)-1;
for (int j = 0; j < z; j++){
int e2 = e;
int f2 = f;
int g2 = g;
int h2 = h;
mnoz(e2, f2, g2, h2, m);
}
cout << e << " " << f << " " << g << " " << h << endl;
}
}
}
system("pause");
return 0;
}
To minimize the number of multiplications, you can use a recursive method.
To give an analogy with real numbers, say you want to compute a^n.
Check whether n is even or odd.
If even, compute b = a^(n/2). Then the final result is b*b.
If odd, compute b = a^((n-1)/2. Then the final result is b*b*a.
Since you are talking matrices instead of numbers, you need to be able multiply any two matrices. You can create a class/struct for the matrix and add an operator*() function.
Here's some sample code.
#include <iostream>
struct Matrix
{
Matrix(double pa, double pb, double pc, double pd) : a(pa), b(pb), c(pc), d(pd) {}
Matrix operator*(Matrix const& rhs) const
{
double an = this->a*rhs.a + this->b*rhs.c;
double bn = this->a*rhs.b + this->b*rhs.d;
double cn = this->c*rhs.a + this->d*rhs.c;
double dn = this->c*rhs.b + this->d*rhs.d;
return Matrix(an, bn, cn, dn);
}
Matrix square() const
{
return (*this)*(*this);
}
double a;
double b;
double c;
double d;
};
Matrix matrixPower(Matrix const& m, int k)
{
if ( k == 1 )
{
return m;
}
Matrix out = matrixPower(m, k/2).square();
if ( k%2 == 1 )
{
return out*m;
}
else
{
return out;
}
}
std::ostream& operator<<(std::ostream& out, Matrix const& m)
{
out << "[ " << m.a << " " << m.b << " ]\n";
out << "[ " << m.c << " " << m.d << " ]\n";
}
int main()
{
Matrix m(0.4, 0.7, 0.7, 0.4);
std::cout << matrixPower(m, 5) << std::endl;
std::cout << matrixPower(m, 10) << std::endl;
std::cout << matrixPower(m, 15) << std::endl;
};
The output:
[ 0.80404 0.80647 ]
[ 0.80647 0.80404 ]
[ 1.29687 1.29687 ]
[ 1.29687 1.29687 ]
[ 2.08862 2.08862 ]
[ 2.08862 2.08862 ]

My c++ code for factoring isn't working

I am trying to create a c++ program that when I input two numbers (num1, combinationNum), it finds two numbers that multiply together to equal num1, but add together to equal combinationNum. It currently works for positive integers, but not negative. How do I make it work with negative integers? Also, If the equation isn't solvable, I would like it to print an error of some sort. Thanks!
Code:
//
// main.cpp
// Factor
//
// Created by Dani Smith on 2/13/14.
// Copyright (c) 2014 Dani Smith Productions. All rights reserved.
//
#include <iostream>
#include <cmath>
using namespace std;
void factors(int num, int comNum){
int a, b;
cout<<"The factors are ";
bool isPrime = true;
int root = (int)sqrt((double)num);
for(int i = 2; i <= root; i++){
if(num % i == 0 ){
isPrime = false;
//cout<<i<<",";
for(int x = 0; x<3; x++){
if(x==1){
a = i;
}
else if(x == 2){
b = i;
}
if(a + b == comNum){
cout << a << ", and " << b << ".";
}
}
}
}
//----------------------------------------
if(isPrime)cout<<"1 ";
cout<<endl;
}
int main(int argc, const char * argv[])
{
int num1 = 0, num2 = 0, multiple = 0, combinationNum = 0, output1 = 0, output2 = 0;
cout << "What number do you want to factor?\n";
cin >> num1;
cout << "What do you want them to add to?\n";
cin >> combinationNum;
factors(num1, combinationNum);
return 0;
}
To solve:
x + y == a
x * y == b
You have to solve
y == a - x
x * x - a * x + b == 0
So with delta == a * a - 4 * b, if delta positive, the solutions are
x1 = (a + sqrt(delta)) / 2
x2 = (a + sqrt(delta)) / 2
The code : (https://ideone.com/qwrSwa)
void solve(int sum, int mul)
{
std::cout << "solution for x + y = " << sum << std::endl
<< " x * y = " << mul << std::endl;
const int delta = sum * sum - 4 * mul;
if (delta < 0) {
std::cout << "No solution" << std::endl;
return;
}
const float sqrtdelta = sqrtf(delta);
const float x1 = (sum + sqrtdelta) / 2.f;
const float x2 = (sum - sqrtdelta) / 2.f;
std::cout << "x = " << x1 << ", y = " << sum - x1 << std::endl;
if (delta != 0) {
std::cout << "x = " << x2 << ", y = " << sum - x2 << std::endl;
}
}

euclid's extended algorithm C ++

I'm having an issue with Euclid's Extended Algorithm. (ax+by=gcd(a,b)) I'm trying to determine both the GCD and x and y. The GCD isn't a problem but using the loop method something is going wrong with x and y. Normally one number comes up as 0 and the other is an abnormally large negative number. Code follows:
#include <iostream>
using namespace std;
main ()
{
int a,b,q,x,lastx,y,lasty,temp,temp1,temp2,temp3;
cout << "Please input a" << endl;
cin >> a;
cout << "Please input b" << endl;
cin >> b;
if (b>a) {//we switch them
temp=a; a=b; b=temp;
}
//begin function
x=0;
y=1;
lastx=1;
lasty=0;
while (b!=0) {
q= a/b;
temp1= a%b;
a=b;
b=temp1;
temp2=x-q*x;
x=lastx-q*x;
lastx=temp2;
temp3=y-q*y;
y=lasty-q*y;
lasty=temp3;
}
cout << "gcd" << a << endl;
cout << "x=" << lastx << endl;
cout << "y=" << lasty << endl;
return 0;
}
Although the question has been asked a long time ago, but the answer will help someone who were finding C++ implementation of extended euclidean algorithm.
Here is a recursive C++ implementation:
int xGCD(int a, int b, int &x, int &y) {
if(b == 0) {
x = 1;
y = 0;
return a;
}
int x1, y1, gcd = xGCD(b, a % b, x1, y1);
x = y1;
y = x1 - (a / b) * y1;
return gcd;
}
Example with code:
#include <iostream>
int main()
{
int a = 99, b = 78, x, y, gcd;
if(a < b) std::swap(a, b);
gcd = xGCD(a, b, x, y);
std::cout << "GCD: " << gcd << ", x = " << x << ", y = " << y << std::endl;
return 0;
}
Input:
a = 99, b =78
Output:
GCD: 3, x = -11, y = 14
Two of your assignments are wrong they should be:
temp2 = x;
x=lastx-q*x;
lastx = temp2;
temp3 = y;
y = lasty-q*y;
lasty=temp3;
Example output with the above fixes:
Please input a
54
Please input b
24
gcd6
x=1
y=-2