hi need help to do this programm thank you for the help!!

Develop a C-code that numerically integrates one of the following functions:

(1) f(x)=x√1+{ πsin(0.1πx))^2 E{-5,5}

(2) f(x)=-1/64 (2cosx+5cos(3x)^2)) sinx


(3) f(x)=225x(1-x^2)(1-x^2)√(1-x^2)^3

using N = 20 and N = 40, where N is the number of steps or intervals. Print out all xi, and final result.

Analytically integrate the function you choose for the interval and compare the result with the sum calculated using the above formula.

Your C-code must contain the following features: (1) use of arrays, (2) use of pointers, (3) use of structure, (4) use of union, (5) use of functions and function calls, (6) formatted output on screen and (7) saving of the same output on a file.

You need to submit a written report on the project and the report needs to follow the format below:


(xxii) Cover page (see attached as an example)
(xxiii) Summary
(xxiv) Introduction – tells what you want to do in this project
(xxv) Theory of numerical integration
(xxvi) Results and Discussion
(xxvii) Conclusion
(xxviii) Appendix
a. List of your C code
b. Sample Run of your Code
Last edited on
what do you have so far?
Wow, you must use unions? I've only seen unions in professional code once (or at least I could count it on one hand).

To integrate something check this out:

1
2
3
4
5
6
7
8
9
10
11
12
13
typedef double type_t;
typedef type_t(*func)(type_t); // func is a pointer to a double f(double) function

type_t Fintegrated( func f, type_t start, type_t end, type_t dx)
{
  if (dx == type_t(0.0)) // assume 100 steps if dx is too small
    dx = (end-start)/100.0;

  type_t output = type_t(0.0);
  for (type_t x = start; x <= end; x += dx)
    output += ( f(x) * dx );
  return output;
} 


For requirements 6 & 7, one class to rule them all:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Logger
{
protected:
  std::ofstream fout;
public:
  Logger(std::string filename) : fout(filename) {}

  template <class T>
  Logger& operator<<(T output)
  {
    std::cout << output;
    fout << output;
    return *this;
  }
};



I guess the problem here is that you can do this without arrays, structure, or unions. As for functions and pointers, you've got both covered with function pointers!
Last edited on
Topic archived. No new replies allowed.