Hi there
I have written the following code to read some data from a file and bin the data into bins of equal size so I can plot this data easily.

#include <stdio.h>

#define N_HITS 1800
#define N_TIME_BINS 78
   int main()
     {
     int i, time_bins[N_TIME_BINS],j;
	 double event;
     double hit[N_HITS],value;
     FILE *fp, *fw;
     fp = fopen("damadata.txt" , "r");
     fw = fopen("bindata.txt", "w");
            
 for (i=0; i<N_HITS ; i++)  /* enter loop to set array elements to zero*/
     {
       hit[i] = 0;
     }
 for (i=0; i<N_TIME_BINS ; i++)
     {
       time_bins[i] = 0;
     }
            
 if (fp != NULL)
     {                                 
        for(i=0; i<N_HITS; i++)                      /*Enter loop to read all lines of data into hit array*/
          {
          fscanf(fp,"%lf\n", event);                 /*Each line is read into a seperate array element*/
          hit[i]=event; 
          }                                 
     for (i = 0; i < N_HITS; i++)                  /*Now cycle through array containing data elements*/
         {                                          /*Open Loop 1*/                          
            for (j = 0; j < N_TIME_BINS; j++)
            {
               value = hit[i];                       /*Value now equal to element i of array*/                     /*enter loop to bin data into 77 bins of group of 60*/
            if (value >= (j*60) && value < ((j+1)*60))    /*Tests if the value lies within a particular boundary*/
                {
                time_bins[j] = (time_bins[j] + 1);                       /*If it is add 1 element into bin j*/
                }                                  
            else
                { 
                time_bins[j] = (time_bins[j]);                       /*If not element j remains the same*/
                }
           }                                             /*Close Loop 1*/
        }                                             /*Close Loop 2*/
    for (j = 0; j <N_TIME_BINS; j++)                       /*Loop to print the number of data points in each bin*/
          {
          fprintf(fw, "%d\t" , j);       /*Prints the bin number and the number of elements in this bin*/
	      fprintf(fw, "%d\n" , &time_bins[j]);
          }
   }
   else {
         printf("Error: The file you specified could not be found!");
        }
          fclose(fp);
          fclose(fw);
     return(0);
}

However I keep getting the following error messages which cause my programme to crash and I am unsure how to fix them and wondered if someone could possibly help me? Many thanks :)

Building pellab.obj.
C:\Users\Toshiba\Documents\Pelles C Projects\pellab.c(27): warning #2234: Argument 3 to 'fscanf' does not match the format string; expected 'double *' but found 'double'.
C:\Users\Toshiba\Documents\Pelles C Projects\pellab.c(48): warning #2234: Argument 3 to 'fprintf' does not match the format string; expected 'int' but found 'int *'.
C:\Users\Toshiba\Documents\Pelles C Projects\pellab.c(8): warning #2116: Local 'event' is used without being initialized.
Building pellab.exe.
Done.

fscanf(fp,"%lf\n", event);

requires address of variable so use

fscanf(fp,"%lf\n", &event);

lly,

fprintf(fw, "%d\n" , &time_bins[j]);

does not need address to print

fprintf(fw, "%d\n" , time_bins[j]);

is sufficient.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.