I'm really confused as to how to set up this program. The program is to have a function "trap", so I'm guessing that means I have to include a function that solves the integral before my main function?

#include <stdio.h>
#include <math.h>

double f(double x)
{
    return exp(-x*x);
}
double f(double x)
{
    return exp(-x);
}
float a;
float b;
float x;
float h;
float sum;
float s;
int n;
int i;

int trapeze(int n, int i)
{
    h=(b-a)/n;
    s=(0.5*h)*(f(a)+f(b));
    for (i = 1; i < n; i++) 
    {
        sum = s+h*f(a+(i*h));
    }
}

int main(void)
{
    printf("Enter the number of intervals: ");
    scanf("%d", &n);
    
    return 0;
}

So basically we're given 2 functions: f(x) = e^(-x^2) and f(x) e^(-x). The program is suppose to solve both them them depending on number of interval the user inputs. I've sat here for 3 hours trying to figure this out, and I've been just throwing codes together. Hoping to get a good insight on how to better approach this. I believe I have the function that inputs the trapezoidal rule correctly.

Sounds like you need to learn some coding techniques. "just throwing codes together" rarely results in a working program. And "I believe I have the function that inputs the trapezoidal rule correctly" is also a recipe for disaster. If you don't...

Questions:
1) Can you solve the equations by hand?
   a) If not, you need to figure this out. You can't program what you don't know.
   b) If so, write down the steps (in words) you take to solve it. 
      This is partially where your [I]code[/I] comes from.
2) Can you test your function with known values that have a known answer?
   a) If not, you will never be sure if it's right or not.
   b) If so, good. Do it.  
      Test as many possibilities as you can. They [B]all[/B] must work

C is not FORTRAN: It is not "made for mathematics". C is a system programming language, and as such, you must understand that it can't magically figure out what relationship your functions f() are to each other by context. To make this work in C, as WaltP explained, you must construct a series of steps that can be worked out by a human, then map that logic to a series of steps in the C language that emulates that procedure.

Also worth noting, your code produces no output and does no work because the functions that you wrote is never used by the main function.

commented: You also can't make it work in ForTran as you are implying. forTran has the same limitations a C in this context. +17

I think you completely missed my point. I threw it together to brain storm how I'm going to approach the logic of it. Instead of helping, you criticized my approach. All I asked was for help brain storming how I could approach this program. I don't care if C wasn't designed for mathematics, it was an assignment and I had to do it whether or not C was designed for mathematics. Neither of you bothered to post something similar, that could've helped.

If you're going to break down the post "I believe I have the function that inputs the trapezoidal rule correctly", what else am I suppose to say? "My function is absolutely correct." Of course I checked whether or not it works for all the variables that I entered. The phrase suggests that if other posters found it incorrect, then please let me know.

I found several tutorials to programs that achieve a similar mathematic problem. Reworked my program to operate similarly and function like it should.

The trapezoidal function works like its suppose to and fixed the loop to start at 0, the main function calls the trapezoidal function and inputs the two functions it was suppose to solve and prints them.

Could've pointed me to some tutorials instead of being pricks about it.

You may have meant your post that way but obviously that's not what your post said to us. And if we're pricks for misreading your ambiguous phrasing, so be it.

Miscommunication doesn't start with the listener.

If you'd like me to break down your post to show you how and why I misinterpreted it, I will. I'm sure you won't want me to, but the offer is there...

I think you completely missed my point. I threw it together to brain storm how I'm going to approach the logic of it. Instead of helping, you criticized my approach. All I asked was for help brain storming how I could approach this program. I don't care if C wasn't designed for mathematics, it was an assignment and I had to do it whether or not C was designed for mathematics. Neither of you bothered to post something similar, that could've helped.

All criticism is healthy criticism in my opinion. You are right about your post being a logic issue and not a programming issue -- this is exactly why I made a point to distinguish the two things. Your question implied that the programming language could do most of this logic on its own. The Trapezoidal formula you provided, in order to be compatible with C, must be broken down into more steps.

Could've pointed me to some tutorials instead of being pricks about it.

I'm sorry you feel that way. The experts on this forum are generally better with C-specific questions than mathematical formulas. Had you provided the broken down logic, I would have been more than happy to help you turn that logic into C code complete with best practice tips.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.