Function to make unique string array

Hi all,

I am working in Visual Studio 2010 (on Windows) and am near the end of my first C++ class. I am having difficulties writing one of the functions for my project.

This function needs to take an array of strings (arr[]), and store each word of that array in a new array (uneek[]), storing each word only once. Basically I need to fill an array with the unique words from the input array. The parameter reference int counter is to keep track of the number of unique array elements.

The problem is that when I run this program (specifically this function) Windows has a problem with it and has to close the program. This is my function so far:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 void unique140(string arr[], int size, string uneek[], int& counter)
{
	int i=0;
	int k=0;
	while(i<size)
	{
		int j=0;
		while (j<i)
		{
			if(arr[i] != uneek[j])
			{
				uneek[k]= arr[i];
				counter++;
				k++;
			}
			j++;
		}
		i++;
	}
	return;
}


I know it may seem a little complicated (with i, j, and k counters) but I couldn't think of another way to go through each element of the input array (arr[]) and check it with all of the elements of the storage array (uneek[]) while at the same time keeping track of which elements of the storage array are filled!

Any input about what the issue is with this function would be great. Tips on efficiency would be equally valued. Thanks for reading.
Because these are determinate loops, I would suggest using for loops instead of while. Also, the condition for your selection control statement if(arr[i] != uneek[j]) is not the proper condition to test for.Try:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 void unique140(string arr[], int size, string uneek[], int& counter)
{
	int k = 0;
	for(int i = 0; i < size; i++)
		for(int j = 0; j < size; j++)
			if((j != i) && (arr[j] != arr[i]))
			{
				uneek[k] = arr[i];
				counter++;
				k++;
			}
			else
				;
	return;
}


And then use the counter to determine how many elements of uneek[] need to be displayed.
Last edited on
Thanks for the suggestion dacheeba. I finally worked this out on paper this morning and wrote up almost the same code as you suggested with a small change of (j<i) for the innermost loop. I feel I am closer to the correct function but am still getting the same error message. What I have now is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void unique141(string arr[], int size, string uarr[])
{
	int k=0;

	for(int i=0; i<size, i++;)
	{
		for(int j=0; j<i; j++)
		{
			if(arr[i] != arr[j])
			{
				uarr[k]= arr[i];
				k++;
			}
		}
	}
	return;
}


I have left out "counter" for now. I think the error lies somewhere in my loops but do not know for sure. Thanks again for any input.
What is the error message?
It is a pop-up window that says:

unique_array.exe has stopped working.

Windows is checking for a solution to the problem. (green bar)

(Then): A problem has caused the program to stop working correctly. Windows will close the program and notify you if a solution is available.

By setting the first element of my unique array equal to the first element of the input array before calling my unique140() function, I was able to get rid of the error message listed above. However now the unique array (when displayed later) only stores the first element of the input array and the rest of its elements are blank.
Break it up into two smaller functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>

// return true if str is present in array a, false otherwise
bool is_present( const std::string a[], int size, const std::string& str )
{
     for( int i = 0 ; i < size ; ++i ) if( a[i] == str ) return true ;
     return false ;
}

// const added
void unique140( const std::string arr[], int size, std::string uneek[], int& counter)
{
    counter = 0 ; // uneek is initially empty

    for( int i = 0 ; i < size ; ++i ) // for each string in arr
        if( !is_present( uneek, counter, arr[i] ) ) // if it is not present in uneek
            uneek[counter++] = arr[i] ; // copy to uneek, increment counter
}
This idea works very nicely! Thanks for the tips.
Topic archived. No new replies allowed.