Please help with my Arrays code.

This is a discussion on Please help with my Arrays code. within the C++ Programming forums, part of the General Programming Boards category; The instructions are: You will write a program that evaluates the integral of sin(x) using the left-hand rectangle rule with ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    7

    Please help with my Arrays code.

    The instructions are:

    You will write a program that evaluates the integral of sin(x) using the left-hand rectangle rule with
    2000 subintervals, over 10 intervals. The intervals to test are [0, 1), [1, 2), …, [8, 9), [9, 10). You will
    declare an array of type double that can hold 10 elements, and you will use this array to hold all 10
    results you get from evaluating each interval.
    Once you have calculated the integral as described for each interval, and the results are all stored in
    your array, you will sort the array from smallest to largest, and output each element on separate lines.
    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    double integrate(double a, double b, int n);
    double f(double x);
    
    int main()
    {
    const int Array_Size = 10;
    int numbers[Array_Size];
    int count;
    
    for(count=0; count<10; count++)
    
    cout << f(numbers[count]) << endl;
    }
    
    double integrate(double x1, double x2, int n)
    {
    double sum = 0, delta_x = (x2-x1) / n;
    for (int i=0; i<n; ++i)
    {
    sum+=f(x1+i*delta_x);
    }
    return sum * delta_x;
    }
    
    double f(double x)
    {
    return sin(x);
    }
    // I think I did something incorrect. Can you please check my code, and tell me what I did wrong? Thank you.
    Last edited by Salem; 04-14-2010 at 12:49 PM. Reason: I see dead posts.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    This is the same code and the same question as here, and therefore gets the same answer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 01:45 AM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  3. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM

Tags for this Thread


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