Original question is above, and is for C programming. As you will see in my code below, I am not sure how to properly implement the functions I made for g(x) and h(x) (I call them g_x and h_x) into my function called trap. I know that in my main function in my function call to trap, I have to put something in the parentheses of g_x() and h_x() since they both take a type double argument, however I'm not sure what to write since ultimately the arguments they take will actually come from the function trap
/*
Create a function "trap" with input parameters a, b, n, and f that
implements the trapezoidal rule
to approximate the integral of f(x) from a to b. Call trap with
values for n of 2, 4, 8, 16, 32, 64, 128
on functions g(x) = x^2sinxx (a = 0, b = PI) and h(x) = sqrt(4-x^2)
(a = -2, b = 2)
*/
#include
#include
#define PI 3.14159
double g_x(double x);
double h_x(double x);
double trap(int n, double a, double b, double f(double));
int main(int argc, char *argv[]) {
int n;
printf("TRAPEZOIDAL RULE g(x) = x^2sin(x), a = 0, b =
PI\n");
for (n = 2; n <= 128; n *= 2)
{
printf("for n = %d, area is
approximately %.4f\n", n, trap(n, 0, PI, g_x()));
}
printf("\n");
printf("TRAPEZOIDAL RULE h(x) = sqrt(4 - x^2), a = -2,
b = 2\n");
for (n = 2; n <= 128; n *= 2)
{
printf("for n = %d, area is
approximately %.4f\n", n, trap(n, -2, 2, h_x()));
}
return 0;
}
double trap(int n, double a, double b, double f(double))
{
double h = (b - a) / n;
double riemann_sum = 0;
double area_approximation;
double i;
for (i = a + h; i <= b - h; i += h)
{
riemann_sum += f(i);
}
area_approximation = (h / 2) * (f(a) + f(b) + 2 *
riemann_sum);
return area_approximation;
}
double g_x(double x)
{
return pow(x, 2) * sin(x);
}
double h_x(double x)
{
return sqrt(4 - pow(x, 2));
}
THE PROBLEM IN YOUR CODE IS THAT WHEN YOU CALL THE METHOD TRAP, AND PASSING ANOTHER FUNCTION CALL AS PARAMETER WHICH WILL SIMPLY MEAN THAT FIRST THE FUNCTION g_x() OR h_x() WILL BE CALLED.
OR
ANOTHER METHOD CAN'T BE PASSED AS PARAMETER IN A METHOD AS THIS.
IN THIS CASE THE PARAMETER THAT WILL BE PASSED WILL BE THE RESULT OF THAT ANOTHER METHOD.
THUS, JUST PASS SOME char DATA WHICH WILL BE USED TO IDENTIFY WILL FUNCTION SHOULD BE CALLED AND USE IF-ELSE CONDITION.
THE CODE IS AS FOLLOWS-
#include<stdio.h>
#include<math.h>
#define PI 3.14159
double g_x(double x);
double h_x(double x);
double trap(int n, double a, double b, char F);
int main(int argc, char *argv[])
{
int n;
printf("TRAPEZOIDAL RULE g(x) = x^2sin(x), a =
0, b = PI\n");
for (n = 2; n <= 128; n *= 2)
{
printf("for n = %d, area is approximately %.4f\n", n,
trap(n, 0, PI, 'g'));
}
printf("\n");
printf("TRAPEZOIDAL RULE h(x) = sqrt(4 - x^2), a
= -2, b = 2\n");
for (n = 2; n <= 128; n *= 2)
{
printf("for n = %d, area is approximately
%.4f\n", n, trap(n, -2, 2, 'h'));
}
return 0;
}
double trap(int n, double a, double b, char F)
{
double h = (b - a) / n;
double riemann_sum = 0;
double area_approximation;
double i;
if(F=='g')
{
for (i = a + h; i <= b - h; i +=
h)
riemann_sum += g_x(i);
area_approximation = (h / 2)
* (g_x(a) + g_x(b) + 2 * riemann_sum);
return
area_approximation;
}
else
{
for (i = a + h; i <= b -
h; i += h)
riemann_sum += h_x(i);
area_approximation = (h / 2)
* (h_x(a) + h_x(b) + 2 * riemann_sum);
return
area_approximation;
}
}
double g_x(double x)
{
return pow(x, 2) * sin(x);
}
double h_x(double x)
{
return sqrt(4 - pow(x, 2));
}
Original question is above, and is for C programming. As you will see in my code...
So I have a question in regards to my program. I'm learning to program in C and I was curious about the use of functions. We don't get into them in this class but I wanted to see how they work. Here is my program and I would like to create a function for each of my menu options. I created a function to display and read the menu and the option that is entered, but I would like to...
NEED CODE HELPS WITH C PROGRAMMING. ISSUES EXPLAINED IN BOLD. COMPARING TWO LETTERS AND CALLING A FUNCTION. Problem are explained in bold #include <stdio.h> #include <string.h> #include <stdlib.h> #define pi 3.1415 void Area (double n); void VolSphere (double n); void Circumference (double n); void AllCalc (); // i dont know if the declaration of the function is correct AllCalc = AllCalculations //function main begins program execution int main (void) { puts("-Calculating Circle Circumference, Circle Area or Sphere Volume-\n"); void (*f[4])...
Cant find what is wrong to fix the code. can you please fix the code. The function is: sin^-1 (x/5) #include <iostream> #include <iomanip> #include <math.h> #include <cmath> using namespace std; double f(double x) { return pow(sin, -1)*(x / 5); } int main() { int i, n; double a = 0.0, b =1.0, h, s = 0.0, x, xbar; double s1, s2, s3; cout << " Enter # of partitions n "; cin >> n; cout << " n =...
C++ Could you check my code, it work but professor said that there are some mistakes(check virtual functions, prin() and others, according assignment) . Assignment: My code: #include<iostream> #include<string> using namespace std; class BasicShape { //Abstract base class protected: double area; private: string name; public: BasicShape(double a, string n) { area=a; name=n; } void virtual calcArea()=0;//Pure Virtual Function virtual void print() { cout<<"Area of "<<getName()<<" is: "<<area<<"\n"; } string getName(){ return name; } }; class Circle : public...
C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string using the RSA algorithm (C90 language only). It can contain ONLY the following libraries: stdio, stdlib, math and string. I want to use the following function prototypes to try and implement things: /*Check whether a given number is prime or not*/ int checkPrime(int n) /*to find gcd*/ int gcd(int a, int h) void Encrypt(); void Decrypt(); int getPublicKeys(); int getPrivateKeys();...
From the following code, define a new structure and use it to define the upper and lower limits of the trapezoidal and simpsons functions. #include<stdio.h> #include "math.h" #include <stdlib.h> double F1(double x) { return 1000 * exp(7*x) * cos(0.3*M_PI*x); } double F2(double x) { return pow(x,3) - (0.23*x) + 30.67; } void calculateIntegeralBySimpson(int n) { FILE *fp = fopen("data_Simpson_method.txt", "w"); int i; /* Calculating for first half of the interval */ double lowerLimit = -5, upperLimit = 0, x[n+1],...
I wrote a program which computes the area and perimeter of a square, circle, or rectangle. As you will see in my main function, there is a for loop in which the user is supposed to be able repeat the program until they enter "q" to quit. However, my program runs through one time, the prompt appears again, but then it terminates before the user is allowed to respond to the prompt again. I'm not able to debug why this...
I have this code, but I need a flowchart that shows how my program works, and the inputs and outputs??? #include <iostream> #include <cmath> using namespace std; int fact(int n) { int i; int c = 1; for(i=1; i<=n; i++) { if(n==0) c = 1; else c = c*i; } return c; } double ex(int n,double x) { int i; double c = 0.0; for(i=0; i<=n; i++) { c = c + (double)(pow(x,i)/ (double)fact(i)); } return c; } double sinx(int...
c++ Please help! i keep getting an error message. I think my main problem is not understanding the circle calculations function and how both the area and the circumference is returned from the function. I know & needs to be used, but I am unsure how to use it. Copy the following code and run it. You should break it into the following 3 functions getValidinput - which asks the user to enter the radius and then make sure that...
PLEASE TYPE OUT IN TEXT (please no pdf or writing) C++ CODE Consider the following program in which the statements are in the incorrect order. Rearrange the statements in the following order so that the program prompts the user to input: The height of the base of a cylinder The radius of the base of a cylinder The program then outputs (in order): The volume of the cylinder. The surface area of the cylinder Format the output to two decimal...