mirror of
https://github.com/VictorEijkhout/TheArtOfHPC_vol3_cppf08programming.git
synced 2026-01-24 22:44:48 +09:00
42 lines
828 B
C++
42 lines
828 B
C++
/****************************************************************
|
|
****
|
|
**** This file belongs with the course
|
|
**** Introduction to Scientific Programming in C++/Fortran2003
|
|
**** copyright 2016-2021 Victor Eijkhout eijkhout@tacc.utexas.edu
|
|
****
|
|
**** hello.cxx : an empty program
|
|
****
|
|
****************************************************************/
|
|
|
|
#include <iostream>
|
|
using std::cin, std::cout;
|
|
|
|
#include <cmath>
|
|
using std::sqrt;
|
|
|
|
bool legal_square_root( float &x ) {
|
|
if (x<0)
|
|
return false;
|
|
else {
|
|
x = sqrt(x);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
|
|
float x;
|
|
|
|
for (;;) {
|
|
cout << "Give a number: ";
|
|
cin >> x;
|
|
if (!legal_square_root(x)) {
|
|
cout << "Sorry, that was negative!" << '\n';
|
|
break;
|
|
} else
|
|
cout << "... and its square root is: " << x << '\n';
|
|
}
|
|
|
|
return 0;
|
|
}
|