![]() |
|
#1
|
|||
|
|||
Deleting elements of arrays C++New to this and was after some help/direction.
I'm writing a program (new to C++). I've to open a file, read in 20 chars, sort it alphabetically and then remove any duplicates. I can get it to read in, sort and display the sorted array, but I can't work out the deletion bit. Any advice? The source code is below for the programs. Any algorithm that would help would be a massive weight off the shoulders:- CPP / C++ / C Code:
I presume I have to move all elements down 1 position if there is a duplicated pair? But how? Last edited by admin : 27-Jun-2007 at 20:31.
Reason: Use [c] & [/c] for syntax highlighting
|
|
#2
|
|||
|
|||
|
Deleting the elements in an array is not possible. You'd have to use linked lists which involves use of pointers. If you are not familiar with pointers and linked lists, i suggest you do some reading.
One other way I can think of is while you are sorting your array, you can add an else if(GetTwentyLetters[j] == GetTwentyLetters[j+1]) statement after your current if(GetTwentyLetters[j] > GetTwentyLetters[j+1]) and if you find a duplicate, just replace it with '$' or something, and also place a condition that if you find duplicate '$' in your array, just leave it alone because there are bound to be more than one '$' in your array if you have many duplicates. and when you print your elements, just ignore the '$' characters if you want. CPP / C++ / C Code:
|
|
#3
|
||||
|
||||
|
Hello crazyman. Welcome to GIDForums. You can do this using arrays, but as you said, you need to move all of the elements down.
One thing you should do is null terminate the array to make this easier. So after you read in the chars, add: CPP / C++ / C Code:
Make sure your array is large enough to hold this. Then you can just index through, with something like: CPP / C++ / C Code:
Thats untested, but it should give you an idea of how to do it. BTW - did you have to use such a large variable name as GetTwentyLetters? ![]() HTH, d |
|
#4
|
|||
|
|||
|
Ah! I didn't even read the last part before! Sorry about that!
Quote:
|
|
#5
|
|||
|
|||
|
dsmith and machinated.
much appreciated. I've got it working. both very useful tips you gave. ended up using the code from dsmith. simple really when moving the elements up. I wasn't counting up an element, as I was starting the second nested for loop at 0 instead of 1. you stars... |
|
#6
|
|||
|
|||
|
Hi,
only thicko me again I'm still having a few problems with this. the program is this now giving me a few headaches just on the deletion part. using test data FFFFEEEEDDDDCCCCBBAA, should produce, ABCDEF, but instead gives me, ABCCDDEEFF the basis of the program is as follows: I think the problem is in function 3, void deleteDuplicates() but I can't see what's going wrong. I thought I had it working, but as i put more test data through, it's showing a few errors. any help greatly appreciated. (sorry about the format of the text, i've had to copy/paste the program from notepad, as work won;t provide me with a complier CPP / C++ / C Code:
Last edited by dsmith : 05-May-2004 at 07:05.
Reason: Please use [c] & [/c] for syntax highlighting
|
|
#7
|
||||
|
||||
|
*ahem*... http://www.gidforums.com/t-689.html
__________________
-Aaron |
|
#8
|
||||
|
||||
|
Yes,your function 3 is the problem.
CPP / C++ / C Code:
If you look at it carefully, this is what it is actually doing. lets say you have this data in your array: AABBCCCDDD... your loop compare the first element array[0] with the second array[1], and if its the same, it does THE SHIFTING, so this is what you got after the first loop, ABBCCCDDD.. now, it compares array[1] with array[2] and do the shifting, produce this ABCCCDDD here it compares array[2] with array[3], same thing happens ABCCDDD but now is the problem, when comparing array[3] with array[4] and see the resul: ABCCDDD... so I assume you can see the problem here.. To do this coorectly, Issuggest you to do something like this... CPP / C++ / C Code:
what the code does is that it keeps the loop running when it found duplicates and stops when no more duplicates and do the shifting. notice I haven't tested this, its just a guide.. I'll try to test it when I got time later... __________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds |
|
#9
|
||||
|
||||
|
Here is your code, I hope you can understand it. Its just a simple, you need analyse each loops and what happens on each loop..
CPP / C++ / C Code:
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds |
|
#10
|
|||
|
|||
|
hi,
thanks for this. 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, ie. writing out to a temporary array and then sending the temp array contents back into getTwentyLetters. 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:
|
Recent GIDBlog
NARMY 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 00:22 |
| Problem multiplying arrays | hellhammer | C Programming Language | 9 | 29-Mar-2004 15:32 |
| Arrays | Chazza | CPP / C++ Forum | 10 | 23-Jan-2004 21:19 |
| pointers and arrays | jack | C Programming Language | 4 | 15-Jan-2004 12:27 |
| arrays in c | wolfgangaz | C Programming Language | 1 | 26-Oct-2003 04:52 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The