Trapozoidal rule in C++
Calculate area under curved with trapozoidal rule and implemented in C++
30 Jun 2022
mathhomeworkprogramming
Introduction
First it read the data from the CSV file. Then it put the data into arrays. After that the program calculate trapezoidal area using for-loop. Finally, it output the result. NOTE: You must convert the excel file to csv file and remove the header and store it in the same directory in order to make the program work
Data
Here is the data with x and y points
Code
/*30/06/2022 By Koungmeng*/
#include <iostream>
#include <fstream>
int main()
{
char c; //useless variable haha
std::fstream File("AUC_data.csv");
double arr_x[351];
double arr_y[351];
double AUC = 0.0;
//Read file from csv and put to array
for (int i = 0; i < 351; i++)
{
File >> arr_x[i] >> c >> arr_y[i];
}
//Calculate AUC using Trapezoidal Rule
for (int i = 0; i < 350; i++)
{
AUC += (arr_y[i] + arr_y[i + 1]) * (arr_x[i + 1] - arr_x[i]) / 2; //Area = (B+b)*h/2
}
std::cout << "The area under curve using trapezoidal rule is: " <<AUC << std::endl;
std::cin.get();
return 0;
}