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
The simplest solution is to store the unique values in a second array. For each value in the original array, if the value doesn't exist in the second array, store it. When you've exhausted all of the values in the original array, only the unique values will be stored in the second array.
Give that a shot and feel free to ask for more help if you have any problems.
The simplest solution is to store the unique values in a second array. For each value in the original array, if the value doesn't exist in the second array, store it. When you've exhausted all of the values in the original array, only the unique values will be stored in the second array.
Give that a shot and feel free to ask for more help if you have any problems.
actually my problem is little different . my real proplem is = i have txt file which include :
orange
green
white
orange
white
black
blue
red
blue
white
green
and so on.. Now ı want to write different colour another txt file ..how can i do that . thanks for your answers
>actually my problem is little different .
Then why did you ask about something different from your actual problem? Do you go to the auto mechanic and say that your radio isn't working when you really want the air conditioning fixed?
>Now ı want to write different colour another txt file ..how can i do that
It just so happens that the answer is the same. Cache the unique strings in an array and then write them to the second file. You can also do the original solution using file I/O, but it's tedious and inefficient.
actually my problem is little different . my real proplem is = i have txt file which include :
orange
green
white
orange
white
black
blue
red
blue
white
greenand so on.. Now ı want to write different colour another txt file ..how can i do that . thanks for your answers
You could loop through the array using strcmp().
>You could loop through the array using strcmp().
Sure, a loop is pretty much required, and strcmp is the classic way of comparing strings, so no matter how this problem is solved, both of those will be used. Can you be any more vague about what you had in mind or were you just trying to sound like you know what you're talking about?
>You could loop through the array using strcmp().
Sure, a loop is pretty much required, and strcmp is the classic way of comparing strings, so no matter how this problem is solved, both of those will be used. Can you be any more vague about what you had in mind or were you just trying to sound like you know what you're talking about?
No I wasn't just trying to sound like I know what I am talking about as you put it.
If there was a little bit more information to the problem he was having or what he needed help trying to solve than maybe it might have called for a bit more information and I might have added more.
>If there was a little bit more information to the problem he was
>having or what he needed help trying to solve than maybe it might
>have called for a bit more information and I might have added more.
I don't see any lack of information (after he described his real situation at least), but whatever you say...
>I don't see any lack of information (after he described his real situation at least), but whatever you say...
Which situation are you referring to? This one?
actually my problem is little different . my real proplem is = i have txt file which include :
orange
green
white
orange
white
black
blue
red
blue
white
green
and so on.. Now ı want to write different colour another txt file ..how can i do that . thanks for your answers
Now am I to guess what he wants to do with the colors...does he want to copy them to another file as they are, does he want to reverse the order they are in, does he want to take every instance of green and change it to blue, or how about every instance of red and replace it with white, or why not figure out a way to sort the colors alphabetically.
We could go on all night with all different variations of what he wants to do.
But I don't profess to know everything or know how to read minds, so I just left a general suggestion.
shortly,
input.txt include
orange
green
white
orange
white
black
blue
red
blue
white
green
and output.txt must be include:
orange
green
white
black
blue
red
narue has already given you the answer.
i'm not telling you anything she hasn't already. but maybe i can break it down for you into easy-to-chew bites.
1 -- create a new array.
2 -- start indexing existing array at the first element
3 -- if the existing element does not exist within the new array,
-- 3a -- then copy the existing element to the new array
4 -- move to the next existing array element and repeat at (3)
there are other ways to do it, but it just does not get any more simple than this.
.
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
>Please Can anyone write code?
No. If you ever get a job as a programmer, you can't say "I can't do it, you do it for me" to one of your peers. Likewise, we're here to help you become a better programmer through answers to your questions. Daniweb is not a one stop solution factory where you can get volunteer code monkeys to do your work for you.
In fact, our rules clearly state that we expect you to provide proof of effort if you want help. Please do so now or I'll close your thread on the grounds that you're in violation of Daniweb policies.
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
we'll help you but you've got to show some effort. you cant just come here asking someone to do all your work for you.
surely you've written some code already? post it here, then we'll help.
if you havent written anything yet, you need to get busy. I'll give you a place start:
#include <stdio.h>
#include <string.h>
int main ()
{
//declare your variables first
//then write the body of your code
return 0;
}
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??
#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]);
}
}
I think I see where you're going with this, but it's overly complicated. A better solution in my estimation would be to only store the unique strings in the temporary array, with no holes between them. What this means is that the size of the temporary array would be independent of the size of the original array, up to the size of the original array (if there are no duplicates)
This way you can treat the temporary as a conventional array and you don't have to play games like "this element is a string with one space if a duplicate occupies that index in another array":
#include <stdio.h>
#include <string.h>
#define length(a) ( sizeof (a) / sizeof *(a) )
void display_list ( char *a[], size_t n )
{
/* You can write this */
}
int exists ( char *a[], size_t n, char *value )
{
/* You can write this */
}
int main ( void )
{
char *a[] = {
"string1", "string2", "string3",
"string4", "string2", "string1",
"string3", "string5", "string6"
};
char *unique[length ( a )];
size_t k = 0;
size_t i;
display_list ( a, length ( a ) );
for ( i = 0; i < length ( a ); i++ ) {
if ( !exists ( unique, k, a[i] ) )
unique[k++] = a[i];
}
display_list ( unique, k );
return 0;
}
>sizeof(arr) / sizeof(int)
For the record, int and char* are independent of each other. sizeof(int)
is not necessarily equal to sizeof(char*)
. Your code is both confusing and non-portable.
Also, main must return a value in C89, and it's a good practice to return a value explicitly in C99. If you aren't sure that you're using C99, your code is wrong.
Narue thank you for everything ,your advises and code were very helpful for me..
#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(void ){
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...how can i solve this problem??
because you cant use variables to declare sizes of arrays. why do you feel that you need to dynamically size your arrays? what's wrong with hard coding them?
if you insist on dynamic memory allocation, then you need to use "malloc" . but i really suggest that you don't go there.
[#include<iostream.h>
#include<conio.h>
void main(void){
clrscr();
int i,j,k,count=0,flag;
char a[10],b[10],c[20],t,p[20];
cout<<"Enter First Array";
for(i=0;i<=9;i++) {
a[i]=getche();cout<<" ";
}
cout<<endl<<"Enter Second Array";
for(i=0;i<=9;i++){
b[i]=getche(); cout<<" ";}
cout<<endl<<"First Array is:";
for(i=0;i<=9;i++)
cout<<a[i]<<" ";
cout<<endl<<"Second Array:";
for(i=0;i<=9;i++)
cout<<b[i]<<" ";
for(i=0;i<=9;i++){
c[i]=a[i];
c[i+10]=b[i];
}
p[0]=c[0];
for(i=1;i<20;i++)
{
flag=0;
for(j=0;j<=count;j++)
{
if(c[i]==p[j])
flag=1;
}
if(flag==0)
{
p[count]=c[i];
count++;
}
}
cout<<endl<<"merged arrray is";
for(i=0;i<count;i++)
{
cout<<p[i]<<"\t";
}
for(i=0;i<=count-1;i++){
for(j=i+1;j<=count;j++){
if(p[i]>p[j]){
t=p[j];
p[j]=p[i];
p[i]=t;
}
}
}
cout<<endl<<"Soorted Array is:";
for(i=0;i<=count;i++)
cout<<p[i]<<" ";
getch();
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.