CEG220: Introduction to C Programming for Engineers – I

Section 2

 

Project 3: Integration Simulation: RAM

 

Program Background:

Frequently, you will want to integrate a function over a certain interval without having to perform the integration yourself. Instead, we can use the rectangle approximation method – or RAM – to simulate integration. We can simulate it by left-centered rectangles, right-centered rectangles, or middle-centered rectangles. For more accurate (but slightly more work computationally), we can use the trapezoidal rule.

 

Recall that the area formulas are as follows:

Arectangle = length x width

Atrapezoid = (base2 + base2) * height / 2

 

So, for the equation of a semi-circle of radius 1, y = √ ( 1 - x2  ), we can estimate area between the curve and the y axis using LRAM (left-centered rectangles), RRAM (right-centered rectangles), and the trapezoidal rule:

 

 

An integral ∫ f(x) dx over the interval [a, b] is the same thing as the infinite sum of all the rectangles of between f(x) and the x-axis, where x ε [a, b]. When you boil an integral down to its basics, dx is basically the width of the rectangle, and f(x) is the height! The example of f(x) = √( 1 - x2  ) can be difficult to integrate; it requires the use of trigonometric substitutions. It becomes the nasty integral of    ∫ sin2 t dt with different bounds (instead of [a,b]). Frequently, you will need to integrate such functions, and you don’t want to spend 30-60 minutes trying to integrate it. Since the rectangle approximation method only requires m computations of the value of f(x) (where m is the number of rectangles), LRAM or RRAM or the Trapezoidal Rule might be the best means of approximating ∫ f(x) dx.

 

Program Description:

Your task is to implement a function LRAM(), RRAM(), and TRAP() that each take a function as a parameter (you will be shown this in class), the values for a and b, and the number of rectangles / trapezoids – and should return the sum. You should test these functions by giving them a variety of functions, values for a and b, and rectangles. You should also test boundary cases. Boundary cases are cases that are “on the edge” of the Boolean test. For example, in the test x < 50, you should test for x = 49, x = 50. Furthermore, if you hadn’t already done so, you should also check for x <= 0, as many errors come from performing the function of a negative number.

 

Program Requirements:

1)      Your program must comply with the class programming standard

2)      You should justify what loops you are using and why in a one-line comment and in your program specification

3)      You must submit a hand-written program specification detailing (1) your approach to solving the problem, (2) the algorithm(s) you used, and (3) justifying the data types you have used

 

Grading Standard:

This project is worth 100 points. Please note that programs that do not compile or do not link will lose all points related to run-time requirements (examples of run-time requirements includes but is not limited to correct run output, handling certain cases adequately, etc.). Grading standard will be followed strictly:

 

25 points:         Preparatory Requirements

·        Compliance with class coding standard (4 points each)

·        adequate comments – clear, concise, useful; not excessive

·        well-named identifiers

·        follows program specification

·        hand-written document / program specification (13 points)

·        all algorithms designed properly

·        Uses pseudo-code to express the problem and how to solve it

 

75 points:         Run-time Requirements

·        Implements all three algorithms correctly (20 points each)

·        LRAM

·        RRAM

·        TRAP

·        Code is modularized (5 points)

·        Run output (10 points)

·        Must include at least 3 test cases for each boundary case

·        Must include at least 2 invalid inputs that cause an error message to pop up

·        Please note that failure to display error messages when bad input is entered is considered non-compliant with the coding standard. Furthermore, you are not to use “abort()” to get out of the application. The only way you should be exiting your program is within main with the command “return” and a value – and preferably only using “return” once.

 

Due Date:

This project is due on 10/8 at the beginning of class. If your assignment is late – by 5 minutes or (the maximum) 2 days, the penalty is 30% (see syllabus).