Delphi and Kylix

Moderators: pritaeas
Number of threads: 7244
Number of posts: 19051

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
non repeating array Posted by Rico124a on 4 Sept 2004 at 6:42 PM
This message was edited by Rico124a at 2004-9-4 18:43:44

How would you go about creating an array that would not repeat results?
example:
I want 15 different results from
aTrait: array [1..100] of string =

thanks


Report
Re: non repeating array Posted by zibadian on 4 Sept 2004 at 9:58 PM
: This message was edited by Rico124a at 2004-9-4 18:43:44

: How would you go about creating an array that would not repeat results?
: example:
: I want 15 different results from
: aTrait: array [1..100] of string =
:
: thanks
:
:
What do you mean by 15 different results? If you want 15 different arrays with 100 string elements, you can either create 15 different array variables, or a 2-D array with in the second dimension 15 elements.
Report
Re: non repeating array Posted by Rico124a on 5 Sept 2004 at 2:24 AM
: : This message was edited by Rico124a at 2004-9-4 18:43:44

: : How would you go about creating an array that would not repeat results?
: : example:
: : I want 15 different results from
: : aTrait: array [1..100] of string =
: :
: : thanks
: :
: :
: What do you mean by 15 different results? If you want 15 different arrays with 100 string elements, you can either create 15 different array variables, or a 2-D array with in the second dimension 15 elements.
:
Your giving me way too much credit. Just a simple 1-D array of 100 string elements all pertaining to colors (from white to black). I input that I want 15 colors from that array. What I want to see is 15 different colors spit out into a memobox.
example: white, red, blue, black, green, yellow, pink, etc .
not: red, red, blue, green, pink, red, puse, etc.
Thanks
Report
Re: non repeating array Posted by zibadian on 5 Sept 2004 at 4:07 AM
: : : This message was edited by Rico124a at 2004-9-4 18:43:44

: : : How would you go about creating an array that would not repeat results?
: : : example:
: : : I want 15 different results from
: : : aTrait: array [1..100] of string =
: : :
: : : thanks
: : :
: : :
: : What do you mean by 15 different results? If you want 15 different arrays with 100 string elements, you can either create 15 different array variables, or a 2-D array with in the second dimension 15 elements.
: :
: Your giving me way too much credit. Just a simple 1-D array of 100 string elements all pertaining to colors (from white to black). I input that I want 15 colors from that array. What I want to see is 15 different colors spit out into a memobox.
: example: white, red, blue, black, green, yellow, pink, etc .
: not: red, red, blue, green, pink, red, puse, etc.
: Thanks
:
The trick here is to fill a 15-element array in such a way they you check each element to be added with all previous elements. Here is a simple example:
var
  ColorToBeAdded: string;
  ColorsArray: array[1..15] of string;
  NumColor: integer;
  i: integer;
  FoundColor: boolean;
begin
  // Example Initializion: Run only once
  NumColor := 0;
  ColorToBeAdded := 'red';
  // Start of "real example code"
  FoundColor := False; // Assume the color isn't in the list
  if NumColor > 0 then // Check if there are already some added
    for i := 1 to NumColor do
      if ColorToBeAdded = ColorsArray[i] then
        FoundColor := true; // Found a matching color
  if not FoundColor then // Add the new color
  begin
    inc(NumColor);
    ColorsArray[NumColor] := ColorToBeAdded;
  end;
end;

This code will add the color red to the array. If you run the "real example code" twice with red as color, you will find only 1 element is filled, and NumColor is still 1. Only if you add a non-red color, it will be added.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.