Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2013
    Location
    Aloha \m/
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question (Java) Help with alternating the negative and positive numbers of an array.

    Hello coders, I'm new to this forum and especially new to programming.

    But my program objective is to display an array of 200 integers, with the numbers(element) starting at 5, increasing by 5, and negate the even numbers. For instance: 5, -10, 15, -20 and so on.

    I'm having problems negating and displaying the negative numbers
    I believe I'm suppose to use modulus, but I have no clue how

    What I have so far:
    Code:
     public class ArraysAssignment2 {
     	public static void main(String [ ] commandlineArguments){
    																																																																												//  2007 William Albritton
          //Declare & Instatiate an Array of 200 integers
             Integer[ ] arrayOf200Integers = new Integer[200];
          	
          //initialize the array
             Integer powersOf2 = new Integer(5);	
             for(int i=0;i<arrayOf200Integers.length;i++){
                arrayOf200Integers[i] = powersOf2;
             	//add again by 5
                powersOf2 = powersOf2 + 5;
             }
             //Display Array
             Integer numerator = new Integer(0);
    				for(int i=0;i<arrayOf200Integers.length;i++){
                numerator = i;
                System.out.println("index = " +numerator + "," + "element =" + arrayOf200Integers[i]);
    			}	      	 
          }
       }
    My output:
    index = 0,element =5
    index = 1,element =10
    index = 2,element =15
    index = 3,element =20
    index = 4,element =25
    ETC.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    There are several ways to get the negatives on the evens. The easiest way I can think of without writing anything custom is to:
    • PUSH powersOf2 to array
    • IF i IS odd then // Modulus would actually help
      • Multiply array[i] <= -1 // In java you can use a *= operator
    • SET powersOf2 TO powersOf2 + 5 // In java you can use a += operation

    Within your loop. This way you never disrupt the powersOf2 value and only set the negative number where its required.

    Be aware as well that there is a difference between an Integer[] and an int[]. In int can go into an Integer due to implicit wrap, but not the other way around.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •