Next: About this document
Using the vi editor, write a C program on farman that approximates
the definite integral of the function f as defined in the file
~taw2/pub/integrate.c
. More detailed specifications include:
- The integral of a function f between integer values a and b
can be approximated as follows:
For example, the integral of from 2 to 5:
- Your program should prompt the user for two integers a and b,
smaller integer first. If the user enters a smaller integer for the
second number (i.e. b < a), your program should print an error message
and prompt for the two integers again. This should continue until the user
enters two integers with .
- Use the following C code to get an integer value from the user and
place it into an integer variable
a
. (Feel free to change the
prompt as needed.)
printf("Enter the lower bound of the definite integral: ");
scanf("%d", &a);
You can assume that the user will not enter anything except an integer (i.e.
not a float or alphabetic letters). - Approximate the integral of the function f (from
~taw2/pub/integrate.c
) from the lower bound given by the user to the
upper bound given by the user by implementing the above formula.
Your program should not prompt the user for a function - it should only
approximate the integral of the given function f. - The result of the approximation should be assigned to a double
variable (for example,
approx
), and then the value of this variable should be printed.
This can be done using the following C code:
printf("An approximation of the integral is: %lf.\n", approx);
- Suggested strategy:
- Copy the file
~taw2/pub/integrate.c
and modify the comments
appropriately. - Inside
main()
, add the code that prompts for appropriate
bounds and continues prompting until the user enters acceptable bounds.
Compile your program and test that this part works correctly. You will
have to use the -lm
flag for each compilation. For example:
gcc integrate.c -lm -o integrate
- Add code to print all of the integers from the lower bound a to the
upper bound b. Compile and test your program again at this point.
- Modify your code to print the value of f at each value from
a + 1 to b. In other words, your program should print f(a + 1),
f(a + 2), , f(b - 1), f(b).
Compile and test your program again.
- Finally, modify your code to compute and print the approximation.
- Here is an example of output that your program might produce. The
approximation printed is for the given function f, so definitely test your
program on this case:
Enter the lower bound of the definite integral: 5
Enter the upper bound of the definite integral: 1
Error: upper bound must be >= to lower bound!
Enter the lower bound of the definite integral: 1
Enter the upper bound of the definite integral: 5
An approximation of the integral is: 63.667345.
- Don't forget that control-C can be used to break out of
an infinite loop. If your program runs for more than a few seconds, you
will probably need to stop it this way.
- To hand in:
- Either turn in a print out of your source code, or email me
(wahls@psu.edu) the file containing your source code.
If you email me the program, I will email you your score and any comments
on your program sometime after the due date. I will also save a copy of
your program to prevent later disputes.
- Your code will be graded on correctness, readability, the clarity
of messages printed for the user and appropriate use of comments.
- Extra credit: (5 additional points) Modify your program so that it uses
smaller increments when doing the approximation - i.e. so that looks at
rectangles of width 0.5 rather than width 1. You will have to modify the
prototype of function f and the summation formula for this to work
correctly.
Next: About this document
Tim Wahls
Wed Feb 5 14:56:02 EST 1997