26 Replies - 893 Views - Last Post: 25 November 2012 - 01:25 PM
#16
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 11:38 AM
#17
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 12:09 PM
AKMafia001, on 25 November 2012 - 11:24 AM, said:
You can create a char array of large size, maybe char chArray[256];
If a new item is found, insert it to the array and keep track of the current index being used...
Read new item, now you have to search, the simplest way is:
for( i = 0; i < currentIndex; i++) {
if(chArray[i] == item)
return false;
}
chArray[currentIndex+1] = item;
currentIndex++;
// or simply, chArray[currentIndex++] = item;
return true;
I think that's all, in the end chArray is filled up with what you need...
Hope this Helps!
Edit: I have modified the snippet, and now is considered to be a part of function which is supposed to return bool, if item already exists it returns false, else true... You can change it as you want...
I'm sorry. I still don't get it. It doesn't work for me. SIGH. Could you please write the full code that would do the procedure because I can't figure it out myself.
Thank you for your answers though.
#18
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 12:13 PM
Let me try once again.
You must iterate through your entire array. For every element, check if it has appeared in the array before. If it has not, print it. Otherwise, leave it. Here is the pseudo-code
use a loop to go through the entire string
{
if(current element has not appeared before)
print it
}
Did you get this part?
This post has been edited by aresh: 25 November 2012 - 12:14 PM
#19
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 12:18 PM
aresh, on 25 November 2012 - 12:13 PM, said:
Let me try once again.
You must iterate through your entire array. For every element, check if it has appeared in the array before. If it has not, print it. Otherwise, leave it. Here is the pseudo-code
use a loop to go through the entire string
{
if(current element has not appeared before)
print it
}
Did you get this part?
How do you check if it has not appeared before?
char chArray[]="ABCABCABC";
for(i=0;i<strlen(chArray);i++){
if(chArray[i] )
I don't understand what to compare it to.
#20
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 12:22 PM
for(i = 0; i < strlen(charArray); i++)
{
flag = 0; //This is just a variable to monitor whether the character is repeated or not.
//If flag == 0, then it is not repeated. Otherwise, it is repeated.
for every element in charArray from charArray[0] to charArray[i-1], check if it is equal to charArray[i]. If it then, then the character is repeated and we can change the value of flag accordingly.
if (flag == 0)
print it.
}
#21
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 12:37 PM
int main() {
char chArray[]="ABCABCABC";
int flag;
for(int i=0;i<strlen(chArray);i++)
{
flag = 0;
for(int j;j<strlen(chArray);j++)
{
if(chArray[i] = chArray[j])
{
if(flag == 0)
{
cout << chArray[i];
}
}
}
}
system("pause");
return 0;
}
This is where I am following your help. Needless to say it doesn't do what it should. :\
#22
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 12:43 PM
i.e.
#include <stdio.h>
#define MAX_ASCII 127
int main() {
int count[MAX_ASCII] = {0};
char *str = "AABBCCABC", *it;
int ndx;
for(it = str; *it; it++)
count[*it]++;
for(ndx = 0; ndx < MAX_ASCII; ndx++)
if(count[ndx])
printf("%c", (char)ndx);
return 0;
}
#23
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 12:55 PM
#24
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 01:02 PM
Anarion, on 25 November 2012 - 11:57 AM, said:
Just note that the container must be sorted first.
that's a nice solution; sort the array(which can be done in linear time with a counting sort) and use std::unique then iterate from the first element to the one returned. linar time, doesn't require extra space(unless a copy is needed). I don't think it gets much better than this. even without the counting sort it's still pretty good.
std::sort(str, str+strlen(str)); auto e = std::unique(str, str + strlen(str)); while(str != e) std::cout<<*str++;
edit: wait, jjl's is simpler and even more efficient
something like this, this is untested however.
int count[128] = {0};
char* i = str;
while(*i) ++count[*i++];
i = str;
for(int j=0;j<128;++j)
while(count[j]) *i++ = j;
i = std::unique(str, i);
while(str != i) std::cout<<*str++;
This post has been edited by ishkabible: 25 November 2012 - 01:27 PM
#25
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 01:09 PM
aresh, on 25 November 2012 - 12:55 PM, said:
int main() {
char chArray[]="ABCABCABC";
int flag;
for(int i=0;i<strlen(chArray);i++)
{
for(int j;j<strlen(chArray);j++)
{
if(chArray[i] == chArray[j])
{
flag = 1;
}
else
{
flag = 0;
}
}
if(flag == 0)
{
cout << chArray[i];
}
}
system("pause");
return 0;
}
ok made some adjustments according to what you said and it still doesn't work...
ishkabible, on 25 November 2012 - 01:02 PM, said:
Anarion, on 25 November 2012 - 11:57 AM, said:
That stuff is too advanced for my beginner uni course homeworks :\
#26
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 01:17 PM
Let us suppose your array is ABA, and you have printed AB successfully. When it comes to A, it will check if A==A, and set flag = 1. However, it will continue and check if B==A, and hence set flag = 0. So, remove the else flag = 0 part.
Now, let us take a simple case - only 1 element exists, A. So, your array is "A". So, it will go to the outer loop. Then, it will run for j. Now, irrespective of value of i, which can be from 0 to strlen-1, j can also be 0 to strlen-1. So, at one point, i will be equal to j. However, you don't want to do that. You want to run j from 0 to i-1. So, fix that also.
#27
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 01:25 PM
aresh, on 25 November 2012 - 01:17 PM, said:
Let us suppose your array is ABA, and you have printed AB successfully. When it comes to A, it will check if A==A, and set flag = 1. However, it will continue and check if B==A, and hence set flag = 0. So, remove the else flag = 0 part.
Now, let us take a simple case - only 1 element exists, A. So, your array is "A". So, it will go to the outer loop. Then, it will run for j. Now, irrespective of value of i, which can be from 0 to strlen-1, j can also be 0 to strlen-1. So, at one point, i will be equal to j. However, you don't want to do that. You want to run j from 0 to i-1. So, fix that also.
Holy %#$@. It works. I love you. Thanks so much.
|
|

New Topic/Question
Reply




MultiQuote






|