Justin_Finland
Justin_Finland

Reputation: 199

How to save integers in array line by line and calculate unique integers

I want to calculate the number of unique integers per line. The output I desire is:

unique: 6
unique: 3
unique: 5
unique: 5
unique: 1
unique: 1
unique: 7

UPDATE:

   fgets(line, sizeof(line), fp) ;

   int value;
   int index1 = 0;
   int count1;

   for(count = 0; sscanf( &line[index1], "%d%n", &value, &index1 ) == 1 ; count1++) {
    array[count1] = value ;
   }

  int last = sorted[0] ;
  int unique = 1 ;
  for( int i = 1; i < n; i++ ){
    if( sorted[n] != last ){
        last = sorted[n];
        unique++ ;
    }
  }
}


    fclose(fp);
    return 0;
}

So can array[count] be used in place of sorted[0]? And where am I getting the value of n from?

Therefore, I want to read in the file line by line, and calculate the number of unique integers per line (with the output you see above). I do not want hardcoded array values and I do not want only the first unique integer(I want all unique integers per line). Any suggestions? Please please I am so stuck

UPDATED: I performed a quick sort and can get the values line by line in descending order. I applied the uniqueness algorithm and when I print the value of unique, it is always 2.

qsort(array, count, sizeof(int), int_cmp);
      int n;
      for (n=0; n<count; n++)
       printf ("sorted array:%d\n ", array[n]);

      int last = array[0] ;
      int unique = 1 ;
      int i;
      for( i = 1; i < n; i++ ){
        if(array[n] != last ){
        last = array[n] ;
        unique++;
       }
      }
    printf("unique:%d", unique);

OUTPUT:

sorted array:6
 sorted array:5
 sorted array:5
 sorted array:5
 sorted array:4
 sorted array:4
 sorted array:3
 sorted array:2
 sorted array:1
 unique:2
sorted array:62
 sorted array:48
 sorted array:14
 sorted array:14
 unique:2
sorted array:9
 sorted array:7
 sorted array:5
 sorted array:3
 sorted array:1
 unique:2
sorted array:5678
 sorted array:1234
 sorted array:789
 sorted array:456
 sorted array:123
 unique:2
sorted array:34
 sorted array:34
 sorted array:34
 sorted array:34
 sorted array:34
 unique:2
sorted array:1
 unique:1
sorted array:7
 sorted array:7
 sorted array:7
 sorted array:6
 sorted array:5
 sorted array:5
 sorted array:4
 sorted array:4
 sorted array:4
 sorted array:4
 sorted array:3
 sorted array:3
 sorted array:2
 sorted array:2
 sorted array:2
 sorted array:2
 sorted array:2
 sorted array:1
 sorted array:1
 sorted array:1
 unique:2

UPDATE WITH DEBUGGING:

sorted array:6
 sorted array:5
 sorted array:5
 sorted array:5
 sorted array:4
 sorted array:4
 sorted array:3
 sorted array:2
 sorted array:1
 i:1
n:9
last:6
:unique:2
sorted array:62
 sorted array:48
 sorted array:14
 sorted array:14
 i:1
n:4
last:62
:unique:2
sorted array:9
 sorted array:7
 sorted array:5
 sorted array:3
 sorted array:1
 i:1
n:5
last:9
:unique:2
sorted array:5678
 sorted array:1234
 sorted array:789
 sorted array:456
 sorted array:123
 i:1
n:5
last:5678
:unique:2
sorted array:34
 sorted array:34
 sorted array:34
 sorted array:34
 sorted array:34
 i:1
n:5
last:34
:unique:2
sorted array:1
 unique:1
sorted array:7
 sorted array:7
 sorted array:7
 sorted array:6
 sorted array:5
 sorted array:5
 sorted array:4
 sorted array:4
 sorted array:4
 sorted array:4
 sorted array:3
 sorted array:3
 sorted array:2
 sorted array:2
 sorted array:2
 sorted array:2
 sorted array:2
 sorted array:1
 sorted array:1
 sorted array:1
 i:1
n:20
last:7
:unique:2

Upvotes: 2

Views: 81

Answers (1)

Clifford
Clifford

Reputation: 93544

Read the integers into an array of integers, sort the array, and then scan the array incrementing the "unique" counter each time the value changes.

So for input:

           1 2 3 4 5 6 5 4 5`

Sorted:

           1 2 3 4 4 5 5 5 6 
unique=0   1 2 3 4   5     6

Once sorted the unique count iteration is as follows:

int last = sorted[0] ;
int unique = 1 ;
for( int i = 1; i < n; i++ )
{
    if( sorted[n] != last )
    {
        last = sorted[n] ;
        unique++ ;
    }
}

With respect to reading the data; I suggest that you read the lines using fgets(), then scan the line using sscanf().

fgets( line, sizeof(line), fp ) ;

int value ;
int index = 0 ;
int count ;

for( count = 0; 
     sscanf( &line[index], "%d%n", &value, &index ) == 1 ;
     count++ ;
{
    array[count] = value ;
}

The above method reads a line and then validates and converts to integers at the same time. When a non-numeric value or end-of-line is encountered the loop terminates. For empty lines or lines starting with a non-digit, count will be zero; otherwise it will be the total number of integers converted.

Upvotes: 2

Related Questions