Hi all;
I am so new in C.I have problem about array .I want to remove same element in array but i dont know how can i do. Can anyone help me
example : 1,2,3,5,67,31,13,5,2,1,1,4,
the program should produce -->>1,2,3,5,67,13,31
Hi all;
I am so new in C.I have problem about array .I want to remove same element in array but i dont know how can i do. Can anyone help me
example : 1,2,3,5,67,31,13,5,2,1,1,4,
the program should produce -->>1,2,3,5,67,13,31
You can't "remove" elements from an array in C. There are a few ways to go about doing what you want, though.
You can use memmove() to shuffle around the elements of the array. This is annoying but will be close to the results you want (but you have to keep track of how many elements you "remove", because C's arrays cannot change size). If it's actually a pointer to some space you allocated, then you could use realloc() after memmove() and resize it; but you'd still have to keep track of the size.
You can mark deleted elements with an "invalid" value, such as -1; that is, assuming there is a value that you can consider invalid.
You can copy the elements you want to keep into a new array, and use that instead.
The best solution depends on your situation, of course. But removing array elements in C isn't as simple as it is in, say, Ruby, with something like Array#delete.
Right, so the "problem" has several aspects to it. It is correct that we can't delete individual elements in an array. But it is fairly easy to either create second array that contains only the unique elements of the first array, or to remove duplicate elements from an array itself - as suggested, memmove does that sort of thing, but it can be done with a loop as well.
For any work with arrays, we do need to know how many elements there are in the array (this is not, in C at least, the actual size of the array, as the size may be a "large fixed size" in many cases).
Once we know that we know the number of elements, we simply write code to work our way through the array (requires two loops, one for "which element we're at" and one for "where we're looking", that goes from the current element to the end of the current number of elements) and eliminate those that are duplicates, shuffling the rest of the array up when there is a duplicate. When the end is reached, all elements should be unique.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
hi;
thanks for all answers; actually my problem is little bit different ,sorry for the wrong posting.My real problem is ;
I have txt file which include :
orange
green
white
orange
white
black
blue
red
blue
white
green
green
orange
red
white
green
red
black
red
something like that. Now i want to read that txt file then write the other txt file. how can do that ? any idea?
The exact same way, except you work with strings instead of numbers.
Break the data up into an array of strings, step through the array, determining each time if the string has already been entered into your new array. If it hasn't, add it to the array. It it has, skip it.
Sort the array. The duplicates will be right next to each other. Then loop through the array and put every unique value into a second array of the same size and type.
I use one loop for this. I have *no* idea what looping through an array "for 'where we're looking' ", means.
I have tried your solution but I can't solve it .(because i have no much more experience in C ). Please Can anyone write code?.. Thanks
Why don't you post what you have so far - we do not "do" your homework [and whilst this may possibly not be homework, it certainly smells like it - either way, you will learn a whole lot more by doing it yourself].
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I have started the write code but i doest works properly When i compile the code it can only make unique first element of array. .can anyone say where is the wrong part of my code??
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> int strcmp1 (const char * s1, const char * s2) { for(; *s1 == *s2; ++s1, ++s2) if(*s1 == 0) return 0; return *(unsigned char *)s1 < *(unsigned char *)s2 ? -1 : 1; } int main () { char *arr[] = {"string1", "string2","string3","string4","string2","string1","string3","string5","string6"}; char *tmp[sizeof(arr) / sizeof(int)]; int i,v,j=0; char *n=NULL; for (i=0;i<sizeof(arr) / sizeof(int);i++){ for (j=i+j;j<sizeof(arr) / sizeof(int);j++){ v=strcmp1(arr[i],arr[j]); if(v != 0){ tmp[j]=arr[j]; } else{ tmp[j]=" "; } } } for(i=0;i<sizeof(arr) / sizeof(int);i++){ printf("%s\n",tmp[i]); } }
Your code is setting tmp[j] = arr[j] for every element that isn't matching arr[i].
Also, j = i + j, when you get round to the second iteration of i will be outside the valid size of arr, so the loop will terminate immediately - so once you have gone past i = 0, it will not work. You should use j=i+1 or possibly j = i;Code:for (j=i+j;j<sizeof(arr) / sizeof(int);j++){
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Code:#include <stdio.h> #include <string.h> #define length(a) ( sizeof (a) / sizeof (char *) ) uni ( char **src ) { int unique = 0; int n=length(src); char *dst[length(src)]; int i; dst[unique++] = src[0]; for ( i = unique; i < n; i++ ) { int has_dup = 0; int j; for ( j = 0; j < unique; j++ ) { if ( src[i] == dst[j] ) has_dup = 1; } if ( has_dup == 0 ) dst[unique++] = src[i]; } for ( i = 0; i < unique; i++ ) printf ( "%s ", dst[i] ); printf ( " \n " ); } int main( ){ char *src[]={ "string1", "string2", "string3", "string4", "string2", "string1", "string3", "string5", "string6"}; uni(src); return 0; }
this is the last version of my code.but it needs one more modification.When i compile the this code it produce only first string. I have tried the solve this problem then i find the it. the problem is array length. if give the array length manually it works properly. on the other hand when i give the array length using length global it doest work...any idea?
You cannot get the size of a passed array using sizeof. It will give you the size of the pointer, not the block of data. Why don't you pass the size as an argument?
Quzah.
Hope is the first step on the road to disappointment.