A
Army1987
I am a newbie to C++ (altough I already know some C). I've tried to
implement the TPK algorithm.
#include <iostream>
#include <ostream>
#include <cmath>
const int size = 11;
const double max = 400.0;
double f(const double& t);
int main(void)
{
double array[size];
for (int i = 0; i < size; ++i)
std::cin >> array;
for (int i = size - 1; i >= 0; --i) {
double y = f(array);
// std::cout << i + 1 << '\t' << (y > max ? "TOO LARGE" : y )
// << std::endl;
// doesn't compile, because "TOO LARGE" and y are different types.
// Do I really have to use an if statement whose branches are
// almost identical? Is there a better way?
std::cout << i + 1 << '\t';
if (y > max)
std::cout << "TOO LARGE" << std::endl;
else
std::cout << y << std::endl;
}
return 0;
}
double f(const double& t)
{
return std::sqrt(std::fabs(t)) + 5.0 * t * t * t;
}
If something which can't be interpreted as a double is input,
std::cin >> array leaves array uninitialized, and doesn't
discard garbage. How can I check if it succeeds? Since the cascading style can be used, I guess std::cin >> array evalues
to std::cin itself, so I guess using it in the controlling
expression of an if statement doesn't help. I could initialize all
the elements of the array to some strange value (eg. HUGE_VAL) and
check if it was unchanged, but is there a more elegant way?
Also, is there a good free online C++ tutorial, especially one
which points out differences with C? I've tried
http://www.icce.rug.nl/documents/cplusplus/ but it seems to be very
outdated (e.g. it claims there is no long double type in C, which
has had it since C89).
implement the TPK algorithm.
#include <iostream>
#include <ostream>
#include <cmath>
const int size = 11;
const double max = 400.0;
double f(const double& t);
int main(void)
{
double array[size];
for (int i = 0; i < size; ++i)
std::cin >> array;
for (int i = size - 1; i >= 0; --i) {
double y = f(array);
// std::cout << i + 1 << '\t' << (y > max ? "TOO LARGE" : y )
// << std::endl;
// doesn't compile, because "TOO LARGE" and y are different types.
// Do I really have to use an if statement whose branches are
// almost identical? Is there a better way?
std::cout << i + 1 << '\t';
if (y > max)
std::cout << "TOO LARGE" << std::endl;
else
std::cout << y << std::endl;
}
return 0;
}
double f(const double& t)
{
return std::sqrt(std::fabs(t)) + 5.0 * t * t * t;
}
If something which can't be interpreted as a double is input,
std::cin >> array leaves array uninitialized, and doesn't
discard garbage. How can I check if it succeeds? Since the cascading style can be used, I guess std::cin >> array evalues
to std::cin itself, so I guess using it in the controlling
expression of an if statement doesn't help. I could initialize all
the elements of the array to some strange value (eg. HUGE_VAL) and
check if it was unchanged, but is there a more elegant way?
Also, is there a good free online C++ tutorial, especially one
which points out differences with C? I've tried
http://www.icce.rug.nl/documents/cplusplus/ but it seems to be very
outdated (e.g. it claims there is no long double type in C, which
has had it since C89).