• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] Exercise

Status
Not open for further replies.
Level 5
Joined
Oct 15, 2009
Messages
136
I need help with this exercise, I've been trying to make the solution, but I get confused with the *10 equation, and the GetRandomInt. Could anyone made that for me please? It impossible for me, although it must be really simple actually, but I can't do it.


For this exercise, you will be using the GetRandomInt native (random number) and I2S native-
JASS:
native GetRandomInt takes integer lowBound, integer highBound returns integer

Create a function that sets the values of an array to their indexes * 10 from 1 to a random integer that is anywhere from 1 to 100. So x[10] would be 100 and x[11] would be 110. If you had a max of 5, you'd set x[1]=10, x[2]=20, x[3]=30, x[4]=40, and x[5]=50.

Display the maximum index after completing the loop. You are allowed to use a maximum of 2 variables.

JASS:
function Print takes string msg returns nothing
    call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, msg)
endfunction

Example Output

Code:
Initial max is: 34
Found max is: 34

After completing this, add another variable that stores the initial max and then compare the found max to the initial max. If they are equal, output "Max found" and if they aren't equal, output "Max not found"
 
Level 5
Joined
Oct 15, 2009
Messages
136
can you make a code example of what you just said? I would be really grateful

Or where do you got the x variable from?
 
Last edited:
Level 6
Joined
Oct 10, 2009
Messages
1,426
Is this even possible with TWO variables?
I can do it with three...but how would you do it with two?

And as for the second part, I didn't understand what it wants.

Sooo...here's the code for the first part...I believe it should work...in theory.
JASS:
function Trig_Untitled_Trigger_001_Actions takes nothing returns nothing
    local integer randomInt = GetRandomInt(0,100)
    local integer indexInt = 0
    local integer array intArray
    loop
        set intArray[indexInt] = indexInt*10
        set indexInt = indexInt + 1
        exitwhen indexInt > randomInt
    endloop
endfunction

//===========================================================================
function InitTrig_Untitled_Trigger_001 takes nothing returns nothing
    local trigger t = CreateTrigger(  )
    call TriggerAddAction( t, function Trig_Untitled_Trigger_001_Actions )
    call TriggerRegisterTimerEvent(t,0.5,false)
endfunction
 
This is how you would do it with two. :)
JASS:
function LoopTest takes nothing returns nothing
    local integer loopMax = GetRandomInt(1,100) //This is the maximum times to loop
    local integer array x
    call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"Initial max is: "+I2S(loopMax))
    loop
        set x[loopMax] = loopMax*10
        exitwhen loopMax == 0 
        set loopMax = loopMax-1 //just keep decrementing the loopMax until it gets to 0
    endloop
endfunction

Essentially, it is doing the same thing, but with 1 less variable. Instead of setting 0-randomInt to their appropriate indexes, it will set randomInt-0 to their appropriate indexes, maintaining the same results. ;D
 
Level 5
Joined
Oct 15, 2009
Messages
136
Well, I got to this.

JASS:
scope Fuck initializer fuckthis
    function fuckthis takes nothing returns nothing
        local integer initialmaxID = GetRandomInt(1, 100)
        local integer array x
        local integer foundmaxID = initialmaxID
            call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Initial max ID is: " + I2S(initialmaxID))
            call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Found max ID is: " + I2S(foundmaxID))
            loop
                set x[initialmaxID] = initialmaxID * 10
                exitwhen initialmaxID == 0 
                set initialmaxID = initialmaxID - 1
            endloop
                if foundmaxID == initialmaxID then
                    call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Max found")
                else
                    call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Max not found")
                endif
    endfunction
endscope

How do I get the code working according to the problem?
 
Yeah, if you were to do that you would need to count upward. xD
JASS:
scope Fuck initializer fuckthis
    function fuckthis takes nothing returns nothing
        local integer initialmaxID = GetRandomInt(1, 100)
        local integer i = 0
        local integer array x
        call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "InitialmaxID is: " + I2S(initialmaxID))
        loop
            exitwhen i == initialmaxID 
            set x[i] = i * 10 
            set i = i + 1
        endloop
        if i == initialmaxID then
            call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Max found")
        else
            call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Max not found")
        endif
    endfunction
endscope

Otherwise, I'm not sure how Nestharus would want you to do it. You can do it plenty of ways though, this is just one method of doing it.
 
Level 5
Joined
Oct 15, 2009
Messages
136
Ok, that would be fine for me. Another thing: There's a point where it says "Output the initial and the found max". How do I output the found max?

And If I am not wrong, this code will always say that the max is found, because the loop declares "exitwhen i == initialmaxID", right?
 

Dr Super Good

Spell Reviewer
Level 65
Joined
Jan 18, 2005
Messages
27,289
There are multiple ways to find a maximum. The general is an O(n) loop through all indicies with values recording the maximum. By this, if it finds a value larger than the current maximum it sets the current maximum to it.

Obviously if you sort the data, higher efficency can be obtained eg O(1) if in quantitive order.
 
Level 5
Joined
Oct 15, 2009
Messages
136
but I dont understand how to make the code for that thing you just said. I mean, I try to get the comparisson between the Initial max ID and the found max ID, I always try it, but I get confused on how making the code. And the comparisson always output that the max was found. As for me, I suppose this is the problem:

JASS:
exitwhen i == InitialmaxID

Could you plase tell me a way of how do I get the output of the Initial max and the Max found, just like it says on the exercise:

Output example:

Code:
Initial max: 34
Found Max: 34
 
Ok, that would be fine for me. Another thing: There's a point where it says "Output the initial and the found max". How do I output the found max?

And If I am not wrong, this code will always say that the max is found, because the loop declares "exitwhen i == initialmaxID", right?

For the second part, yeah it will always have the max found. To display the found max, just do this:
JASS:
scope Fuck initializer fuckthis
    function fuckthis takes nothing returns nothing
        local integer initialmaxID = GetRandomInt(1, 100)
        local integer i = 0
        local integer array x
        call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "InitialmaxID is: " + I2S(initialmaxID))
        loop
            exitwhen i == initialmaxID 
            set x[i] = i * 10 
            set i = i + 1
        endloop
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Found Max: "+I2S(i)) 
        //display the found max, otherwise known as "i"
        if i == initialmaxID then
            call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Max found")
        else
            call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "Max not found")
        endif
    endfunction
endscope

That should work. :)
 
Status
Not open for further replies.
Top