1

I am writing a program that takes in 3 user inputted values for a quadratic equation, does some calculation, and returns how many roots the quadratic has.

When I print *(point), it gives me the correct value from the function.

However, when I use *(point) in the If conditions, it does not seem to work the way I want it to - I believe that *(point)is always some positive number, hence why it always executing that specific if condition.

The user values: a = 9, b = -12, c = 4 should print out This quadratic has 1 root. and the values: a = 2, b = 16, c = 33 should print out This quadratic has 2 roots. BUT the program always prints out This quadratic has 0 roots. no matter what the values entered.

Here is my code:

#include "stdafx.h"
#include <iostream>

using namespace std;

float *quadratic(float a1[]);

int main()
{

    float a1[3] = {};
    float *point;

    cout << "Enter a: ";
    cin >> a1[0];
    cout << "\nEnter b: ";
    cin >> a1[1];
    cout << "\nEnter c: ";
    cin >> a1[2];

    point = quadratic(a1);

    cout << endl << "d = " << *(point) << endl; 

    if (*(point) < 0) {
        cout << "\nThis quadratic has 2 roots.\n";
    }
    else if (*(point) > 0) {
        cout << "\nThis quadratic has 0 roots.\n";
    }
    else { //else if *(point) is equal to 0
        cout << "\nThis quadratic has 1 root.\n";
    }

    return 0;
}

float *quadratic(float a1[]) {

    float d;
    d = (a1[1] * a1[1]) - (4 * a1[0] * a1[2]);

    float xyz[1] = { d };
    return xyz;

}
Barmar
  • 741,623
  • 53
  • 500
  • 612
Cereal
  • 33
  • 1
  • 5

1 Answers1

1

Your function quadratic returns a pointer to a local array. After the function return that array doesn't exist anymore. The pointer is then a dangling pointer, a pointer pointing to a place in memory that has once held an object, but that now may hold anything or just rubbish.

Since the array that quadratic attempts to return is always one value there is no need for returning an array. Just return that value.

You don't even need to deal with arrays for the polynomial's coefficients, since they're always three, but if array seems better than individual a, b and c variables, then just use std::array, e.g. like this:

#include <iostream>
#include <array>
#include <vector>
using namespace std;

using Float = double;
auto square_of( Float const v ) -> Float { return v*v; }

auto determinant( array<Float, 3> const& a )
    -> Float
{
    return square_of( a[1] ) - 4*a[0]*a[2];
}

auto main()
    -> int
{
    array<Float, 3> a;
    cout << "Enter A: "; cin >> a[0];
    cout << "Enter B: ";  cin >> a[1];
    cout << "Enter C: ";  cin >> a[2];

    Float const d = determinant( a );

    cout << "d = " << d << endl; 
    if( d < 0 )
    {
        cout << "This quadratic has 2 roots." << endl;
    }
    else if( d > 0 )
    {
        cout << "This quadratic has 0 roots." << endl;
    }
    else // d == 0
    {
        cout << "This quadratic has 1 root.";
    }
}

The code above is equivalent to what I perceived as the intent of your code.

However, I'd check out the formula for roots of quadratic equations, and test the program, before handing in something.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331