One way to estimate π is to use the following infinite series, which was discovered by Nilakantha in the 15th century:
π = 3 + (4 / (2 x 3 x 4)) - (4 / (4 x 5 x 6)) + (4 / (6 x 7 x 8)) - ...
Breaking the series down:
Write a C program that estimates π by adding together terms 0 through n, inclusive, in the Nilakantha series, and prints out that estimate to 20 decimal places.
int
.ex0
.$ gcc -Wall -g -std=c17 -o ex0 ex0.c $ ls ex0 ex0.c $ ./ex0 100 Our estimate of Pi is 3.14159241097198238535 $
As part of this exercise you will need to explore some of the basic C
libraries in order to convert the command-line argument (the string of
digit characters) to an int
value and print the result.
The nav bar above links you to
www.cplusplus.com/reference as the "C/C++ Reference", which contains
information about the C and C++ libraries (ignore the C++ info for now).
stdio.h
library for basic input and output, (2) string.h
for handling
C-strings (i.e., null-terminated arrays of characters), and (3)
stdlib.h
for some useful functions.sscanf
, atoi
, and
others; think about the tradeoffs and differences between them.math.h
library.You should handle various inputs from the user, which may be in an unexpected format. For each input, you should take some time to reason through your options for handling it gracefully (i.e., without unexpectedly crashing), decide which one seems "best", and document your decision in your code. Note that you do not need to give a fully detailed explanation; short well-written comments are fine. Example unexpected inputs for this exercise include, but are not limited to:
$ ./ex0
$ ./ex0 hello
$ ./ex0 0.05432
$ ./ex0 10000000 3234
For simplicity, one case you can ignore is when someone inputs an integer
that is too large to fit into an int
data type
(e.g., 2351 is too big to fit into 32 bits).
To avoid floating-point arithmetic errors, please make sure your code adheres to the following rules:
double
type for intermediate and final results.As a starting point, get comfortable with the C best practices mentioned in the first lecture, particularly focusing on program layout and function ordering.
Other style issues to keep in mind, even though not discussed in lecture, include: indenting your code consistently, factoring out code into helper functions when appropriate, and writing an adequate amount of useful comments.
You will also be expected to be robust in handling user input, which includes handling unexpected inputs. Refer to the section above for more details.
Make sure that you run the linter
(./cpplint.py --clint ex0.c
) and valgrind
(valgrind ./ex0 <input>
) on your code and resolve
any indicated issues before submitting.
clint.py
:
$ ./cpplint.py --clint ex0.c Done processing ex0.c
valgrind
:
$ valgrind ./ex0 <input> ... ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
You will submit:
ex0.c
.
For full credit, your code must:
attu
, or CSE home VM).gcc
and valgrind
).$ gcc -Wall -g -std=c17 -o ex0 ex0.c
math.h
is disallowed because it won't work
without the addition of the -lm
option..c
file with
your name(s) and CSE or UW email address(es).cpplint.py --clint
).