![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Well, it's silly to declare it then define it immediately, since a definition serves. It's when the compiler needs to know how it works BEFORE it's been defined that you use a declaration. You also can't declare one function inside another. move that last brace up.
Incidentally, the names must be in sort for my method to work. If they're not in sort the job is a lot longer. Why are you asking for comments on something you haven't compiled and tested for yourself?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#12 |
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 3
![]() |
Well, just looking for feedback and going into the right direction in coding. My project is due in a few hours, so I want to finish!
In this case .. I just wanted to make sure if this would work ..
int removeDups(string a[], int n);
int removeDups(string a[], int n)
{
for (i = 0; i < n; ++i)
{
while ((a[i] == a[i+1])
{
for (int i = index; i<n; ++i)
{
a[i] = a[i+1];
}
a[i-1] = "";
}
} |
|
|
|
|
|
#13 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
Why ask us if it works? You already hinted at having assert statements to test it, so use 'em. As to the code, I prefer the version with 2 functions (mainly for readability reasons)
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
#14 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 894
Rep Power: 4
![]() |
Depending on your compiler, the fact that you are using "i" everywhere is going to cause you problems.
On a modern compiler, your i's will match up like this: int removeDups(string a[], int n)
{
for (i = 0; i < n; ++i)
{
while ((a[i] == a[i+1])
{
for (int i = index; i<n; ++i)
{
a[i] = a[i+1];
}
a[i-1] = "";
}
} |
|
|
|
|
|
#15 |
|
Banned
![]() ![]() |
The most efficient way is to traverse the array once. Each time you find a duplicate you increment an "offset" variable. Then each iteration you're now shifting the next value by "offset" spaces to the left. That way you only need to traverse the array once, as opposed to yours and DaWei's method which involves traversing the array as many times as there are duplicates. For every duplicate it finds, it's going almost twice as slow as it needs to! Yikes! (DaWei's is not as bad, because it only shuffles as many spaces as it needs to).
Edit: Now that I think of it, this single traversal (although in this situation, nested), may only work on a one dimensional plane where you're checking one item against many. In this situation it is many versus many. I'll think about it a bit more thoroughly when I have the time. Maybe try to code up an example for what I mean. Last edited by Sane; Nov 6th, 2006 at 11:36 PM. |
|
|
|
|
|
#16 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I definitely want to see this one pass code....
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#17 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 894
Rep Power: 4
![]() |
Here you go:
int to = 0;
for (i = 1; i < arraySize; ++i)
{
if (theNames [i] != theNames [i-1])
to++;
if (to != i)
theNames[to] = theNames[i];
}
for (i = to + 1; i < arraySize; ++i)
theNames[i] = "";Edit: I guess its 1.n passes really, although you could put the clearing code in the original loop, at the expense of clarity. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Arrays or Vectors? | can342man | C++ | 2 | Apr 20th, 2006 4:57 PM |
| strings, pointers, arrays | bl00dninja | C++ | 21 | Mar 15th, 2006 9:02 AM |
| Arrays in PHP | MrMan9879 | PHP | 6 | Jan 12th, 2006 10:18 PM |
| Question about multidimensional arrays of strings | aznluvsmc | C | 8 | Oct 15th, 2005 11:20 PM |
| pointers strings, and strcat() | conbrio | C | 3 | Apr 16th, 2005 2:23 PM |