Advertisement
Kyrexar

Examen 02/05/12

May 2nd, 2012
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 KB | None | 0 0
  1. /* Escribir una funcion que devuelva el numero de valores positivos
  2. y negativos del fichero datos.txt, completando este main:
  3.    
  4.     #include <stdio.h>
  5.     int main(){
  6.         int npos, nneg;
  7.     // AQUI LA LLAMADA A LA FUNCION
  8.        printf("Hay %d positivos y %d negativos",npos,nneg);
  9.        return 0;
  10.     }                                                              */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14.  
  15. int posneg( int *p, int *n ){
  16.     FILE *f_in;
  17.     int num;
  18.    
  19.     f_in=fopen("datos.txt","r");
  20.     if( f_in==NULL ) printf(" No se encuentra el archivo \n ");
  21.     else{ p=0; n=0;
  22.        while( fscanf(f_in,"%f",&num) != EOF ){
  23.           if( num>0 ) *p++; // if( num%2==0) para pares
  24.           if( num<0 ) *n++; // if( num%2!=0) para impares
  25.        }
  26.        fclose(f_in);
  27.     }
  28. }
  29.  
  30. int main (){
  31.     int npos, nneg;
  32.    
  33.     posneg(&npos,&nneg);
  34.     printf(" Hay %d positivos y %d negativos \n ",npos,nneg);
  35.        
  36.     system("PAUSE");
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement