GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #11  
Old 06-May-2004, 10:48
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by the_crazyman
hi,
hi.

Quote:
Originally Posted by the_crazyman
I'm a bit confused with regards Boolean statements as I've never really used them before.
is there any other way of doing this without using the bool method,
Sure. change bool to int. Boolean is just false/true, or 0/nonzero, integers.

Quote:
Originally Posted by the_crazyman
ie. writing out to a temporary array and then sending the temp array contents back into getTwentyLetters.
No wonder they call you the_CrazyMan. That's like saying "I want to leave the room and have never used a doorknob. Can I somehow remove the window and climb out?" ;-) Learn boolean, it's easy.

Quote:
Originally Posted by the_crazyman
I was thinking that if I ran the loop again that runs around the array again and if it detects that the array still contains duplicates that it could call itself again.. Else, it just falls out of the function and continues to the next call. does this make sense ?? Or, alternatively am I talking pap? ??:
i.e. original code.

CPP / C++ / C Code:
void deleteDuplicates()
{
  int i,j;
  
for(i=firstElement; i<=lastElement; i++) 
  {    
    //At each location, the two adjacent elements are compared
    if(GetTwentyLetters[i] == GetTwentyLetters[i+1])
    {
      // if elements are the same, then elements are moved down one position
      for(j=i;GetTwentyLetters[j];j++)
      {
        GetTwentyLetters[j] = GetTwentyLetters[j+1];
      }
    }
  }
   for(i=firstElement; i<=lastElement; i++)
     { 
       if(GetTwentyLetters[i] == GetTwentyLetters[i+1])
      {  
         deleteDuplicates();
      }
    
}
Whoa! Recursive! And you don't like boolean? You just tried climbing out the chimney here! Mucho overkillo.

CPP / C++ / C Code:
void deleteDuplicates()
{
    int k,j;
  
    for(k=firstElement; k<=lastElement; k++) 
    {    
        //At each location, the two adjacent elements are compared
        if(GetTwentyLetters[k] == GetTwentyLetters[k+1])
        {
            // if elements are the same, then elements are moved down one position
            for(j=k;GetTwentyLetters[j];j++)
            {
                GetTwentyLetters[j] = GetTwentyLetters[j+1];
            }
        }
    }
}
Getting rid of the recursion is almost all you need. The above will remove all doubles. It will leave triplicates as doubles. This routine should get all dups:
CPP / C++ / C Code:
void deleteDuplicates()
{
    int k,j;
  
    for(k=firstElement; k<=lastElement; k++) 
    {    
        //At each location, the two adjacent elements are compared
        while (GetTwentyLetters[k] == GetTwentyLetters[k+1] 
            && GetTwentyLetters[k])
        {
            // if elements are the same, then elements are moved down one position
            for(j=k;GetTwentyLetters[j];j++)
            {
                GetTwentyLetters[j] = GetTwentyLetters[j+1];
            }
        }
    }
}


One problem I see is if the last element in GetTwentyLetters is not 0, you will walk right off the end of your array. Change the 2nd loop to:
CPP / C++ / C Code:
for(j=k; j < lastElement; j++)
By the way,
CPP / C++ / C Code:
for(j=i; GetTwentyLetters[j]; j++)
is using boolean. You comparison says "GetTwentyLetters[j] is true (not zero) run the loop again."

Also, I hope GetTwentyLetters is lastElement+1 in length. If not, your first loop runs off the end too. And you comparison is comparing with the lastElement+1 also.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #12  
Old 08-May-2004, 10:10
the_crazyman the_crazyman is offline
Awaiting Email Confirmation
 
Join Date: Apr 2004
Posts: 11
the_crazyman is on a distinguished road
Walt,

thanks a lot for this.. also big thanks to Max Payne for the help.

Walt, last question.

the part of the code,
while (GetTwentyLetters[k] == GetTwentyLetters[k+1]
&& GetTwentyLetters[k])

how's this helping to remove the triplicates. The original code I had would as you rightly pointed out, remove doubles but not triplicates.

using the code you've submitted certainly helps with that, but not sure what's happening with this while loop. it's replace the 'if' statment I had but i can't work out why that's trapping all triplicates.

can you give me a quick explanation or anything that would help. it looks like a pretty useful thing to use in furture.

cheers

the_crazyman,.



i wo
  #13  
Old 08-May-2004, 10:57
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
the if statement is only run once with each pass in the outer for loop. the while statement on the other hand will not rest till all the identical characters are overwritten, since just inside the while loop, theres the for loop which will move up each element in case it finds a duplicate. now in case, theres a triplicate, it won't do anything, if it's inside the if statement as if statement only executes once per pass, while on the other hand will be executed as many times as it will find duplicates in the next element.

for example lets say your array's first 3 elements are the character 'A'. after you enter the for loop, it will first compare array[0] to array[1] for duplicates inside the if statement. if they are same, it will enter another for loop, which copies array[1] to array[0], array[2] to array[1] and so on until the array ends.so now your array[0] is 'A', array[1]='A' and array[2] is something else. After which it will leave the if statement, and your outer for loop will make another pass. this time comparing array[1] to array[2] which are not equal. and so both array[0] and array[1] still have duplicates.

While on the other hand, after replacing array[0] with array[1] and array[1] with array[2] and so on, will again check array[0] with array[1] and if it finds duplicates again, will repeat the process and so on.
  #14  
Old 08-May-2004, 11:18
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by the_crazyman
Walt,

thanks a lot for this.. also big thanks to Max Payne for the help.

Walt, last question.

the part of the code,
CPP / C++ / C Code:
while (GetTwentyLetters[k] == GetTwentyLetters[k+1] 
            && GetTwentyLetters[k])

how's this helping to remove the triplicates. The original code I had would as you rightly pointed out, remove doubles but not triplicates.
This loops until either
  • two adjacent letters are not equal: GetTwentyLetters[k] == GetTwentyLetters[k+1]
  • end of the list is reached GetTwentyLetters[k] == 0
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #15  
Old 08-May-2004, 15:40
the_crazyman the_crazyman is offline
Awaiting Email Confirmation
 
Join Date: Apr 2004
Posts: 11
the_crazyman is on a distinguished road
cheers for this.

much appreciated.

sorry I appear to be slow of the blocks with this.
  #16  
Old 04-May-2008, 04:28
CBNewbie CBNewbie is offline
New Member
 
Join Date: May 2008
Posts: 2
CBNewbie is on a distinguished road

Re: deleting elements of arrays c++ (HELP!!)


I'm working on something similar, and I've tried to use the code that you put. The sorting works for me, but when it gets to the delete part, it crashes. I'm working with city names though... Any idea why that could be?
  #17  
Old 04-May-2008, 08:59
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: deleting elements of arrays c++ (HELP!!)


Quote:
Originally Posted by CBNewbie
I'm working on something similar

The last post in this thread was something like four years ago. It's OK to start a new one, especially since you are not working on the exact same thing. I mean, how are we supposed to guess what you are working on? See footnote.

It would be most helpful (to us and, maybe to you, too) if you would give us an exact problem statement. What is the program supposed to do? How do you give it your user input? What are the output requirements? Stuff like that.
Quote:
Originally Posted by CBNewbie
...crashes...

I respectfully suggest the following:

1. Show us the exact code you are working with. If parts of it are derived or copied from somewhere else, that's OK. (Unless it is copyrighted---then you should first get permission from the copyright owner. Code from previous posts on gidforums is OK.)

2. Tell us what happened when you compiled the program. Were there any compiler messages that you didn't understand? You might also mention what compiler you are using. Sometimes it makes a difference to people who are trying to help.

3. Tell us what input you gave the program.

4. Tell us what happened. (Saying that it crashed doesn't give us much information. Sometimes that's all you can say, but sometimes there's more information---did the program have any output at all before it crashed?)

5. Tell us what you expected to happen.

Regards,

Dave

Footnote: It's perfectly OK to ask about old threads. Keep in mind that certain specific problems were covered, and even if the Original Poster's problems were solved (or the solution was understood), there still may be problems with code that appeared in the thread. It may be difficult to believe, but even people who give advice on public forums sometimes make mistakes.

I think that copying from other people's code is sometimes an excellent way to learn. (And sometimes not.)

Sometimes it lets you see how to approach the problem. Sometimes it lets you see that other people's bugs are really hard to spot, especially if you don't have much experience. (And sometimes even if you have a lot of experience.)
  #18  
Old 06-May-2008, 20:16
deejman deejman is offline
New Member
 
Join Date: May 2008
Posts: 3
deejman is on a distinguished road

Re: Deleting elements of arrays C++


I am new to this forum and new C++. I was looking for the code I found in this thread which is how I found this forum and I would like to share my program and what did and didn't work for me with the code that I ended up using.

I did take the code originally posted, but it kept deleting the wrong elements of the array. What I ended up with (with help from a tutor from my school) is the following:
----------------------------------------------------------------------
CPP / C++ / C Code:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
 
#include <cctype>
 
// required for conversion to lowercase
class toLower {public: char operator()(char c) const {return tolower(c);}};
 
void sortNames(string* name, int nNames)
{
for (int i = 0; i < nNames; i++)
{
for (int j = i + 1; j < nNames; j++)
{
if (name[i] > name[j]) //start swap 
{
string temp = name[i];
name[i] = name[j];
name[j] = temp;
} //if
} // for j
} // for i
} // void sortNames
 
void printName(string* name, int nNames)
{
int i;
for (i = 0; i < nNames; i++) // print names greater than 0 length
{
if (name[i].length()!=0) cout << "Name = " << name[i] << endl;
} // for i
} // void printNames
 
void deleteDupes(string* name, int nNames)
{
int temp=0;
bool flag;
 
//For each pair of elements in the array do the following
for(int i=0; i< nNames-temp; ) // while we haven't reached the end of nNames
{ 
string aName=name[i]; // temp variable for first name
string bName=name[i+1]; // temp variable for next name
 
transform(aName.begin(), aName.end(), aName.begin(), toLower()); // make it lowercase to compare
transform(bName.begin(), bName.end(), bName.begin(), toLower());
 
flag=false;
//At each location, the two adjacent elements are compared
if(aName == bName)
{
flag=true;
temp++;
} 
 
if(flag) // if test is true
{
// if elements are the same, then elements are moved up one position
for(int j=i+1;j<=nNames-temp;j++)
{
name[j] = name[j+1]; // duplicate elminated
} //for 
} // if
else i++; // now move i counter
} // for
} // void deleteDupes
 
int main()
{
// create an empty list
const int MAX_NAMES = 100; // capacity
int nNames = 0; // initially empty
string name[MAX_NAMES];
 
// ask for filename and open for input
ifstream fin;
string fileName;
cout << "What file do you want to use for input? ";
getline(cin, fileName);
fin.open(fileName.c_str());
 
// read name from file
while (fin.good())
{
string aName;
getline(fin, aName);
 
// add record to list, if it's not full
if (nNames < MAX_NAMES && aName.length() > 0)
name[nNames++] = aName;
}
fin.close();
 
// sort the name alphabetically
sortNames(name, nNames);
 
// delete duplicate names
deleteDupes(name, nNames);
 
// print names
printName(name, nNames);
 
return 0;
} // main
---------------------------------------------------------------------

I had certain requirements for this program, including to remove duplicates and look by case, so I included in my delete duplicate subprogram extra lines that go with algorithm and cctype to store the values in temporary variables, and convert the values in the array to uppercase.

Obviously, I have text and not integers in my program. Our class was to use array lists, not linked lists, for the purpose of understanding the use of array lists. I changed the font for the void function used in the duplicate deletion.

I have to agree with Davekw7x that it's a 2 edge sword learning from others coding. I've been a system admin for years and have just done simple batch and shell scripting. Learning C++ is whole different animal in regards to learning "true" programming, but my personal experience has been to just nab whatever code I need off the web or my own canned code and tweak it to my needs. Doing that with C++ has been slightly challenging as I don't know enough to know if something will work or how to fix it if it doesn't. But I have learned a good amount.

I hope that by posting my program I can give back what little I have learned and help another newbie or perhaps another programmer who needs some canned code ( I should be so flattered to have someone use my code for anything other than a print screen shot ).

JM2C.

Deej
Last edited by admin II : 07-May-2008 at 05:09. Reason: Please surround your C++ code with [cpp] your code [/cpp]
  #19  
Old 06-May-2008, 20:17
deejman deejman is offline
New Member
 
Join Date: May 2008
Posts: 3
deejman is on a distinguished road

Re: Deleting elements of arrays C++


Hmm, lost my formatting. Sorry about that, it was indented very nicely, but not anymore.
  #20  
Old 07-May-2008, 03:17
Golmal Golmal is offline
Junior Member
 
Join Date: May 2006
Posts: 43
Golmal has a little shameless behaviour in the past

Re: Deleting elements of arrays C++


Hi,
There is some easy way to do , if some sandard libraries are used.

CPP / C++ / C Code:
//-----------------------------------------------------------------------------------
#include<iostream>
#include<vector>
#include<set>
 
using namespace std;
 
int main(){
vector<char> v;
v.push_back('D');
v.push_back('B');
v.push_back('A');
v.push_back('B');
 
set<char> s;
for(vector<char>::iterator it=v.begin();it!=v.end();it++){
cout << (*it) << endl;
s.insert((*it));
}
 
cout << "Removing duplicates" << endl;
for(set<char>::iterator it=s.begin();it!=s.end();it++)
cout << *it << endl;
}
//---------------------------------------------------------------------
~
~
~
~
~
Last edited by admin II : 07-May-2008 at 05:10. Reason: Please surround your C++ code with [cpp] your code [/cpp]
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB 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
need help with passing 3 arrays into a function tommy69 C Programming Language 14 07-Apr-2004 01:22
Problem multiplying arrays hellhammer C Programming Language 9 29-Mar-2004 16:32
Arrays Chazza C++ Forum 10 23-Jan-2004 22:19
pointers and arrays jack C Programming Language 4 15-Jan-2004 13:27
arrays in c wolfgangaz C Programming Language 1 26-Oct-2003 05:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:55.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.