Languages :: Pascal :: array deletion |
|||
| By: digitaltree |
Date: 26/04/2003 00:00:00 |
Points: 25 | Status: Answered Quality : Excellent |
|
hi I need this program to do multiple deletions but its only doing one . what do I need to do with the procedure purge? PROGRAM SortingofArray; Const amax = 12; TYPE ArrayIndex = 1..amax; LetterList = RECORD count: 0..amax; alpha: ARRAY[ArrayIndex] OF char; END; VAR F :LetterList ; n: integer; m:char; PROCEDURE LoadArray( var C : LetterList; x: integer); var Index: ArrayIndex; BEGIN writeln ('Enter ',x, ' characters:'); with C DO BEGIN Count := x; FOR Index := 1 to Count DO readln(alpha[Index]); writeln; END; END; PROCEDURE PrintArray( D:LetterList ); VAR i: integer; Index: ArrayIndex; BEGIN with D DO BEGIN writeln('Count: ',count); write(' '); FOR i :=1 TO amax DO write(i:4); writeln; write('alpha '); FOR Index := 1 TO Count DO write(alpha[Index]:4); writeln; END; END; PROCEDURE Purge(var Letters: LetterList; ToPurge: char); var i ,k : integer; Index: ArrayIndex; begin k:=0; with Letters DO BEGIN For index := 1 to count do if alpha[index] = ToPurge then begin k:=k+1; For index := index to count do alpha[index]:=alpha[index+k]; end; Count:=Count-k; END; End; BEGIN Writeln ('Enter number of character that would be loaded: '); readln(n); LoadArray(F,n); PrintArray(F); writeln; write('Enter letter to purge: '); readln(m); Purge(F,m); PrintArray(F); readln; End. |
|||
| By: VGR | Date: 26/04/2003 17:07:00 | Type : Comment |
|
| that's because when you suppress a charcater, count computed at the beginnign of the loop has stayed the same and gets incremented, while the deletion wxould have suggested to treat again the character of the same index. I hope I'm clear. the loop is badly written with a for, given : -count of the for varies during iteration -you use for index:=index to ... something that is not encouraged do you want it rewritten ? |
|||
| By: VGR | Date: 26/04/2003 17:15:00 | Type : Answer |
|
| here it is : -------------------- results Enter number of character that would be loaded: 5 Enter 5 characters: A B B C B Count: 5 1 2 3 4 5 6 7 8 9 10 11 12 alpha A B B C B Enter letter to purge: B Count: 2 1 2 3 4 5 6 7 8 9 10 11 12 alpha A C ------------------ source code PROCEDURE Purge(var Letters: LetterList; ToPurge: char); var i ,k : integer; Index, index2: ArrayIndex; begin index:=1; With Letters Do While (index<=count) Do Begin if alpha[index] = ToPurge then begin For index2 := index to count do alpha[index2]:=alpha[index2+1]; Dec(count); end { For } Else Inc(index); End; { While, With } End; { Purge Procedure } |
|||
| By: VGR | Date: 26/04/2003 17:16:00 | Type : Comment |
|
| the "{ For }" in line "end { For }" is an old comment I had put in your code. Ignore it. |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








