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

## 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.

Thanks a lot!

2. What have you attempted so far? maybe one of your attempts was close enough that we just need to tweak it a little.

3. Member
Join Date
Jul 2010
Posts
22
Rep Power
0
Originally Posted by Sno
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

4. Senior Member
Join Date
Aug 2010
Posts
127
Rep Power
0
So you want to loop through the array and then add each item to the next item?

5. Your code is confusing to me.

So yes in this statement
Java Code:
`temp = a[ctr] + a [ctr];`

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++];`

6. Senior Member
Join Date
Aug 2010
Posts
127
Rep Power
0
Originally Posted by Sno
Your code is confusing to me.

So yes in this statement
Java Code:
`temp = a[ctr] + a [ctr];`

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. 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 10:44 PM.

8. 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. Member
Join Date
Jul 2010
Posts
22
Rep Power
0
Thank you guys, specially to "Sno".. I'll try that and show some improvement here. I 'am trying to create a triangle with numbers....

10. Member
Join Date
Jul 2010
Posts
22
Rep Power
0
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.

11. 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. Member
Join Date
Jul 2010
Posts
22
Rep Power
0
Originally Posted by Norm
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? :))

13. 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. Member
Join Date
Jul 2010
Posts
22
Rep Power
0
Originally Posted by Norm
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 :)

[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 01:14 AM.

16. Member
Join Date
Jul 2010
Posts
22
Rep Power
0
Originally Posted by Norm
[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 :)

17. Member
Join Date
Jul 2010
Posts
22
Rep Power
0
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....

18. 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. Member
Join Date
Jul 2010
Posts
22
Rep Power
0
I don't know why.. but it doesn't give me any output when I add 1...

20. 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 Last

#### Posting Permissions

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