I am writing in C ++. The task is to calculate the values ​​of a function using a random number generator. E <= - 0.02 a F <= 0.02. When I open the file, it is empty. Tell me what the error is.

#include <iostream> #include <stdio.h> #include <math.h> #include <cstdlib> 
  using namespace std; int main () { float t=0, b=0, c=0; bool E[1]={}, F[1]={}; char d = ';'; int i=0; FILE *file; file = fopen("kola.csv", "w+"); while (i<=1001) { E[i]=rand()<=(-0.02); F[i]=rand()<=(0.02); t=t+0.01; b=cos(t+E[i]); c=0.5*t*cos(t+F[i]); fprintf (file, "%f%c%f\n",b,d,c); i++; } fclose(file); return 0; } 
  • @Mot, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Nicolas Chabanovsky

2 answers 2

Have you looked at the output of the program?

 bool E[1]={}, F[1]={}; ... E[i]=rand()<=(-0.02); 

What is this? What can i be equal to you? More than 1? More, it means that the array should be defined large, or instead of arrays use simple variables (which would be more reasonable)


I forgot to write about the output, that the program issues a segmentation fault , which in this case just means going beyond the array

    And you don’t have a real random number generator; rand needs to be initialized with srand (time(NULL)); otherwise, it always produces the same sequence, you still need to add #include <time.h> . Strictly speaking, he can feed any unsigned, but over time, the easiest way