Concept
How can conditionals validate input for a logarithm calculator?
ComputerScienceOne / Quadratic Roots Revisited
"16. Conditionals\n\n1 #include <stdlib.h>\n\n2 #include <stdio.h>\n\n3 #include <math.h>\n\n4\n\n5 /**\n\n6 * This program computes the logarithm base b (b > 1)\n\n7 * of a given number x > 0\n\n8 */\n\n9 int main(int argc, char **argv) {\n\n10\n\n11 double b, x, result;\n\n12 if(argc != 3) {\n\n13 printf(\"Usage: %s b x\n\n\", argv[0]);\n\n14 exit(1);\n\n15 }\n\n16\n\n17 b = atof(argv[1]);\n\n18 x = atof(argv[2]);\n\n19\n\n20 if(x <= 0) {\n\n21 printf(\"Error: x must be greater than zero\n\n\");\n\n22 exit(1);\n\n23 }\n\n24 if(b <= 1) {\n\n25 printf(\"Error: base must be greater than one\n\n\");\n\n26 exit(1);\n\n27 }\n\n28\n\n29 result = log(x) / log(b);\n\n30 printf(\"log_(%f)(%f) = %f\n\n\", b, x, result);\n\n31 return 0;\n\n32 } Code Sample 16.2.: Logarithm Calculator Program in C"
Related Ideas
- How can conditionals validate logarithm inputs and handle errors?ComputerScienceOne · Life & Taxes
- How can conditionals and the change of base formula validate logarithm input in PHP?ComputerScienceOne · Life & Taxes
- How can the logarithm of a number be computed in an arbitrary base?ComputerScienceOne · Computing a Logarithm
- How can a Java program compute a logarithm in an arbitrary base?ComputerScienceOne · Computing a Logarithm
- How are if, if-else, and if-else-if statements written in C?ComputerScienceOne · If, If-Else, If-Else-If Statements
- How can conditionals verify the number of command line arguments?ComputerScienceOne · Life & Taxes
- How do you use the C standard mathematics library?ComputerScienceOne · Preprocessor Directives
- How can conditionals handle invalid inputs and special cases in a quadratic roots program?ComputerScienceOne · Quadratic Roots Revisited