Page 1 of 2 12 LastLast
Results 1 to 20 of 28

Thread: array

  1. #1
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default array

    For example I have this array : 5 2 4 6 8
    then I want to add: 5 and 2, 2 and 4, 4 and 6, 6 and 8

    I really don't know how to add the element of index to the element of next index. I hope someone can help me in formula... I really need it on my project.

    Please tell me.. please, please, please :)

    Thanks a lot!
    Heart is the only broken thing that still works.

  2. #2
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    4

    Default

    What have you attempted so far? maybe one of your attempts was close enough that we just need to tweak it a little.
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  3. #3
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by Sno View Post
    What have you attempted so far? maybe one of your attempts was close enough that we just need to tweak it a little.
    I tried to initialized one variable that will hold the sum of two elements.

    temp = a[ctr] + a [ctr];

    output:

    array: 5 2 4 6 8
    10 4 8 12 16


    which is wrong because the element adds to itself...
    it should add to the next element...
    so the output should be like this...

    array: 5 2 4 6 8
    7 6 10 14
    Heart is the only broken thing that still works.

  4. #4
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    So you want to loop through the array and then add each item to the next item?

  5. #5
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    4

    Default

    Your code is confusing to me.

    So yes in this statement
    Java Code:
    temp = a[ctr] + a [ctr];
    you are adding.

    What do you mean adding it to the next element
    like this?
    5+2 = 7, 2+4=6,...

    so CTR is a position in the array

    so you need to have one array +1;
    try this
    Java Code:
    temp = temp = a[ctr] + a[ctr++];
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  6. #6
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    Quote Originally Posted by Sno View Post
    Your code is confusing to me.

    So yes in this statement
    Java Code:
    temp = a[ctr] + a [ctr];
    you are adding.

    What do you mean adding it to the next element
    like this?
    5+2 = 7, 2+4=6,...

    so CTR is a position in the array

    so you need to have one array +1;
    try this
    Java Code:
    temp = temp = a[ctr] + a[ctr++];
    You do realize that ctr==ctr++?

  7. #7
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    4

    Default

    yes i do realize, and i apologize, I did that out of memory.

    it should be this

    Java Code:
    temp = a[ctr] + a[++ctr];
    and to prove it...

    Java Code:
    public class TestingGround {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		int CTR = 0;
    		int[] test = new int[6];
    		test[0]=5;
    		test[1]=2;
    		test[2]=4;
    		test[3]=6;
    		test[4]=8;
    		for(int x = 0; x <4; x++){
    		int temp = test[CTR] + test[++CTR]; 
    		System.out.println(temp);
    		}
    		
    	}
    
    }
    OUTPUT:
    Java Code:
    7
    6
    10
    14
    Last edited by Sno; 08-19-2010 at 09:44 PM.
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    add the element of index to the element of next index
    Given an "index" how to do get the "next index"? Add one to it.

  9. #9
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    Thank you guys, specially to "Sno".. I'll try that and show some improvement here. I 'am trying to create a triangle with numbers....
    Heart is the only broken thing that still works.

  10. #10
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    hmmm... sorry but I forgot to tell you guys that the value of array isn't fixed. I mean, it will depend on the input of the user.
    Heart is the only broken thing that still works.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    By value, do you mean the size of the array or the contents of the array's elements?
    Its normal for the content of variables to change depending on input. Either from a user or from a file or where ever. As you acquire the data, you assign it to the variable.

  12. #12
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    By value, do you mean the size of the array or the contents of the array's elements?
    Its normal for the content of variables to change depending on input. Either from a user or from a file or where ever. As you acquire the data, you assign it to the variable.

    the size of the array is only 5 and that is fixed.... but array's elements will depend on the input of the user. What Sno did is good but that is for fixed array's elements.

    What if I input: 5 4 5 3 2?

    that should give different output.

    the out put should be:

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


    Yeah, I used for loop on this ..

    for(int arr=0;arr<a.length;arr++)
    {
    temp+=" "+a[arr]; //will display the array's element
    that I Input ex. 5 4 5 3 2
    int storage=a[arr]+ a[arr]; //which like what I've said is
    wrong formula
    temp+=" "+storage;
    }
    JOptionPane.showMessageDialog(null,temp);
    ____________

    it doesn't give me the output I mentioned above.....


    I think I don't need to put here the complete codes, right? :))
    Heart is the only broken thing that still works.

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    You need to show your code, show its input and its output and describe what is wrong with the output.
    Or if you're having problems with one part of the code, post the code for that problem and describe what you are trying to do.

    int storage=a[arr]+ a[arr];
    Can you describe what this statement does?
    It adds together the contents of two variables and assigns the results to a third variable.
    What are the two variables? They are really only one variable: a[arr] the element at index arr in array a.

  14. #14
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    You need to show your code, show its input and its output and describe what is wrong with the output.
    Or if you're having problems with one part of the code, post the code for that problem and describe what you are trying to do.


    Can you describe what this statement does?
    It adds together the contents of two variables and assigns the results to a third variable.
    What are the two variables? They are really only one variable: a[arr] the element at index arr in array a.

    __________________________________________________ ___________


    import javax.swing.*;
    public class testing
    {
    static int [] val=new int [5]; //length of array
    public static void main(String args [])
    {
    for(int arr=0;arr<val.length;arr++)
    val[arr] = Integer.parseInt(JOptionPane.showInputDialog("Ente r value")); // ask to input value
    compare(val);
    }
    public static void compare(int a[]) //this is my own function
    {
    int arr;
    String temp="";
    for(arr=0;arr<a.length;arr++)
    {
    temp+=" "+a[arr]; //to display the array's element that I input for ex. 5,4,5,3,2
    int storage=a[arr]+ a[arr]; //I want to add first array's element to the next array's element
    temp+=" "+storage;
    }
    JOptionPane.showMessageDialog(null,temp);
    }


    }//class


    __________________________________________________ _______


    output:

    5 10 4 8 5 10 3 6 2 4



    haha! which is wrong....

    wrong formula: storage=a[arr]+ a[arr]; // bc it adds array's elements to itself instead to the next array's elements.

    what I'm trying to do is.... 5+4, 4+5, 5+3, 3+2


    wrong display: 5 10 4 8 5 10 3 6 2 4

    what I want to display is.....

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


    hmmm.... pardon me for being so stupid :) I admit I'm really not good on this but I need to solve this by hook or by crook ;) I wish you could help me :)
    Heart is the only broken thing that still works.

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    Please edit your code and surround it with code tags to make it readable.
    [Code ]
    ... code ...
    [/code ]
    without the spaces before the ]

    what I'm trying to do is.... 5+4, 4+5, 5+3, 3+2
    At what index in the array is the 5?
    and what is the index for the 4?
    How do you get the value of an element in an array? ArrayName[index] gives the value of the element at index index
    Last edited by Norm; 08-21-2010 at 12:14 AM.

  16. #16
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Please edit your code and surround it with code tags to make it readable.
    [Code ]
    ... code ...
    [/code ]
    without the spaces before the ]


    At what index in the array is the 5?
    and what is the index for the 4?
    How do you get the value of an element in an array? ArrayName[index] gives the value of the element at index index

    Java Code:
    import javax.swing.*;
    public class testing
    {
    static int [] val=new int [5]; //length of array
    public static void main(String args [])
    {
    for(int arr=0;arr<val.length;arr++)
    val[arr] = Integer.parseInt(JOptionPane.showInputDialog("Ente r value")); // ask to input value
    compare(val);
    }
    public static void compare(int a[]) //this is my own function
    {
    int arr;
    String temp="";
    for(arr=0;arr<a.length;arr++)
    {
    temp+=" "+a[arr]; //to display the array's element that I input for ex. 5,4,5,3,2
    int storage=a[arr]+ a[arr]; //I want to add first array's element to the next array's element
    temp+=" "+storage;
    }
    JOptionPane.showMessageDialog(null,temp);
    }
    
    
    }//class


    but you can copy paste it on netbeans or whatever code editor you are using to make it more readable :)
    Heart is the only broken thing that still works.

  17. #17
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    At what index in the array is the 5?
    and what is the index for the 4?
    How do you get the value of an element in an array? ArrayName[index] gives the value of the element at index index
    index 0 =[5]
    index 1 =[4]
    index 2 =[5]
    index 3 =[3]
    index 4 =[2]

    if i call the variable a[ ] that will give me the value of an element, right?

    Please try to run the program....
    Heart is the only broken thing that still works.

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    int storage=a[arr]+ a[arr]; //I want to add first array's element to the next array's element
    This adds the contents of the array at index arr to the contents of the array at the index arr
    and stores the value in storage.
    Given the array as you posted it, if arr = 0 then you are adding 5 (at index=0) to 5 (at index=0) and should get 10.

    Earlier I suggested: Given an "index" how to do get the "next index"? Add one to it.

    So how would you "add first array's element to the next array's element"
    arr[0] + arr[1]; // add the array's first element to the array's next element

    If you replace the hardcoded numbers 0 and 1 with variables you get:
    arr[i] + arr[i+1]; // add the array's ith element to the array's next element

  19. #19
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    I don't know why.. but it doesn't give me any output when I add 1...
    Heart is the only broken thing that still works.

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    it doesn't give me any output
    This will give you some output so you can see what your program is doing.

    Add this to your code after the line where you change the value of temp:
    System.out.println("temp=" + temp); // show the current value of temp

    Also add this after the for loop where you fill the val array:
    System.out.println("val=" + java.util.Arrays.toString(val)); // show contents of array

Page 1 of 2 12 LastLast

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
  •