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.


6 Years
Discussion Span
Last Post by jonsca

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.


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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.