Need help with an Algebra calculation

This is a discussion on Need help with an Algebra calculation within the C Programming forums, part of the General Programming Boards category; I have written a program to calculate the area under a curve, using the area of a trapezium rule. As ...

  1. #1
    Unregistered
    Guest

    Question Need help with an Algebra calculation

    I have written a program to calculate the area under a curve, using the area of a trapezium rule. As there are two values that vary and not one, the integration rule wont work. So I want to work out each trapezium's area and then sum them.

    I have set up two arrays, x and y. The user enters the values into x and then y. Now i need to make the calculation using the following rule, using i as the current value:

    ((x[i+1]-x[i])*(y[i]+y[i+1]))/2

    This gives the area for each trapezium. Then I need to sum them all together to give a total area and thus the area under a curve.

    P.S. the area of a trapezium:

    Area = (interval width * positive difference in heights)/2

    Can anyone help me with the for statements to do this please?

  2. #2
    Unregistered
    Guest
    EDIT:

    the area of a trapezium SHOULD read:

    Area = (width * (height left + height right))/2

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    You want to add the values up while you run along the for loop. It is very inefficient to actually store the values and then add them up later.
    Code:
    int numValues = the number of values
    int i;
    float area = 0;
    
    // numValues - 1, for example, the points (0, 0), (1, 3), (3, 1)
    //  represent two trapeziums.
    for (i = 0; i < numValues - 1; i++)
    {
     area += ((x[i+1]-x[i])*(y[i]+y[i+1]))/2 
    }
    return area;
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Date calculation program
    By putty88 in forum C Programming
    Replies: 5
    Last Post: 04-17-2009, 08:24 AM
  2. Help with calculation and loop
    By Betty in forum C Programming
    Replies: 7
    Last Post: 04-10-2006, 06:37 AM
  3. algebra solver
    By treenef in forum C++ Programming
    Replies: 20
    Last Post: 03-18-2005, 12:12 PM
  4. Linear Algebra API
    By curlious in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-13-2005, 05:34 PM
  5. help with pay calculation program
    By shugstone in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2003, 09:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21