-4

I have to Write a program that will read in a list of numbers and will then print out the same list except numbers that have already been printed will be printed as a zero instead.

  #include<stdio.h>
int main()
{
  int n, list[100],i,j,k;
  scanf("%d", &n);
  for(i=0; i<n; i++)
    scanf("%d", &list[i]);
    for(i=0; i<n; i++)
    {
      for(j=i+1; j<n; )
      {
        if(list[j]==list[i])
        {
        for(k=j; k<n; k++)
     list[k]=list[k+1];
      // list[j]=0;
          n--;
        }
         else
         {
           j++;
         }
      }
    }
for(i=0; i<n; i++)
  printf("%d\n", list[i]);
 return 0;
}

Test cases Sample Input :

(Length:) 5 (Values:) 2 3 4 3 6

Sample Output:

2  3  4  0  6
0xdeadbeef
  • 86
  • 7
DX Sparta
  • 1
  • 1
  • 4
  • @0xdeadbeef thanks for the edit. Can you tell me the solution and where I have to change the code or add new code – DX Sparta May 12 '15 at 18:36

3 Answers3

1

You could make another list of the same length. While printing the numbers you would check for every number whether it's in the list and if not add it to the list and print it. So after filling the list with input:

int checklist[100];
int count = 0;
for(int i = 0; i<100; i++){
   int valid = 1;
   for(int j = 0; j < count; j++){
      if(list[i]==checklist[j]){
          valid = 0;
      }    
   }
   if(valid == 1){
       printf("%d ", list[i])
       checklist[count]=list[i];
       count++;
   }
   else{printf("0 ");
}
scaletos
  • 113
  • 7
  • can you produce that code please I tried that way too. but it didn't work for me. – DX Sparta May 12 '15 at 18:33
  • please check the code below and suggest some changes because this is not working. I have written only function here. int list[100], a[100]={}; for(i=0; i – DX Sparta May 12 '15 at 19:18
  • I am doing following thing. 1) create a secondary array, same size as source array, initialized to 0. 2) In outer loop, index through each element of source array. 3) For each element in source, use inner loop to read each element of secondary array, to see if element already exists. 4) If not, copy value in source to secondary and continue. If exists, change element in source to 0, and continue. When search complete, contents of source array will contain zeros where duplicates existed. – DX Sparta May 12 '15 at 19:23
0

Only one way in my mind already, use an int x; variable to count your scanned numbers, before your printer loop you should copy to another array[x] and before you print the values, you should check back this array(you can use a temporary int, its always 0 when loop again), and if u find a value like your actual value in the loop, then print 0.

vektor112
  • 1
  • 3
0

If all your elements are positive integers, one simple (if inefficient) way is to find the maximum element, make an array of that size of all zeros using calloc() or similar, and use that array as a perfect hash table:

int 
find_max(int* arr, size_t len) 
{
    int max = INT_MIN;
    for (size_t idx = 0; idx < len; idx++)
        max = (arr[idx] > max) ? arr[idx] : max;
    return max;
}

int*
make_hash_table(size_t len)
{
    return calloc(len, sizeof(int));
}

void
free_hash_table(int** ht)
{
    free(*ht), *ht = NULL;
}

int*
make_filtered_array(int* arr, size_t len, int* ht) 
{ 
    int* new_arr = malloc(len * sizeof(int));
    for (size_t idx = 0; idx < len; idx++) {
        new_arr[idx] = (ht[arr[idx]] == 0) ? arr[idx] : 0;
        ht[arr[idx]] = 1;
    }
    return new_arr;
}

In your main function, you might then do something like:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int
main()
{
    int foo[] = {5, 2, 3, 4, 3, 6};

    /* calculate length of static foo -- does not work for dynamic arrays */
    size_t foo_length = sizeof(foo)/sizeof(*foo);

    /* find maximum element in foo */
    int foo_max = find_max(foo, foo_length);

    /* make an int array of size foo_max that contains all zeroes, to use as a hash table */
    int* hash_table = make_hash_table(foo_max);

    /* make an int array containing filtered values */
    int* filtered_array = make_filtered_array(foo, foo_length, hash_table);

    /* print the filtered array */
    for (size_t idx = 0; idx < foo_length; idx++)
        fprintf(stdout, "%d ", filtered_array[idx]);
    fprintf(stdout, "\n");

    /* clean up dynamically-allocated memory */
    free_hash_table(&hash_table);
    free(filtered_array);

    return EXIT_SUCCESS;
}

Given input of {5, 2, 3, 4, 3, 6}, the printed output is:

5 2 3 4 0 6

If there are negative integers, you can still use this approach with some tweaks to the lookup table. I'll let you think about how to do that.

This should run in time linearly proportional to the size of the input array, because finding the maximum element in your input array is linear (you walk through the whole array once) and lookups to the hash table are in constant time.

On a first pass, this is not memory efficient. For an array of size n, you need two more arrays of size n. If you can replace the static array foo[] with a dynamic array, however, you can replace elements in that input array in-place, without creating a duplicate array. I'll let you think about how to do that.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
  • I am getting error while trying this code as I am newbie to hash tables I dont know much into that. I am getting error of size_t and many more. So can you please tell me how to write code without using hash tables. – DX Sparta May 12 '15 at 19:31
  • I don't know what error you are getting, so I'm not sure what help I can provide. Try reading Wikipedia on hash tables: http://en.wikipedia.org/wiki/Hash_table – Alex Reynolds May 12 '15 at 19:33