errors

I have this question in one of my assignments but I am not sure how to do it.

ostream& operator<<(ostream& out, const vec& v)
{
out << "Vec of size: " << v.sz << " ";
for(int i(0);i<v.sz;i++)
out << v.mem[i];
out << endl;

return out;
}

Write a version of this output operator where each number is printed on its own line, with 4
significant figures. Additionally print the scientific form of each number in brackets on the same line
e.g.
123.4 (1.234e+002)
Can you see, in the code, the line where the number is printed?

Bonus question; can you see what's inside that for loop?
Last edited on
You have to cook up the answer, as c++ is not aware of significant digits. It does provide a form of scientific notation, if using it is acceptable.
http://www.cplusplus.com/reference/ios/scientific/

what i would do is get the scientific notation of the absolute value as a string and take the first 5 letters as a substring. so -30000000 is "3.000" then remove the period and reinsert it in the correct position with leading or trailing zeros as needed all as a string. note that zeros do not contribute to sig digits: 0.0000001234 has 4 SD, as does 12340000000.0 ** and don't forget to put the sign back out if it was negative.

** some take the .0 to mean all the digits are significant and it happens to have zeros in it, so you may need to review what to do for trailing zeros. I think if you had 123 it may want 123.0
or 12 as 12.00 ? whereas 1200 without a decimal has 2 SD and 1200. has 4 because of the decimal pt? I forget all the rules; its something you don't usually see again after elementary school.
Last edited on
Topic archived. No new replies allowed.