Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28

Thread: array

  1. #21
    ThrashingBoy is offline Member
    Join Date
    Apr 2010
    Location
    Phoenix, AZ
    Posts
    25
    Rep Power
    0

    Default

    I hope this is what you are looking for :)

    Java Code:
    package PracticeSpace;
    
    public class PracticeMain{
    
    	public static void main(String[] args) {
    		
    		int[] originalArray = {3,4,1,2,3};
    		int[] newArray = new int[originalArray.length - 1]; 
    
    		for(int counter = 0; counter < originalArray.length - 1; counter++){
    			newArray[counter] = originalArray[counter] + originalArray[counter + 1];			
    		}
    
    		for(int counter = 0; counter < originalArray.length; counter++){
    		System.out.print(originalArray[counter] + " ");
    		}
    		System.out.println();
    		for(int counter = 0; counter < newArray.length; counter++){
    			System.out.print(newArray[counter] + " ");
    		
    		}
    	}
    }
    output
    Java Code:
    3 4 1 2 3 
    7 5 3 5

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    @ThrashingBoy
    If you're going to write the code for the OPs problem, could you at least explain what it does and the possible problems he would have had if he had written it himself so he can learn. For example explain this: < originalArray.length - 1

  3. #23
    ThrashingBoy is offline Member
    Join Date
    Apr 2010
    Location
    Phoenix, AZ
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    @ThrashingBoy
    If you're going to write the code for the OPs problem, could you at least explain what it does and the possible problems he would have had if he had written it himself so he can learn. For example explain this: < originalArray.length - 1
    You're right, sorry, wasn't really thinking about that, I am still a bit of a newbie too and was just happy to be able to answer a question :)

    Java Code:
    package PracticeSpace;
    
    public class PracticeMain{
    
    	public static void main(String[] args) {
    		
    		int[] originalArray = {3,4,1,2,3};
    		int[] newArray = new int[originalArray.length - 1]; 
    
    		for(int counter = 0; counter < originalArray.length - 1; counter++){
    			newArray[counter] = originalArray[counter] + originalArray[counter + 1];			
    		}
    
    		for(int counter = 0; counter < originalArray.length; counter++){
    		System.out.print(originalArray[counter] + " ");
    		}
    		System.out.println();
    		for(int counter = 0; counter < newArray.length; counter++){
    			System.out.print(newArray[counter] + " ");
    		
    		}
    	}
    }
    output:
    Java Code:
    3 4 1 2 3 
    7 5 3 5
    So what my code is doing is first creating an array called originalArray, which is the array that was want to have the numbers added together to. Then we create a new array that is the size of the originalArray (which is what the .length does) - 1 number, so the originalArray can be any size and it will work fine. Because no matter how big you mean the original Array, the newArray will always be 1 spot smaller.

    Then in the first for loop it is doing all the adding. So when counter is at 2 it will add like newArray[2] = originalArray[2] + originalArray[2 + 1]. So it will add the spot the counter is on and the one next to it and put the sum in the correct spot for the newArray.

    Then the next for loops are simply printing out the arrays. Hope this explanation helped, let me know if I can clarify anything. :)

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    Thanks for that.

    One observation: If the comments about the code where actually in the code, anyone reading the code could see them with the code and not have to scroll up and down and up and down etc

  5. #25
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    Hope this explanation helped, let me know if I can clarify anything.
    but that is if you have fixed array's elements... what if the array's elements will be input first?
    Heart is the only broken thing that still works.

  6. #26
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Thanks for that.

    One observation: If the comments about the code where actually in the code, anyone reading the code could see them with the code and not have to scroll up and down and up and down etc



    I used the keyword "return" to repeat the counting and adding elements so that I can have...


    5 4 5 3 2
    9 9 8 5
    18 17 13
    35 30
    65

    but it doesn't work... what is the proper keyword for that?


    Thanks...
    Heart is the only broken thing that still works.

  7. #27
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    I used the keyword "return" to repeat the counting and adding elements
    Can you show the code?
    The return statement exits(returns from) the currently executing method.

    but it doesn't work
    Can you describe what 'doesn't work' means?

    to repeat the counting and adding elements so that I can have.
    Take a piece of paper and write down a sample array of integers.
    Then go thru that array and do the addition operation you want to do, replacing the elements in the original array with the new values.
    Then do it again and again and again until there is only one number left. See how the number of elements you add each time decreases by one.
    You will need loops to do the "again and again" part down to the one item left.
    Last edited by Norm; 08-24-2010 at 02:01 AM.

  8. #28
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    1. declare let us declare the given numbers as givenArray
    2. declare an integer[] that will hold the "added " givenArray, or the sum of sum of givenArray
    3. Use do while statement, this will do the code while newArray.lenght is not equal to 1
    4. Use for next statement to add the givenArray values, add sum of sum
    5. add the current array and the next array and pass the sum to newArray
    6. pass the newArray to the givenArray, we are doing this for the next Do While statement
    7. Everytime you add each value in givenArray it decrease its lenght. So formula will be newArray.lenght - 1
    8. When Do.. While statement is done, it means that the output that you want can be computed already.
    Java Code:
    public class ArraySumofSum
    {   public ArraySumofSum()
        {   //1.
            int givenArray[] = {5,2,4,6,8};
    
            //2.
            int newArray[] = new int[4];
    
            //3.
            do
            {   //4.
                for (int iGivenArray = 0; iGivenArray < newArray.length; iGivenArray++)
                {   if(iGivenArray < newArray.length)
                    {   //5.
                        newArray[iGivenArray] = givenArray[iGivenArray] + givenArray[iGivenArray + 1];
                        
                        //6.
                        givenArray[iGivenArray] = newArray[iGivenArray];
                        System.out.println(givenArray[iGivenArray]);
                    }
                }
                //7.
                newArray = new int[newArray.length - 1];
            }
            while(newArray.length > 1);
            //8
            newArray[0] = givenArray[0] + givenArray[1];
    
            //print newArray
            System.out.println("------ .:: NEW ARRAY ::. -------");
            for(int iGivenArray = 0; iGivenArray < newArray.length; iGivenArray++)
            {   System.out.println(newArray[iGivenArray]);
            }
        }
    
        public static void main(String[] args)
        {   ArraySumofSum at = new ArraySumofSum();
        }
    }
    //5+2 = 7, 2+4 = 6, 4+6 = 10, 6+8 = 14
    //7+6 = 13, 6+10 = 16, 10+14 = 24
    //13+16 = 29, 16+24 = 40
    //29 + 40 = 69
    //69

Page 2 of 2 FirstFirst 12

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 11:32 AM
  2. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 09:01 PM
  3. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 11:41 AM
  4. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Posting Permissions

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