Printing out unique members of a char array.

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 2969 Views - Last Post: 25 November 2012 - 01:25 PM Rate Topic: -----

#16 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 11:38 AM

Yes! @aresh you are correct. I wrote it in a hurry, as I'm leaving now...

@OP, I have changed the snippet. Please review it and make the changes as said by aresh...
Was This Post Helpful? 1
  • +
  • -

#17 GloomyDawn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-November 12

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 12:09 PM

View PostAKMafia001, on 25 November 2012 - 11:24 AM, said:

I assume you have some knowledge of fixed size arrays...

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.
Was This Post Helpful? 0
  • +
  • -

#18 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,176
  • Joined: 08-January 12

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 12:13 PM

We can easily write the code if we want. But will it really help you learn?

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

Was This Post Helpful? 2
  • +
  • -

#19 GloomyDawn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-November 12

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 12:18 PM

View Postaresh, on 25 November 2012 - 12:13 PM, said:

We can easily write the code if we want. But will it really help you learn?

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.
Was This Post Helpful? 0
  • +
  • -

#20 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,176
  • Joined: 08-January 12

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 12:22 PM

Well, I thought that you might run into a problem there. Here is a handy little way to check it has appeared before
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.
}

Was This Post Helpful? 1
  • +
  • -

#21 GloomyDawn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-November 12

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. :\
Was This Post Helpful? 0
  • +
  • -

#22 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 12:43 PM

The simplest solution I can think of would be to create an array the size of the max ascii code. Iterate through your string and increment the array element at the corresponding ascii character. Once you have your counter array, iterate through that and print all non-zero indices.

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;
}


Was This Post Helpful? 1
  • +
  • -

#23 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,176
  • Joined: 08-January 12

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 12:55 PM

No, you are doing it wrong. Think about it. Look at line 9. Is that how you compare two variables? Also, if they are equal, you just have to change the value of flag to something other than 0. And finally, the condition of flag == 0 should be outside the inner for loop.
Was This Post Helpful? 1
  • +
  • -

#24 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 01:02 PM

View PostAnarion, on 25 November 2012 - 11:57 AM, said:

The C++ Standard Library way of doing this task is to use the methods available in <algorithm> header like unique or unique_copy.

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 :P/>/>

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

Was This Post Helpful? 1
  • +
  • -

#25 GloomyDawn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-November 12

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 01:09 PM

View Postaresh, on 25 November 2012 - 12:55 PM, said:

No, you are doing it wrong. Think about it. Look at line 9. Is that how you compare two variables? Also, if they are equal, you just have to change the value of flag to something other than 0. And finally, the condition of flag == 0 should be outside the inner for loop.


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...


View Postishkabible, on 25 November 2012 - 01:02 PM, said:

View PostAnarion, on 25 November 2012 - 11:57 AM, said:

The C++ Standard Library way of doing this task is to use the methods available in <algorithm> header like unique or unique_copy.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.edit:wait, jjl's is simpler :P/>


That stuff is too advanced for my beginner uni course homeworks :\
Was This Post Helpful? 0
  • +
  • -

#26 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,176
  • Joined: 08-January 12

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 01:17 PM

Two problems still persist.

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.
Was This Post Helpful? 2
  • +
  • -

#27 GloomyDawn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-November 12

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 01:25 PM

View Postaresh, on 25 November 2012 - 01:17 PM, said:

Two problems still persist.

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.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2