char array[] = "ABABCABC";and I need to print out only unique letters (in this case only ABC). I've been trying to write different codes but none worked at all so theres no point to show them here. Any help or hints would be greatly appreciated.
26 Replies - 855 Views - Last Post: 25 November 2012 - 01:25 PM
#1
Printing out unique members of a char array.
Posted 25 November 2012 - 03:30 AM
Replies To: Printing out unique members of a char array.
#2
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 03:51 AM
- Create a new array. Then, iterate through your original character and for every character not in the new array, add it to the new array. If it already exists in new array, ignore it. Then print the new array.
- Before printing any character, check if it has occurred in the array before. If it has, then skip over. If it has not, then print it. You can run a loop from array[0] to array[i-1] to check if array[i] has occurred before in the array or not.
#3
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 03:52 AM
#4
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 04:20 AM
AKMafia001, on 25 November 2012 - 03:52 AM, said:
As far as I understand these functions help me find the characters that I declare. I need to print out unique characters from a random array of characters. I kinda figured it out with the aditionnal array, but I want to do it as simply as possible, hopefully without using other arrays. Anyways. Thanks for the help. I'll keep trying to make it work.
#5
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 04:57 AM
Just note that the container must be sorted first.
This post has been edited by Anarion: 25 November 2012 - 04:57 AM
#6
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 06:32 AM
All you need to do is:
Char Array of original string
Char Array of string you want to search
Loop {
If OriginalArray[indexI] == SearchArray[IndexJ]
IndexFound = indexI;
}
If a first match is found, increment the indexes to check if the next element also matches... If it doesn't, reset the Index of the SearchArray and search further in the OriginalArray. Remember to reset the IndexFound too... Once a match is found and IndexJ is equal to the size of the search string, you can break out of the loop and have the exact FoundIndex...
If you are confused, you can ask...
Hope this Helps!
#7
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 08:22 AM
AKMafia001, on 25 November 2012 - 06:32 AM, said:
All you need to do is:
Char Array of original string
Char Array of string you want to search
Loop {
If OriginalArray[indexI] == SearchArray[IndexJ]
IndexFound = indexI;
}
If a first match is found, increment the indexes to check if the next element also matches... If it doesn't, reset the Index of the SearchArray and search further in the OriginalArray. Remember to reset the IndexFound too... Once a match is found and IndexJ is equal to the size of the search string, you can break out of the loop and have the exact FoundIndex...
If you are confused, you can ask...
Hope this Helps!
Well first of all thank you for replying, but
I'm sorry maybe I asked wrong. I think that I understand what you're telling me and its finding elements that you declare in the second array, right? What I need to do is to print out elements of an array only once. As I've said before if I have elements A B B B A C I need to print out A B C ignoring duplicated. My program isn't supposed to know what elements are repeating. It reads the data from a text file and then prints out all the capital letters but I need to print them out only once.
#8
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 10:25 AM
I think, you want to identify different elements. If something is repeating then it is ignored, and if something is new, insert it into the list...
Is that what you mean?
#9
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 10:33 AM
AKMafia001, on 25 November 2012 - 10:25 AM, said:
I think, you want to identify different elements. If something is repeating then it is ignored, and if something is new, insert it into the list...
Is that what you mean?
I want to print out individual elements only once even if there are many of them. My program should read capital letters from a text file and then print out the letters only once.
#10
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 10:48 AM
You haven't given any more information about the situation, but I'm assuming this is for an assignment and you may be forbidden from using STL algorithms? What about data structures? Can you use a Vector?
#11
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 10:54 AM
It would be easier if you maintain a std::vector and insert every new element into it... Whenever you read a new element, just search it in the std::vector if it already exists... If the vector contains less items, then you can use STL algorithms like find and find_if. But if you have many elements in your vector, then for better performance you can sort it and use a search algorithm like binary search or any other...
PS: Vectors can expand automatically, so you don't have to worry about the size...
Hope this Helps!
#12
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 11:01 AM
AKMafia001, on 25 November 2012 - 09:24 PM, said:
It would be easier if you maintain a std::vector and insert every new element into it... Whenever you read a new element, just search it in the std::vector if it already exists... If the vector contains less items, then you can use STL algorithms like find and find_if. But if you have many elements in your vector, then for better performance you can sort it and use a search algorithm like binary search or any other...
PS: Vectors can expand automatically, so you don't have to worry about the size...
Hope this Helps!
If any of the STL algorithms are to be used, then using unique (and ofcourse sort before it) seems better.
#13
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 11:10 AM
Anarion, on 25 November 2012 - 10:48 AM, said:
You haven't given any more information about the situation, but I'm assuming this is for an assignment and you may be forbidden from using STL algorithms? What about data structures? Can you use a Vector?
You are correct. This in university homework in a course called "Programming basics". We haven't learned vectors yet nor data structures. :\
#14
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 11:24 AM
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...
This post has been edited by AKMafia001: 25 November 2012 - 11:31 AM
#15
Re: Printing out unique members of a char array.
Posted 25 November 2012 - 11:29 AM
|
|

New Topic/Question
Reply




MultiQuote





|