If I want "3x²" to be output in my program, and I have a variable holding the "3" as float coeff and one holding the "2" as int expn, how do I go about displaying the polynomial using cout?

Do I need #include<cmath> as well?

thanks in advance.

Jess

Recommended Answers

All 5 Replies

This is just a logic problem. What part of this is troubling you?

A simple way to do this could be something like so :

for i = 0 to coefficients.size
   print coefficients[i] + "x" + "^" + i + " "

So in the simple example, say coefficients contains C = [2 0 1] which means "2x^2 + 0x^1 + 1x^0"

The above algorithm does exactly that.

Thanks! But when the program is compiled and ran, does it display 2x², or just 2x^2? Or does c++ not support the tiny corner number thing and instead we're simply looking at a way for the user to understand the rightmost number is an exponent?

does c++ not support the tiny corner number thing

Not natively. I'm not sure if you could find a library that would do it in a console app, but it's probably not worth it either way.

std::cout << "3x" << char(253);

I suppose I stand corrected on that particular case (and for cubed also) but, those are special characters; in general, there is not a way to direct cout to print something as an exponent.

Be a part of the DaniWeb community

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