Development Arrays

Discussion in 'Software' started by cool_dude, 14 Jun 2012.

  1. cool_dude

    cool_dude Minimodder

    Joined:
    26 Aug 2004
    Posts:
    2,687
    Likes Received:
    22
    Hey

    Just wondering if you have an array is it possible to select numbers?

    Say instead of j=0; j<20;

    How could I go about 'selecting' for example
    j=0
    j=4
    j=10
    j=15

    I'm aware that I could select "up to" or "less than" a number etc, but commas aren't working and neither are semi-colons!?
     
  2. faugusztin

    faugusztin I *am* the guy with two left hands

    Joined:
    11 Aug 2008
    Posts:
    6,943
    Likes Received:
    268
    Reformulate your question. Define what programming language you are talking about.

    Do you want to search the contents of the array for specified values ?
    Do you want to access specific index of the array ?
     
  3. Guinevere

    Guinevere Mega Mom

    Joined:
    8 May 2010
    Posts:
    2,484
    Likes Received:
    176
    Sounds like you want to enumerate through the array and pick out only very specific values?

    You need to give us more information, starting with the language you're talking about!

    Generally speaking if you've got a question such as "How do I do 'x' with an array in language 'y' then use stack overflow... or just use google and stack overflow will probably come up.
     
  4. Zoon

    Zoon Hunting Wabbits since the 80s

    Joined:
    12 Mar 2001
    Posts:
    5,497
    Likes Received:
    630
    I'll assume PHP....

    Code:
    for ($j=0; $j<20; $j++)
    {
    
    if ($j == 0 || $j == 4 || $j == 10 || $j == 15)
    {
    echo '<p>' . $j . ' is a cool number, screen bro fist me dude.</p>';
    }
    else
    {
    echo '<p>' . $j . ' is just not a cool number, sorry bro.</p>';
    }
    
    }
     
  5. faugusztin

    faugusztin I *am* the guy with two left hands

    Joined:
    11 Aug 2008
    Posts:
    6,943
    Likes Received:
    268
    For that, it is pointless to iterate over the array :D :
    Code:
    function doSomethingCool($param) {
      echo "something cool $param<br>";
    }
    
    doSomethingCool($array[0]);
    doSomethingCool($array[4]);
    doSomethingCool($array[10]);
    doSomethingCool($array[15]);
    
     
  6. azrael-

    azrael- I'm special...

    Joined:
    18 May 2008
    Posts:
    3,852
    Likes Received:
    124
    Are you thinking of something like this?

    Code:
    int numbers[4] = { 0, 4, 10, 15 };
    You could then access them somewhat like this.

    Code:
    for (int i = 0; i < 4; ++i)
    {
      printf("%d. element of array j: %d\r\n", i+1, j[i]);
    }
    
    Which would output:
    Code:
    1. element of array j: 0
    2. element of array j: 4
    3. element of array j: 10
    4. element of array j: 15
    
    That's C/C++ btw...
     
  7. cool_dude

    cool_dude Minimodder

    Joined:
    26 Aug 2004
    Posts:
    2,687
    Likes Received:
    22
    Apologies for the late reply...

    It's Java based so, for example....

    The code for the array is.... (j=0; j<10; i++)

    That means go from J=0 to J=10 in +1 increments... I'm aware that setting it to I+3 would make it go up in every 3 increments and so on.

    I'm trying to work out how I can have the code for J=0 J=2 J=4 J=8 and J=9.

    As you can see not in an 'even' increment.

    I hope you can see what I mean?
     
  8. TheManicGibbon

    TheManicGibbon I have no idea what's going on

    Joined:
    12 Aug 2009
    Posts:
    427
    Likes Received:
    13
    xCould you maybe explain a bit of context...you can't have a loop incrementing with arbitrary amounts as far as I know.

    So you mean if you had the array {1,2,3,4,5,6,7,8,9,10)

    And the code you had in the loop was to print out item "J" of the loop; the output would be:
    Code:
    1
    3
    5
    9
    10
    
    Assuming the array index starts from 0? Is that what you mean?

    If this is what you're looking for, you could have J as a reference to a second array containing the indexes you wanted to access? If that makes sense. Let me see if that works.
     
  9. faugusztin

    faugusztin I *am* the guy with two left hands

    Joined:
    11 Aug 2008
    Posts:
    6,943
    Likes Received:
    268
    But why ? Anyway :

    Code:
    public class Example {
    
    	public static void main(String[] args) {
    		String[] data = new String[]{
    			"Zero string",
    			"First string",
    			"Second string",
    			"Third string",
    			"Fourth string",
    			"Fifth string",
    			"Sixth string",
    			"Seventh string",
    			"Eight string",
    			"Ninth string",
    			"Tenth string"
    		};
    		int[] indexes = new int[]{0,2,4,8,9};
    		for (int i=0;i<indexes.length;i++) {
    			System.out.println(data[indexes[i]]);
    		}
    	}
    }
    Output :
    Code:
    Zero string
    Second string
    Fourth string
    Eight string
    Ninth string
     
  10. TheManicGibbon

    TheManicGibbon I have no idea what's going on

    Joined:
    12 Aug 2009
    Posts:
    427
    Likes Received:
    13
    Yeah, it would be this in Java, still not sure what this could be for though:
    Code:
    public class ExperimentationProj {
    
        public static void main(String[] args) {
            int [] indexes = {0,2,4,8,9};
            
                    
            for (int j=0;j<indexes.length;j++){
                System.out.println("Output" + indexes[j]);
            }
        }
    }
    Giving output:
    Code:
    Output0
    Output2
    Output4
    Output8
    Output9
    EDIT: faugusztin, ninja'd :(
     
  11. Flibblebot

    Flibblebot Smile with me

    Joined:
    19 Apr 2005
    Posts:
    4,734
    Likes Received:
    207
    Actually, it goes from j=0 to j=9 (to go from 0-10, you'd need (j=0;j<=10;j++). Also, your loop is incrementing i, but checking j...
    As with the others, I can't see why you would only set certain members of an array. This is a waste of memory, as you'd have empty members of an array.

    Perhaps if you explain what you're trying to achieve, then we might be able to help.
     
  12. cool_dude

    cool_dude Minimodder

    Joined:
    26 Aug 2004
    Posts:
    2,687
    Likes Received:
    22
    Still quite confused here.

    Is there not a simple code like there is to do the +3 increments with say "i+3".

    I know I am wasting an array however I'm just trying to see if its possible to select out the positions within an array. I found the I++ (increment of +1) and I+2 (increment of +2) etc.

    I'm trying to achieve the positions in the array of J=0 J=2 J=4 J=8 and J=9 only out of a 10 piece array - so 5 'positions'.

    Rather than going through the loop of the array as the code suggests j=0; j<10; i++ that I could use the i++ function to select OR type j=0,2,4,8,9; (Or something to that effect).

    Hope that makes sense.
     
    Last edited: 29 Jun 2012
  13. faugusztin

    faugusztin I *am* the guy with two left hands

    Joined:
    11 Aug 2008
    Posts:
    6,943
    Likes Received:
    268
    cool_dude, are you blind to the two code examples above ? And still no one sees a logic in your request, because what you seek has absolutely no use for any real world programming scenario. I simply don't see how that would be valuable for any application, at all.
     
  14. TheManicGibbon

    TheManicGibbon I have no idea what's going on

    Joined:
    12 Aug 2009
    Posts:
    427
    Likes Received:
    13
    cool_dude, what you're trying to achieve is a waste of memory and is inefficient. The code samples we've provided for you in Java show you exactly how to do what you're trying to achieve. Let me explain.

    There is no way to increment a loop by a random amount. Think about it. How would it know what number to increment by in each run.

    The code provided by myself and faugusztin provide a workaround by using a second array as a list of the indexes that your loop is to use. The loop references this array to see what its next index should be.

    That's as close as you're going to get I'm afraid. We understand what you're trying to do, could you explain why? We might be able to give you a more efficient solution.
     
    BentAnat likes this.
  15. azrael-

    azrael- I'm special...

    Joined:
    18 May 2008
    Posts:
    3,852
    Likes Received:
    124
    Don't know if Java has associative containers but that would be a way to minimize memory usage. Still can't figure out why the OP wants to do what he/she/it wants to do...
     
  16. roundyz

    roundyz What's a Dremel?

    Joined:
    16 Jan 2002
    Posts:
    153
    Likes Received:
    2
    late to the game

    Sorry for being late to the thread and my java is rusty so bear with..

    Code:
    public class test {                                                                                                        
        public static void main(String[] args)                                                                                 
        {                                                                                                                      
            int[] a= {0,1,2,3,4,5,6,7,8,9, 10};                                                                                
            for(int i = 0; i<10; i++) {                                                                                        
                if (i%2==0 || i ==9)                                                                                                                                                                                                                                                                                              
                    System.out.println(a[i]);                                                                                  
            }                                                                                                                  
        }                                                                                                                      
    }       
    
    This works. I think what I can gather from what you asked is you wanted the modulus. Another way you could do this is by setting i to 2 and the increment to i=i+2. Hope this helps :)
     
  17. theshadow2001

    theshadow2001 [DELETE] means [DELETE]

    Joined:
    3 May 2012
    Posts:
    5,263
    Likes Received:
    176
    Its only been 3 years. But I'm sure he's still there sitting in front of his monitor, with tired blood shot eyes wondering how he can solve the problem.

    Actually what I think he wanted to do was reduce the number of overall iterations so the loop would only iterate as many times as the number of elements he wanted to operate on. As opposed to looping through the full 10 times.

    So he wanted five elements in a 10 element array, but the elements weren't evenly positioned in the array. He wanted to operate on those elements by looping only 5 times and not 10 times.

    The way to do that would be to dynamically adjust the iterator value i in the loop itself rather than in the for loop header. See the quick sample in C below.

    Code:
    #include <stdio.h>
    
    int main()
    {
        int i = 0;
        int iterationNum = 0;
        int array[10] = {1,2,3,4,5,6,7,8,9,10};
    
        for(i = 0; i<10; i++){
            if(i < 7){
                i++;
            }
            else if(i == 9 )
                break;
    
            printf("%d",array[i]);
    
            printf(" Iteration: %d\n",iterationNum);
    
            iterationNum++;
        }
    
        return 0;
    }
    
    
    Which will output

    Code:
    2 Iteration: 0
    4 Iteration: 1
    6 Iteration: 2
    8 Iteration: 3
    9 Iteration: 4
    But the above is some horrible code and no one should do things that way. Your approach is better roundyz even though it is iterating all 10 times.
     
  18. roundyz

    roundyz What's a Dremel?

    Joined:
    16 Jan 2002
    Posts:
    153
    Likes Received:
    2
    lol, yerh should have checked the date more thouroughly. He could have done a cs degree in that time!
     

Share This Page