-2

In my file includes two columns and I'm try to gather data from file. I need to compare two value in one column. For example, if array[5] is higher than array[4], do something. Here my code:

int control(double col2[], double col3[], int subscript){

  double a, b, fcontrol ;
  int k /* group */ ;  

  /* some necessary values for JD controlling */ 
  a = col2[subscript] ;
  b = col2[subscript-1] ;        

  /* for JD controlling */
  fcontrol = a - b ; 
  printf("kontrol = %.12f     a = %.12f     b = %.12f\n", fcontrol, a, b) ;  

  /* if value of between two data is equal or higher than 10 hour return 1 */ 
  if(fcontrol >= 0.416666666667){

     return 1 ;
  }
  else{

     return 0 ;
}

b is always 0. How can I fix it?

My terminal is :

kontrol = 258.426728989849     a = 258.426728989849     b = 0.000000000000

kontrol = 258.447161800788     a = 258.447161800788     b = 0.000000000000

kontrol = 258.467594711488     a = 258.467594711488     b = 0.000000000000

kontrol = 260.245248070103     a = 260.245248070103     b = 0.000000000000

kontrol = 260.265680861012     a = 260.265680861012     b = 0.000000000000

kontrol = 260.286113551461     a = 260.286113551461     b = 0.000000000000

kontrol = 260.306546441912     a = 260.306546441912     b = 0.000000000000

Here my all code :

/* TASK */
#include<stdio.h>

int kontrol(double col2[], double col3[], int subscript) ;


int main(){


   int kolon1,
       n = 0, /* for array */
       j, z, /* for "for" loopr */
       flag = 0 ; 
   int  grup = 0 ;

   double kolon2, kolon3,
          col2[100000], col3[100000] ; 


   char ignore[100]; 


   FILE *okuPtr ; 
   FILE *yazPtr ; 


   char oku_tbl[100] ;
   sprintf(oku_tbl, "deneme.tbl") ;

   /* error if file isnt opened*/
   if ((okuPtr = fopen(oku_tbl, "r")) == NULL)
      printf("%s Acilamadi", oku_tbl) ;
   /* file is opened */ 
   else{

      char yaz_tbl[100] ;
      sprintf(yaz_tbl, "deneme_data.tbl") ; 

      /* errof if file isnt opened */
      if((yazPtr = fopen(yaz_tbl, "w")) == NULL)
         printf("%s acilamadi\n", yaz_tbl) ;
      /* file is opened */ 
      else{           
         /* first read */ 
         fscanf(okuPtr, "%d%lf%lf", &kolon1, &kolon2, &kolon3) ;

         /* until end of file */ 
         while (!feof(okuPtr)){
            /* ignore first 3 line */        
            fgets(ignore, 100, okuPtr) ;


            col2[n] = kolon2 ;
            col3[n] = kolon3 ;


            flag = control(col2, col3, n) ; 


            n++ ; 

            /* if flag == 1 */
            if (flag == 1){

               for (z = 0 ; z <= --n ; z++){
                  fprintf(yazPtr, "%d\t%.12f\t%.12f\n", grup, col2[z], col3[z]) ;
               }

               n = 0 ; 
               grup++ ; 
            }

            /* yeni veri oku */ 
            fscanf(okuPtr, "%d%lf%lf", &kolon1, &kolon2, &kolon3) ;
         }

        /* diziyi yazdir 
         for (j = 0 ; j <= n-1 ; j++){
         printf("%d\t%-.12f\t%-.12f\n", k, col2[j], col3[j]) ;
         } */
      } 
   }       
return 0 ;        
}


int control(double col2[], double col3[], int subscript){


  double a, b,
         fcontrol ;
  int k /* group */ ;  

  /* some necessary values for JD controlling */ 
  a = col2[subscript] ;
  b = col2[subscript-1] ;        

  /* for JD controlling */
  fcontrol = a - b ; 
  printf("kontrol = %.12f     a = %.12f     b = %.12f\n", fcontrol, a, b) ;  

  /* if value of between two data is equal or higher than 10 hour return 1 */ 
  if(fcontrol >= 0.416666666667){

     return 1 ;
  }
  else{

     return 0 ;
  }         
}
K.A.C.
  • 11
  • 2
  • 6
  • 2
    What are you passing into this function as a subscript that's giving you these outputs? – Ricky Mutschlechner Jul 24 '15 at 16:16
  • `if(control >= 0,416666666667)` This decimal value should have `.` not `,` .This may be typo . – ameyCU Jul 24 '15 at 16:19
  • 2
    this shouldn't work at all. `if (control ...)`. ? control is your function, not a variable. you can't test a function for `>=`, unless you CALL the function and test the return value, e.g. `if (control() >=` – Marc B Jul 24 '15 at 16:20
  • 1
    and what do you do with 'col3'? – Skizz Jul 24 '15 at 16:20
  • i'm passing into function as a subscript "n" value. col2[n] = kolon2 ; col3[n] = kolon3 ; flag = control(col2, col3, n) ; n++ ; – K.A.C. Jul 24 '15 at 16:20
  • Need relevant information like what you pass in function while calling. – ameyCU Jul 24 '15 at 16:23
  • @KutayArinc my clairvoyance is on the fritz today. Pretend we don't know the value of `n`. *Do you* ?? Care to share it? If `n == 0` then you're subscripting with `b = col2[-1]`, which I sincerely doubt is a good idea. – WhozCraig Jul 24 '15 at 16:24
  • i fixed "." decimal and changed "control" to "fcontrol" and still "b" is "0". – K.A.C. Jul 24 '15 at 16:25
  • It was just a typo but please provide more information with your question. – ameyCU Jul 24 '15 at 16:25
  • The problems with the comma in `0,416666666667` and using `control` as a function pointer in `if(control >= 0,416666666667)` are each valid C code (even though they're obviously wrong). Unfortunately, neither MSVC nor GCC warn about these problems by default. Even adding the `-Wall` option only warns about the comma operator problem, not comparing a function pointer using `>=` (again on either compiler). – Michael Burr Jul 24 '15 at 16:27
  • 'n' begins "0" to unkown value because i don't know how many data there are. – K.A.C. Jul 24 '15 at 16:28
  • @MichaelBurr That is just typo problem will still persist. – ameyCU Jul 24 '15 at 16:28
  • We would need to see information about the arrays passed into the function and the actual arguments used. – Michael Burr Jul 24 '15 at 16:30
  • In `control`, you don't ever use `col3`. Also, you probably want to remove your duplicate code sample (original code, since it is now included in the "full" code). – crashmstr Jul 24 '15 at 16:55

1 Answers1

0

Problem is that when you call control for first time when n=0

 flag = control(col2,col3, n);

But notice here b=col2[subscript-1] will actually be b=col2[-1] which is creating problem as array indexing start with 0 thus your program has this behaviour .

      while (!feof(okuPtr))

It is always wrong. See here-Why is “while ( !feof (file) )” always wrong?

Instead use this -

while(fgets(ignore, 100, okuPtr))
{
  /*Your code*/
}
Community
  • 1
  • 1
ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • I thought col3 will be necessary but i guess it won'y. I must compare col2[subscript] and previous one col2[subscript-1] – K.A.C. Jul 24 '15 at 17:23