Skip to content

Instantly share code, notes, and snippets.

@mcanvar
Last active June 11, 2016 07:29
Show Gist options
  • Save mcanvar/28d13b7195703c4ba8459c8c9473f010 to your computer and use it in GitHub Desktop.
Save mcanvar/28d13b7195703c4ba8459c8c9473f010 to your computer and use it in GitHub Desktop.
Parallel programming final exam 3rd question. C function that calculate trapezoidal area.
/*
Paralel Programlama Final Soruları
Soru 3) Argüman olarak n sayısını alan ve geriye trapezoidal alanı döndüren C fonksiyonunu yazınız.
[email protected]
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
#define A_POINT 0.0
#define B_POINT 0.8
double f(double x) {
return 0.2 + 25 * x + -200 * pow(x, 2) + 675 * pow(x, 3) - 900 * pow(x, 4) + 400 * pow(x, 5);
}
/* like the coursebook page:216 */
double calculateArea(int n) {
double approx, h = 0.0, x_i;
h = (B_POINT - A_POINT) / n;
approx = (f(A_POINT) + f(B_POINT)) / 2.0;
for (int i = 1; i <= n - 1; i++) {
x_i = A_POINT + i * h;
approx += f(x_i);
}
approx = h * approx;
return approx;
}
/* from the book's examples*/
double Trap(int n) {
double integral;
double h = (B_POINT - A_POINT) / n;
int k;
integral = (f(A_POINT) + f(B_POINT)) / 2.0;
for (k = 1; k <= n - 1; k++) {
integral += f(A_POINT + k*h);
}
integral = integral*h;
return integral;
}
int main(int argc, char* argv[]) {
printf("%lf\n", calculateArea(8));
system("PAUSE");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment