• Link to this text:
Esercizioschifo by guest on 2nd December 2019 10:18:17 PM

#include <stdlib.h>

#include <stdio.h>

#include <math.h>


#define N 200

#define NITER 20

#define NMIN 0

#define NMAX 100

#define NVAL (NMAX-NMIN+1)


int main() {

  char *filename = "ex6.txt";

  int i,j,r;

  int nlt50[NITER]; /* Array numeri < 50 */

  FILE *fptr;

 

  /*

     Si utilizza il tempo di sistema per inizializzare

     il generatore random

  */

  srand(time(NULL));

 

  for(i=0;i<NITER;i++)

    nlt50[i] = 0;

 

  for(i=0;i<NITER;i++) {

    for(j=0;j<N;j++) {

      r = rand()%NVAL;

      if(r<50)

    nlt50[i]++;

    }

  }

 

  if((fptr=fopen(filename,"w"))==NULL) {

    fprintf(stderr,"ERRORE: impossibile aprire il file '%s'\n",filename);

    return(1);

  }

  fprintf(fptr,"%-3s %4s\n","N","n<50");

  fprintf(fptr,"----------\n");

  for(i=0;i<NITER;i++)

    fprintf(fptr,"%-3d %4d\n",i+1,nlt50[i]);

  fclose(fptr);


  return(0);

}

Report Abuse

Login or Register to edit or copy and save this text. It's free.