Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C by htcl ( 15 years ago )
#include <stdio.h>
#include <stdlib.h>
#define T 3

void fill(FILE* f){

    int a;
    char s[T];
    
    do{
        printf("inserire il numero");
        scanf("%d",&a);
        fwrite(&a,sizeof(int),1,f);
        printf("finito?");
        scanf("%s",s);
    }while(strcmp(s,"N")==0);
    fclose(f);
}

int readLenght(FILE* f,int* even,int* odd){

    *odd=0;
    *even=0;
    int i=0;

    do{
        fread(&i,sizeof(int),1,f);
        if((i%2)==0)
            (*even)++;
        else
            (*odd)++;
    }while(!feof(f));
    
    return (*even+*odd);
}

void main() {

    FILE* f=fopen&#40;"valori.dat","wb"&#41;;
    int a,b,c,d=0,e=0;
    fill(f);
    FILE* k=fopen&#40;"valori.dat","rb"&#41;;
    int u=readLenght(k,&a,&b);
    int* s=(int*)malloc(u*sizeof(int));
    do{
        fread(&c,sizeof(int),1,k);
        if((c%2)==0){
            s[d]=c;
            d++;
        }
        else{
            s[(a+e)]=c;
            e++;
        }
    }while(!feof(k));
    
    int l;

    for(l=0;l<(u-1);l++)
        printf("l' elemento di posto %d è: %dn",l+1,s[l]);
    fclose(k);
    free(s);
}

 

Revise this Paste

Your Name: Code Language: