I know how to make a simple program for calculating the arithmetic mean, but how to make this incomprehensible

Use a 32-bit floating-point type ( float , single ) to find the arithmetic average of F(8+30) 10,000 values. F(x)=sin(x)cos(x) To calculate, create a program in an arbitrary (any) programming language and perform the calculations:

  • a) Add in a cycle 10,000 times the values ​​of F(8+1) , divide the result by the number of terms 10,000, write the result.
  • b) Add 10,000 times the F(8+1)/10000 times in a loop, write the result.
  • c) Knowing the exact result of the arithmetic mean (the same numbers are added 10,000 times, and the result is divided by 10,000, therefore the result should be equal to the value of F(N+1) ), find the relative and absolute error of the calculations

    1 answer 1

    That is, it is a problem to write a code like

     #include <iostream> #include <cmath> using namespace std; #define NUM 10000 float f(float x) { return sin(x) * cos(x); } float v1(float a) { float r = 0; for (int i = 0; i < NUM; i++) { r += f(a); } return r / NUM; } float v2(float a) { float r = 0; for (int i = 0; i < NUM; i++) { r += f(a) / NUM; } return r; } int main() { // your code goes here float arg = 30e-8; float r1 = v1(arg); float r2 = v2(arg); float r3 = f(arg); std::cout << "r1 = " << r1 << "\nr2 = " << r2 << "\nr3 = " << r3 << std::endl; float d1 = (r3-r1)/r3 * 100; float d2 = (r3-r2)/r3 * 100; std::cout << "error1 = " << d1 << "%\nerror2 = " << d2 << "%" << std::endl; return 0; } 

    (in the code there is a strange thing like 8 + 30, N + 1. I think this is just an unfinished form of the exponential notation)

    • Interesting, minus for writing code? - KoVadim
    • I did not put anything at all, thanks for the answer (PS how can I even put something to the one who answered?) I found everything, put a plus. But I can’t bet, you need to have 15 points there ((( - Snake
    • Minus was with the flag, no more. - Nick Volynkin