Arrays for dummies

Archive of the old editing forum
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. This forum is archived - please use this set of forums to ask new questions.
Locked
User avatar
Travis
Posts: 79
Joined: Sat Jul 29, 2006 7:09 pm
Contact:

Arrays for dummies

Post by Travis »

Alright, up until this point, I've only used arrays for strings. I never even realised it would be possible to store integers within them. After doing a few searches and looking through about 15 threads, I'm still a little confused.

Let me get a few things straight here.

Code: Select all

int arrayfour[4];
Sets four integers... 0, 0, 0, 0. Correct? So that would mean....

Code: Select all

script 02 open
{
    arrays[1]=8;
}
Would make 0, 8, 0, 0. Correct?

That all being said, would it be possible to get the amount of integers in an array that equal a number? For example, you have an array of 7 integers, and 3 of those integers = 2. How would I get that 3? The closest thing I can get is something like this, but it isn't quite working properly...

Code: Select all

numbers = (arrayseven[0 && 1 && 2 && 3 && 4 && 5 && 6 && 7]==2);
User avatar
TheDarkArchon
Posts: 7656
Joined: Sat Aug 07, 2004 5:14 am
Location: Some cold place

Post by TheDarkArchon »

arrays[7] = {2};, IIRC.
User avatar
Necromage
Posts: 484
Joined: Thu Feb 10, 2005 3:13 pm
Location: NJ
Contact:

Post by Necromage »

In order to do that you will have to something like this:

Code: Select all

for(int i = 0; i < 7; i++)
         if(arrays[i] == 2) numbers++;
What this says is that for every integer, i, less then seven and starting at 0, I want to know if the value of arrays is equal to 2 and if so add 1 to numbers.
User avatar
HotWax
Posts: 10002
Joined: Fri Jul 18, 2003 6:18 pm
Location: Idaho Falls, ID

Post by HotWax »

After declaration, you can only access one member of an array at a time. That's why you have to do something like what Necro posted.
User avatar
TheDarkArchon
Posts: 7656
Joined: Sat Aug 07, 2004 5:14 am
Location: Some cold place

Post by TheDarkArchon »

Mine was supposed to be a declaration, though I made a couple of gaffs (missing ";" and missing "int" prefix).
User avatar
Travis
Posts: 79
Joined: Sat Jul 29, 2006 7:09 pm
Contact:

Post by Travis »

HotWax wrote:After declaration, you can only access one member of an array at a time. That's why you have to do something like what Necro posted.
Argh. That's gonna be a problem, since I'm looking for something that constantly moniters an array.

Thanks for the efforts though, I'll play around with what you've given me a bit here...
User avatar
durnurd
Posts: 116
Joined: Wed Jan 18, 2006 8:44 pm
Location: Minnesota

Post by durnurd »

What's wrong with the code Necro gave?

Code: Select all

script 5 (void)
{
   int numbers = 0;
   for(int i = 0; i < 7; i++)
      if(arrays[i] == 2) numbers++;
   if (numbers == 3)
      dosomething();
   restart;
}
User avatar
HotWax
Posts: 10002
Joined: Fri Jul 18, 2003 6:18 pm
Location: Idaho Falls, ID

Post by HotWax »

Travis wrote:Argh. That's gonna be a problem, since I'm looking for something that constantly moniters an array.
Hey, don't worry about it. Modern computers are lightning-fast little beasties. :P

This script will display a constantly-updated count of the number 2 within an array of 100 values:

Code: Select all

int MyArray[100];

script 200 ENTER {
     int Count, i;

     while (1) {
          for (i=0; i<100; i++) if (MyArray[i] == 2) Count++;

          Print(s:"Current count: ", n:Count);
          delay(1);
     }
}
This shouldn't faze ZDoom at all, even though it's running the loop 100 times per tic (3500 times a second).
TheDarkArchon wrote:Mine was supposed to be a declaration, though I made a couple of gaffs (missing ";" and missing "int" prefix).
To my knowledge you cannot assigned an entire array to a single number. Your code would assign the number 2 to the 0th element of the array and leave the rest as 0's.

However, you can do something like this:

Code: Select all

int ArrayOfTwos[8] = { 2, 2, 2, 2, 2, 2, 2, 2 };
int MixedArray[10] = { 3, 1, 8, 2, 5, 0, -2, 4, 3, 0 };
int StringAndIntsCauseACSRox[5] = {
     17,
     "Charlie",
     "Brown",
     -2,
     0
};
User avatar
Travis
Posts: 79
Joined: Sat Jul 29, 2006 7:09 pm
Contact:

Post by Travis »

Bear with me here, guys. You all appear to think that necro's code is what should work, which it doesnt...meaning I'm most likely doing something wrong elsewhere. Let me go back and troubleshoot a few minutes...I'll edit this post if I can't seem to get it working.

EDIT:: Yeah, I'm just not getting it. I'm using your script, Hotwax, and it's just constantly adding to the Count int.

EDIT 2:: Nevermind, guys, I got a system workin' on my own. I appreciate all the help though!
User avatar
HotWax
Posts: 10002
Joined: Fri Jul 18, 2003 6:18 pm
Location: Idaho Falls, ID

Post by HotWax »

Stupid forum bug deleted my post (grumble, grumble).
Travis wrote:EDIT:: Yeah, I'm just not getting it. I'm using your script, Hotwax, and it's just constantly adding to the Count int.
That's because I made a noobie mistake. :oops:

Code: Select all

while (1) {
     Count = 0; // <-- Add this line
     for (i=0; i<100; i++) if (MyArray[i] == 2) Count++; 

     Print(s:"Current count: ", n:Count); 
     delay(1); 
}
Locked

Return to “Editing (Archive)”