Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 6th, 2006, 6:56 PM   #11
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Nov 6th, 2006, 7:15 PM   #12
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 3 aznballerlee is on a distinguished road
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] = "";
         }
}
aznballerlee is offline   Reply With Quote
Old Nov 6th, 2006, 8:54 PM   #13
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
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>
Jimbo is offline   Reply With Quote
Old Nov 6th, 2006, 10:00 PM   #14
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
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] = "";
         }
}
See that last red one? At best it will be blanking out the data you want to keep, at worst it will crash your program.
The Dark is offline   Reply With Quote
Old Nov 6th, 2006, 11:20 PM   #15
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is offline   Reply With Quote
Old Nov 7th, 2006, 12:29 AM   #16
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Nov 7th, 2006, 12:46 AM   #17
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:42 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC