Imported Upstream version 3.24.3
[platform/upstream/cmake.git] / Help / guide / tutorial / Step6 / MathFunctions / MakeTable.cxx
1 // A simple program that builds a sqrt table
2 #include <cmath>
3 #include <fstream>
4 #include <iostream>
5
6 int main(int argc, char* argv[])
7 {
8   // make sure we have enough arguments
9   if (argc < 2) {
10     return 1;
11   }
12
13   std::ofstream fout(argv[1], std::ios_base::out);
14   const bool fileOpen = fout.is_open();
15   if (fileOpen) {
16     fout << "double sqrtTable[] = {" << std::endl;
17     for (int i = 0; i < 10; ++i) {
18       fout << sqrt(static_cast<double>(i)) << "," << std::endl;
19     }
20     // close the table with a zero
21     fout << "0};" << std::endl;
22     fout.close();
23   }
24   return fileOpen ? 0 : 1; // return 0 if wrote the file
25 }