2

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
  • 1
    Down-voting without comment is unhelpful. There's a reasonable question in here obscured perhaps by some superfluous content and inappropriate tags, but that what comments are for - to help improve the question. – Clifford Jan 23 '16 at 16:26
  • Consider your tags carefully; is the question really about file-io and arrays? It seems to me that what you are in fact looking for is an *algorithm* - file-io and arrays do not seem to be your problem. – Clifford Jan 23 '16 at 16:28
  • @Clifford I do not understand how to apply an algorithm because I do not understand how to pull the values line by line from the file into an array to perform an algorithm. And thank you for the tag suggestion. – Justin_Finland Jan 23 '16 at 16:30
  • My answer now covers both (although may no longer match the question). Hope that helps. – Clifford Jan 23 '16 at 16:52
  • The index of the array elements you compare might have something to do with `unique` always ending up two. – greybeard Jan 23 '16 at 22:07
  • @greybeard Hello, are you referring to the indexing here: `int last = array[0] ` ? – Justin_Finland Jan 23 '16 at 22:32
  • @greybeard This: `last = array[n]` ? – Justin_Finland Jan 23 '16 at 23:11
  • @greybeard Updated. n is the total number of integers that each line of the file contains..I am not following what you are saying, sir. Please explain. (: – Justin_Finland Jan 23 '16 at 23:19
  • @greybeard So n never changes because n starts at 20 (let us say) and then is less than i and is a position in the array....so I have to decrement n? I traced it by hand too and do not understand. Edit: Decrementing does not make a different. – Justin_Finland Jan 23 '16 at 23:40
  • 1
    Generally: comment your code (you started (good), but there is more: near the top, state the purpose of the code in the snippet/file. Continue [here](http://stackoverflow.com/q/2592030/3789665)). Declare control variables of `for`-loops in its first expression and temporary variables locally in a block as the loop body. If you are confused what your variables are, choose expressive names. If you don't use a debugger to step through the execution, add logging or _debug output statements_. What is the iteration variable in the `for`-loop intended to count unique values, what do you use to index? – greybeard Jan 24 '16 at 00:52
  • @greybeard I will do that, sir. I solved the issue also. – Justin_Finland Jan 24 '16 at 00:59

1 Answers1

2

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.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • Ok. I am having trouble with reading the integers line by line from the file into an array of integers. Please see my edit of what I am printing when trying to add the integers to an array line by line. – Justin_Finland Jan 23 '16 at 16:21
  • @Justin_Finland : The text of your question clearly states: *"I want to read in the file line by line (done)"*, so I naturally assumed that that was not part of the question. Don't make the question too large and diverse - ask one question about reading the file, and another about the algorithm. If you conflate the issues, you may then get otherwise good answers that only cover part of your question, or bad answers that cover it all - in which case which would you accept? – Clifford Jan 23 '16 at 16:33
  • I am sorry. I realized that part was not done and forgot to change that statement (Updated removing that and unnecessary code).I just do not know how to apply both at the same time..I have seen tutorials that sort with an array of integers but not get that array of integers value from a file line by line. – Justin_Finland Jan 23 '16 at 16:36
  • The question title is "Calculate number of unique integers per line" but you seem more concerned about the data input. I strongly suggest that you either split the question in tw (or more), or at least make it very clear what you want answers to - perhaps with a summary question bullet list. – Clifford Jan 23 '16 at 16:46
  • I implemented the code involving the file reading. I am not understanding how to apply that array to the sorting technique..is sorted[0] suppose to be array[count] in this case? And where does n in the sorting code come from? I will edit my answer with my updated code. Thanks. – Justin_Finland Jan 23 '16 at 17:19
  • @Justin_Finland : It is just illustrative, you can implement it as appropriate to your application. I used the name `sorted` merely to indicate that it was a sorted array in that fragment. You might sort the array in place, or if for some reason the original order is required, you might first take a copy of the data and sort the copy. `n` is simply the number of elements in the array. I wrote the fragments independently, after you clarified the question and used `count` for the same purpose in the second fragment. – Clifford Jan 23 '16 at 19:08
  • Ok, I understand. I am working on the sorting algorithm part right now. (: – Justin_Finland Jan 23 '16 at 19:11
  • @Justin_Finland : Sorting is already provided by the standard library [`qsort()`](http://www.cplusplus.com/reference/cstdlib/qsort/) function. – Clifford Jan 23 '16 at 19:14
  • Thank you for your suggestion. Please see my updated about the function qsort(). I read the hyperlink reference and looked at other examples on SO and other programming websites. I get a segmentation fault at unsigned int (*array)[count]; – Justin_Finland Jan 23 '16 at 19:49
  • 1
    @Justin_Finland : You really have to post a separate question; you now have at least three questions here. Adding questions after the event renders all existing answers incomplete which is unfair on those who made the effort, Understanding how to use qsort is an entirely different question. The link I posted includes an example of sorting an int array - exactly what you need. For some reason you have created a `int*` array then tried to sort it. – Clifford Jan 23 '16 at 20:08
  • I understand. Thank you for all your help and patience. But, may have additional questions about your unique counting iteration after I discuss and figure out the quick sort though. Thank you, sir. Also, I did that because the first argument must be a pointer to the first object of the array to be sorted, converted to a void*, and that is what I thought I was doing. – Justin_Finland Jan 23 '16 at 20:11
  • I got it, sir. Thank you, again, for all of your help, time and patience. I appreciate it so much. – Justin_Finland Jan 24 '16 at 00:22