[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

help with C algorythm (find unique value in an array) could you please make changes



Good evening to all,...
i wrote a small algorythm (in C ) to find how many uniqe value are in an array ..
then change the size of the array to store all the unique values ..

code you please look at the code and make changes to it (to)
/*
This program Should find the true length of an array (the number of uniqe values)
then create a new array and copy all the unique values onto it.
It is relased with the GNU licence
Created by Jabka Atu (mashrom{dot}head{shift and 2 aka at}gmail.com)
*/

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

int truelength(int *arr,int *temp,int size){
int i=0,j,e=1,n=0;
//temp=(int *) malloc(sizeof(int)*size);
temp[0]=arr[0];
while(i<size){
   for(j=0;j<n;j++)
           if(arr[i]==temp[j])
           {
               e=0;
           }
if(e==1)
       {
       temp[n]=arr[i];
n++; }
   i++;
   e=1;

}
//This will shrink the corrct amount of mem ..
temp=realloc(temp,sizeof(int)*n);


return n;
}

int main(){
int i,e,n,j,size,*ptr,*temp,*newarry;
   printf("Please enter the size ");
   scanf("%d",&size);
   ptr=(int *) malloc(sizeof(int)*size);
   for(i=0;i<size;i++)
       scanf("%d",&ptr[i]);
   temp=(int *) malloc(sizeof(int)*size);
   n=truelength(ptr,temp,size);
   printf("The size of the array %d",n);

for(i=0;i<n;i++)
   printf("%d ",temp[i]);
free(temp);
free(ptr);
return 0;
   }





Reply to: