Output problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
template<class T>
class XVar {
private:
	T m_X;
public:
	XVar(T x) {
		m_X = x;
	}
	T GetXValue() {
		return m_X;
	}
};

template<class T, class U, class V>
class Quadratic {
private:
	XVar<T> *m_pXPoints[15];
	int m_Pos;
	U m_A;
	U m_B;
	U m_C;
public:
	Quadratic(U a, U b, U c) {
		m_A = a;
		m_B = b;
		m_C = c;
		m_Pos = 0;
	}
	void Insert(XVar<T> *pInput) {
		for (int i = 0; i < 15; i++) {
			m_pXPoints[i] = pInput;
		}
		m_Pos++;
		if (m_Pos > 15) {
			throw((string)"array is full");
		}
	}
	void ComputePrint() {
		T y;
		for (int i = m_Pos; i > 0; i--) {
			y = (m_A*pow(m_pXPoints[i]->GetXValue(), 2)) + (m_B*m_pXPoints[i]->GetXValue()) + m_C;
			cout << "Given x = " << m_pXPoints[i]->GetXValue() << ", y = " << y << endl;
		}
	}
};

int main() {
	double a, c, x;
	int b;
	cout << "Enter a, b and c coefficients: ";
	cin >> a >> b >> c;
	cout << "Quadratic Eq, y =" << a << "x^2 + " << b << "x + " << c << endl;
	Quadratic<double, int, double>obj1(a, b, c);
	cout << "Enter a x-value (or a char to exit): ";
	cin >> x;
	if (!cin.good())
		return 0;
	XVar<double> *obj2 = new XVar<double>(x);
	obj1.Insert(obj2);
	while (1) {
		cout << "Enter another x-value (or a char to exit): ";
		cin >> x;
		if (!cin.good())
			break;
		obj2 = new XVar<double>(x);
		obj1.Insert(obj2);
	}
	cout << "y is computed as follows: " << endl;
	obj1.ComputePrint();
}


My program output:

Enter a, b and  c coeficients: 3 2 1
Quadratic Eq, y=3x^2 + 2x + 1
Enter a x-value (or a char to exit): 2.5
Enter a x-value (or a char to exit): 3.5
Enter a x-value (or a char to exit): a
y is computed as follows:
Given x = 3.5, y= 44.75
Given x = 3.5, y= 44.75


The output should be:

Enter a, b and  c coeficients: 3 2 1
Quadratic Eq, y=3x^2 + 2x + 1
Enter a x-value (or a char to exit): 2.5
Enter a x-value (or a char to exit): 3.5
Enter a x-value (or a char to exit): a
y is computed as follows:
Given x = 2.5, y= 24.75
Given x = 3.5, y= 44.75


Hi guys I am having a problem with my code. The function of this code is take as many x values and computes y value using quadratic equation. My problem in this code is when in entered multiple x values the output just shows last x value and it's respect values. The previous x values donesn't shows. I tried debug as much as I can but the problem still persist. Hopefully you can help me out.
Last edited on
1
2
3
4
5
6
	void Insert(XVar<T> *pInput) {
		for (int i = 0; i < 15; i++) {
			m_pXPoints[i] = pInput;
		}
		//...
	}
read that loop, ¿what were your intention? ¿what is that loop doing? ¿why do you have a loop at all?


> XVar<double> *obj2 = new XVar<double>(x);
http://www.cplusplus.com/forum/general/138037/
First of all...
what is the point of the following class?

1
2
3
4
5
6
7
8
9
10
11
12
template<class T>
class XVar {
private:
	T m_X;
public:
	XVar(T x) {
		m_X = x;
	}
	T GetXValue() {
		return m_X;
	}
};


It's a class which is templated with the type of value it contains... but it does nothing else useful. In other words, I see no benefit to using XVar<double> over just plain double. You'll be able to reason about the logic much easier if you get rid of such cruft.
The question is based on UML. So that class is present in UML so I cant skip it @woldaker

It takes more than one values from main function and assigning to the m_pXPoints so I did the loop and dynamic allocation need to be done because the question stated it @ne555
Last edited on
To me, there are a number of mysteries in your code... Why the array of 15 pointers to XVar<double>? Why is this inside the Quadratic class?

It appears to me, you are getting values from stdin for A, B, and C, where y = Ax^2 + Bx + C. Then it asks for the value of x, and it prints out y. Simple. Where does 15 come in at all?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Quadratic {
  private:
    int a, b, c;

  public:
    Quadratic() : a(1), b(0), c(0) {}
    Quadratic(int a, int b, int c) : a(a), b(b), c(c) {}

    double const ComputeY(double const x) const {
        return this->a * std::pow(x, 2) + this->b * x + this->c;
    }
};

// get values of a, b, and c from keyboard input.

Quadratic q(std::atoi(inputA), std::atoi(inputB), std::atoi(inputC));
std::cout << "For X = " << inputX << ", Y = " << q.ComputeY(inputX) << std::endl;

// If you need 15 of these, then just make 15 Quadratic objects:
std::vector<Quadratic> quads(15);


I may be missing something but I don't think it needs to get any more complicated than that.
https://imgur.com/a/OShZb

As I told before the question is based on this uml. That's why my code looks like that. This is one of the question in past year finals.
Last edited on
> It takes more than one values from main function and assigning to the m_pXPoints
no, it takes one value
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	while (1) {
		cout << "Enter another x-value (or a char to exit): ";
		cin >> x; //one value
		if (!cin.good())
			break;
		obj2 = new XVar<double>(x); //one value
		obj1.Insert(obj2);
	}

	void Insert(XVar<T> *pInput) {
		for (int i = 0; i < 15; i++) {
			m_pXPoints[i] = pInput; //one value
		}
		//...
	}
you take one value and copy it on all the positions of the array and then wonder why all the positions of the array have the same value.
Instead, you only need to touch the last element, and increase the size
m_pXPoints[m_Pos++] = pInput


> dynamic allocation need to be done because the question stated it
ok, you are leaking memory. fix that.
thanks you so much that helps me out. I fixed it
Topic archived. No new replies allowed.