Hello. I am trying to write a program that solves the "quadratic equation" when given variables "A", "B" and "C" and find the number of zero's. One code is the definition, the next is the implementation and the last code it just the test file. I need help running the codes through a test file. To do this, I need to know which code must be written in the "Header File" and which code must be written in the ".CPP" file (I know the test file needs to be saved in a ".CPP file" already). After that, I also want to know if I used the "assert" code correctly. Thank You.

This part of the code is the definition:

#include <stdio.h>
#include <iostream>
#include <cmath>
#include "QuadSolver/Quad_Imp/main.h"
using namespace std;

class QuadRoots()
{
private:

//The coefficients are defined here
    double aVar;
    double bVar;
    double cVar;

//Find Real Zeros
    double vauleOne;
    double vauleTwo;

//Define discriminat so that we can see if there are one, two are zero solutions
    double findDis;

//Find imaginary numbers(might not need)
    //bool findImaginary;

    void input(); //Control input
    void foundDis(); //Will find discriminat
    //void negativeRoot(); //Sove quadratic using negative symbol
    //void positiveRoot(); //Sove quadratic using positive symbol
    //void foundReal(); //Find real zero's
    //void showSolution(); //Wills show answer to quadratic

    //Sets variables "A", "B" and "C" to double.
    void set_aVar(double);
    void set_bVar(double);
    void set_cVar(double);
public:
//Constructor that will allow this portion of code to run
    QuadRoots(); 

//Allows the variables "A", "B" and "C" to pass through this part of code
    QuadRoots(double aVar, double bVar, double cVar);
};

This is the implementation:

#include <stdio.h>
#ifndef _Quad_Imp_h
#define _Quad_Imp_h

void QuadRoots::input()
{
//Initialize 
    double aVar;
    double bVar;
    double cVar;

//Tells user they are using a "Quadratic Solver
    cout<< "Welcome to Quadratic Solver:"<<endl;
    cout<<endl;

/*************** Controls Variable "A" **************/
//Tell user to type in number for varaiable A
    cout<<"\n Type in variable A: "<<endl;

//Will allow value to be typed for variable A
    cin>>aVar;
    set_aVar(aVar);
/*************** Controls Variable "A" **************/

/*************** Controls Variable "B" **************/

//Tell user to type in number for varaiable B
    cout<<"\n Type in variable B: "<<endl;

//Will allow value to be typed for variable B
    cin>>bVar;
    set_bVar(bVar);
/*************** Controls Variable "B" **************/

/*************** Controls Variable "C" **************/
//Tell user to type in number for varaiable C
    cout<<"\n Type in variable C: "<<endl;

//Will allow value to be typed for variable C
    cin>>cVar;
    set_cVar(cVar);
}
/*************** Controls Variable "C" **************/

/*********** With Regards to the Constructor *********/
QuadRoots::QuadRoots()
{
    input(); //Activates "input"
    foundDis(); //Finds the discriminant();
    showSolution(); //Show answer
}

QuadRoots::QuadRoots()
{
    set_aVar(aVar);
    set_bVar(bVar);
    set_cVar(cVar);

    findDis();
    showSolution(); //Show answer
}
/*********** With Regards to the Constructor *********/

/*********** Find Discriminate and solve *********/
void QuadRoots::foundDis()
{
    findDis = (b * b) + (-4 * a * c);
    assert(findDis >= 0); //Makes sure that discriminate is greater than zero so no imaginary numbers 

    if (findDis > 0) {
        x1 = (-b + sqrt(findDis)) / (2*a);
        x2 = (-b - sqrt(findDis)) / (2*a);
        cout << "Roots are real. Number of Solutions is two" << endl;
        cout << "x1 = " << x1 << endl;
        cout << "x2 = " << x2 << endl;

    }
     else if (findDis == 0) {
        cout << "Roots are real. Number of Solutions is one." << endl;
        x1 = (-b + sqrt(findDis)) / (2*a);
        cout << "x1 = x2 =" << x1 << endl;
    }
    else {
        cout << "Roots are complex. Number of Solutions is zero"  << endl;
    }
    count<< "Number of real numbers: " << countReal++;
}
/*********** Find Discriminate and solve *********/

#endif

This is the test code:

#include <stdio.h>
#include <cmath>
#include <assert.h> 
#include <cmath>

/* Use as test: 
 *  a= 4, b=-3, c=-2 
 *  solution:
 *      x1 = 1.17539
 *      x2 = -0.425391
 * */
int main(){
    QuadRoots solve; //Initialize the object called "solve" with "QuadRoot" information
    solve.variable(4,-3,-2); //Solves equation using numbers
}

Hopefully I will get better at programming and be able to solve these problems on my own however I welcome any guidance that can be provided to me.

Note: Some of the code is written as a comment because I don't think the teacher asked us to sove for "imaginary numbers" but just to solve for "real zero's".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.