Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void quick(float *n, int ct);
int comparare(const void *arg1, const void *arg2);
int non_unique( float *numere, int i);
int main()
{

FILE *pfile1,*pfile2,*pfile3;
pfile1=fopen("file1.txt", "r");
pfile2=fopen("file2.txt", "r");
pfile3=fopen("file3.txt","w");
float nr,nr2,md;
while(fscanf(pfile1,"%f", &nr)!=EOF) // we introduce the elements
in the new folder
fprintf(pfile3,"%f\n", nr);
while(fscanf(pfile2,"%f", &nr2)!=EOF)
fprintf(pfile3,"%f\n", nr2);
fseek( pfile1,0,SEEK_SET );
fseek( pfile2,0,SEEK_SET );
while(fscanf(pfile1,"%f", &nr)!=EOF && fscanf(pfile2,"%f", &nr2)!
=EOF )
{
md=(nr+nr2)/2.0;
fprintf(pfile3,"%f\n", md);
}
while(fscanf(pfile2,"%f", &nr2)!=EOF)
{
fscanf(pfile2,"%f", &nr2);
md=nr2/2.0;
fprintf(pfile3,"%f\n", md);
}
while(fscanf(pfile1,"%f", &nr)!=EOF)
{
fscanf(pfile1,"%f", &nr2);
md=nr2/2.0;
fprintf(pfile3,"%f\n", md);
}
fclose(pfile1);
fclose(pfile2);
fclose(pfile3);
pfile3=fopen("file3.txt","r");
int ct=0;
while(fscanf(pfile3,"%f", &nr)!=EOF)
{ct++;}
float *numere=NULL;
numere=(float*)malloc(ct*sizeof(float));
fseek( pfile3,0,SEEK_SET ); int i=0;
while(fscanf(pfile3,"%f", &nr)!=EOF)
{ *(numere+i)=nr; i++;
}
quick(numere, ct);
for(i=0;i<ct;i++)
{printf(" %f ", *(numere+i));}
printf("\n The number of non-unique elements is:%d",
non_unique(numere, ct));
fclose(pfile3);
free(numere);
_getch();
return 0;
}

void quick(float *numere, int n)


{

qsort((float*)numere, n, sizeof(float), comparare);

}
int comparare(const void *arg1, const void *arg2) // comparing function
for qsort
{
if (*(float*)arg1<*(float*)arg2)
return -1;
if (*(float*)arg1 == *(float*)arg2)
return 0;
if (*(float*)arg1>*(float*)arg2)
return 1;
}
int non_unique( float *numere, int ct) // function to see how many
values repeat
{
int n=0;
for(int i=0;i<ct-1;i++)
{
if(*(numere+i)==*(numere+i+1))
n++;
}
return n;

You might also like