satellite03
IS-IT--Management
hi, say i have a sequence like below
10 12 10 10 12 14 125 0 -1 -2 14 15 12 11 3 11 ........ (in a file)
i want to store all the elements in an array...but i want to store
each element only once(no repetation) and also how many times
elements are repeting.
that is , my array will have all distinct elements.
for example, i want output for mine above sequence like below
array[0] = 10 repetation = 3
array[1]=12 repetation = 3
array[2]=14 repetation = 2
array[3] =125 repetation = 1
..............like this
how can i do it?
i wrote a code ....but its not fulfiling my desire
Code:
#include<stdio.h>
#include<stdlib.h>
int main( )
{
FILE *ptr1, *ptr2;
ptr1 = fopen("d:\\datatest.txt","r");
if(ptr1==NULL ) printf("can not open");
ptr2 = fopen("d:\\datatest.txt","r");
if(ptr2==NULL ) printf("can not open");
int repetation[50],number[50];
int static i = 0 ;
int x,y;
static long int k=0;
do
{
fscanf(ptr1,"%d",&x);
int count = 0;
do
{
fscanf(ptr2,"%d",&y);
if(x==y)
count++;
}while( !feof(ptr2) );
//printf("count=%d",count);
repetation[k] = count;
k = k+1;
number[i]= x;
i=i+1;
rewind(ptr2);
}while( !feof(ptr1));
for(int m =0;m<k; m++)
printf("%d %d\n ",number[m],repetation[m]);
close(ptr1);
close(ptr2);
}
getting output :
________________
10 3
12 3
10 3
10 3
12 3
14 2
125 1
0 1
-1 1
-2 1
14 2
15 1
12 3
11 2
3 1
11 2
"d:\lcc2\lcc\adf.exe"
Return code -1
Execution time 0.032 seconds
Press any key to continue...
although i am getting output which shows which element is repetating how many times..but they are showing multiple times.here 10 3 has come three times but i want it for only once.
my expected output:
------------------
10 3
12 3
14 2
125 1
0 1
-1 1
-2 1
14 2
15 1
11 2
3 1
.....
i am not able to get a good algo...how can i do it? i have large amount of data..