![]() |
|
#11
|
||||
|
||||
|
Quote:
Quote:
Quote:
Quote:
CPP / C++ / C Code:
CPP / C++ / C Code:
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:
CPP / C++ / C Code:
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
__________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#15
|
|||
|
|||
|
cheers for this.
much appreciated. sorry I appear to be slow of the blocks with this. |
|
#16
|
|||
|
|||
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
|
|||
|
|||
Re: deleting elements of arrays c++ (HELP!!)Quote:
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:
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
|
|||
|
|||
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:
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
|
|||
|
|||
Re: Deleting elements of arrays C++Hmm, lost my formatting. Sorry about that, it was indented very nicely, but not anymore.
|
|
#20
|
|||
|
|||
Re: Deleting elements of arrays C++Hi,
There is some easy way to do , if some sandard libraries are used. CPP / C++ / C Code:
~ ~ ~ ~ Last edited by admin II : 07-May-2008 at 05:10.
Reason: Please surround your C++ code with [cpp] your code [/cpp]
|
Recent GIDBlog
Accepted for Ph.D. program by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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