Pointers and printing distinct elements in array

This is a discussion on Pointers and printing distinct elements in array within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> void main() { int *currentPoint, *pointerData, *newPoint; int i=0, n=0, j=0, freq=0; printf("Enter the number ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    Pointers and printing distinct elements in array

    Code:
     #include <stdio.h>
    #include <stdlib.h>
    void main() 
    { 
     int *currentPoint, *pointerData, *newPoint;
     int i=0, n=0, j=0, freq=0;
     
     printf("Enter the number of random integers to be generated: ");
     scanf("%d", &n);
     while (n<0)
     {
      printf("Enter the number of random integers to be generated: ");
      scanf("%d", &n);
     }
     //allocate unused space for the array
     pointerData = (int*) calloc (n, sizeof(int));
       
     if (pointerData==NULL)
      exit (1);
     
     //set current point to first value in array
     currentPoint=pointerData;
     for (i=0; i < n; i++)
      //create random array
      *(currentPoint+i) = rand() % 10 + 1;
    }
    Ive created an array using pointers that lets the user generate a random array of 'n' numbers.

    Now I need to output, for example:

    10 occurs 4 times
    6 occurs 7 times
    2 occurs 1 times


    Whats the best way to get an output like this because Im completely lost?

    I cannot use array indices and the final version should contain no ‘square’ brackets.

    Thanks
    Last edited by dave_the_bear10; 10-28-2011 at 09:42 AM. Reason: Forgot curly bracket

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Well sorting the array makes it easy to count how many of each value there is, since they'll all be adjacent in the array.

    > I cannot use array indices and the final version should contain no ‘square’ brackets.
    A pointless exercise IMO.
    You can just write arr[i] for now to make it all work, then go through at the end turning everything into *(arr+i)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    For your 'distinct' elements you need a int-Dictionary with key-type = int and value-type = int like C++/STL map<int,int>.

    A simple ANSI C implementation for this map eg:

    Code:
    typedef struct {
      int (*a)[2],i;
    } IntDict;
    
    int cmp(const void*a,const void *b) {
      int i=*(int*)a,j=*(int*)b;
      return (i>j)-(i<j);
      }
    
    void add(IntDict *a,int i) {
      int x[2]={i},*p=bsearch(x,a->a,a->i,sizeof*a->a,cmp);
      if( p )
        p[1]++;
      else
      {
        a->a=realloc(a->a,++a->i*sizeof*a->a);
        memcpy(&a->a[a->i-1],x,sizeof x);
        qsort(a->a,a->i,sizeof*a->a,cmp);
      }
    }
    
    int main()
    {
      IntDict id={0};
    
      /* insert all your values here, also duplicates... */
      add(&id,3);add(&id,3);add(&id,3);add(&id,3);
      add(&id,2);add(&id,2);add(&id,2);
      add(&id,1);add(&id,1);
      add(&id,0);
    
      /* print out the dictionary with keys and values */
      /* your distinct values are the dictionary-keys, here in [][0], values are occurrences in [][1] */
      while( id.i-- )
        printf("%5d %d\n",id.a[id.i][1]+1,id.a[id.i][0]),free(id.a[id.i]);
    
      free(id.a);
      return 0;
    }
    Last edited by BillyTKid; 10-29-2011 at 01:46 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-21-2010, 12:39 PM
  2. Printing out Array Elements
    By BB89 in forum C++ Programming
    Replies: 6
    Last Post: 03-24-2010, 02:00 PM
  3. printing elements of an array using pointers
    By zeebo17 in forum C Programming
    Replies: 3
    Last Post: 05-22-2009, 09:30 PM
  4. Small problem with printing elements of an array
    By DLR in forum C Programming
    Replies: 17
    Last Post: 03-09-2006, 05:57 PM
  5. printing array elements in ascending order
    By galmca in forum C Programming
    Replies: 29
    Last Post: 10-24-2004, 11:24 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21