Showing posts with label Solved Exercise. Show all posts
Showing posts with label Solved Exercise. Show all posts

Quadratic equation in C++ - Bhaskara

In this C ++ tutorial, we will learn how to solve a 2nd degree equation using the Bháskara formula in C ++. It's a matter of our list of conditional testing exercises:

  • 11. Make a program that calculates the roots of a quadratic equation in the form ax² + bx + c. The program should request the values of a, b and c and make the consistencies, informing the user in the following situations:
    If the user enters the value of A equal to zero, the equation is not of the second degree and the program should not ask the other values, being closed;
    If the calculated delta is negative, the equation has no real roots. Inform the user and quit the program;
    If the calculated delta equals zero the equation has only one real root; inform it to the user;
    If delta is positive, the equation has two real roots; inform them to the user;

Bhaskara Formula in C ++

First, we ask for the three coefficients a, b and c.
We also declare the delta variable to store delta (really?) and root1 and root2 to store roots.

The first test is to check if a is other than 0, only if we can calculate the roots.
If not, it falls into ELSE and we warn you that the equation does not exist.

Being different from 0, we have to calculate the delta:
From Bhaskara: delta = b² - 4ac

Now, let's test the delta.
If it is negative, we say it has no real roots.
If 0, we calculate the single root: (-b / 2a)
If greater than 0, we calculate the two roots (-b- sqrt(delta))/2a and (-b + sqrt(delta))/2a

See how our code looked:
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    float a, b, c, delta,
          root1, root2;

    cout << "Coefficient a: ";
    cin >> a;

    cout << "Coefficient b: ";
    cin >> b;

    cout << "Coefficient c: ";
    cin >> c;

    if(a != 0){
        delta = (b*b) - (4*a*c);

        if(delta<0){
            cout <<"No real roots\n";
        }
        else if (delta==0){
            root1=(-b)/(2*a);
            cout << "One real root: "<<root1<<endl;
        }else{
            root1=(-b - sqrt(delta))/(2*a);
            root2=(-b + sqrt(delta))/(2*a);
            cout << "Root 1: "<<root1<<endl;
            cout << "Root 2: "<<root2<<endl;
        }
    }else{
        cout <<"a=0, this is not a quadratic equation\n";
    }

}

C++ challange

Can you solve this question to give complex roots?


#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    float a, b, c, delta,
          root1, root2;

    cout << "Coefficient a: ";
    cin >> a;

    cout << "Coefficient b: ";
    cin >> b;

    cout << "Coefficient c: ";
    cin >> c;

    if(a != 0){
        delta = (b*b) - (4*a*c);

        if(delta<0){
            delta = -delta;
            cout<<"Root 1: "<<(-b/(2*a))<<"+"<<(sqrt(delta)/(2*a))<<"i\n";
            cout<<"Root 2: "<<(-b/(2*a))<<"-"<<(sqrt(delta)/(2*a))<<"i\n";
        }
        else if (delta==0){
            root1=(-b)/(2*a);
            cout << "One real root: "<<root1<<endl;
        }else{
            root1=(-b - sqrt(delta))/(2*a);
            root2=(-b + sqrt(delta))/(2*a);
            cout << "Root 1: "<<root1<<endl;
            cout << "Root 2: "<<root2<<endl;
        }
    }
    else
        cout <<"a=0, this is not a quadratic equation\n";

    return 0;
}

Equilateral Triangle, Isosceles or Scalene? C++ Exercise

In this C++ tutorial, we will solve the following question from our list of exercises:

  • 10. Make a program that takes all three sides of a triangle and tells you if it is equilateral, isosceles, or scalene.

C++ Triangle Types

There are three types of triangle:

  1. Equilateral: all sides are equal
  2. Isosceles: Only Two Sides Are Equal
  3. Scalene: All Sides Are Different


So all we have to do is ask for three sides of a triangle, and test whether the sides are all the same, or have two equal or if it's all different.

One way to do this is by testing if it's all the same in an IF, to know if it's equilateral:

  • if ((a == b) && (b == c))

Then if they are all different, to know if it is scalene:

  • if ((a! = b) && (a! = c) && (b! = c))


If it is none of the above, it is because it is isosceles.

Our code looks like this:
#include <iostream>
using namespace std;

int main()
{
    int a, b, c;

    cout << "Side a: ";
    cin >> a;

    cout << "Side b: ";
    cin >> b;

    cout << "Side c: ";
    cin >> c;

    if( (a==b) && (b==c) )
        cout<<"Equilateral\n";
    else if( (a!=b) && (a!=c) && (b!=c))
        cout<<"Scalene\n";
    else
        cout<<"Isosceles\n";

}

Equilateral, Isosceles or Scalene?

An interesting thing about programming is that code is a kind of fingerprint.
Each one does their own thing, each one has their own methods, lines of thought and creativity.

Sometimes it is common to make big code, ugly and confusing.
Then we see someone using half the lines we use, doing something far more beautiful and comprehensive.

That's why it's important to study through other people's books, websites, tutorials, and codes to 'pick up' on others' reasoning. Never miss this custom, okay?

Let's go for one more solution.
First, let's test if there are two equal sides:

  • (a == b) || (a == c) || (b == c)


If you have, either one: either equilateral or just isosceles.
So we test if the three sides are equal:

  • (a == b) && (b == c)

If so, we say it is equilateral. Otherwise it falls into the nested ELSE and we say it is isosceles.
If it does not have at least two equal sides, it is scalene.

Look:

#include <iostream>
using namespace std;

int main()
{
    int a, b, c;

    cout << "Side a: ";
    cin >> a;

    cout << "Side b: ";
    cin >> b;

    cout << "Side c: ";
    cin >> c;

    if( (a==b) || (a==c) || (b==c))
        if( (a==b)&&(b==c) )
            cout<<"Equilateral\n";
        else
            cout<<"Isosceles\n";
    else
        cout<<"Scalene\n";

}

C++ hacker test


A hacker is the one who finds loopholes, errors, code problems.
There is a problem with the code above: the conditions of existence of a triangle.

It's not just entering three values and we have a triangle, no.

Research the conditions of existence of a triangle, and before deciding whether it is equilateral, isosceles or scalene, see if the triangle can even exist.

Post your code in the comments.