# array

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-19-2010, 08:24 PM
blindfolded916
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!
• 08-19-2010, 08:25 PM
Sno
What have you attempted so far? maybe one of your attempts was close enough that we just need to tweak it a little.
• 08-19-2010, 08:40 PM
blindfolded916
Quote:

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
• 08-19-2010, 08:46 PM
imorio
So you want to loop through the array and then add each item to the next item?
• 08-19-2010, 08:56 PM
Sno
Your code is confusing to me.

So yes in this statement
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
Code:

`temp = temp = a[ctr] + a[ctr++];`
• 08-19-2010, 09:32 PM
imorio
Quote:

Originally Posted by Sno
Your code is confusing to me.

So yes in this statement
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
Code:

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

You do realize that ctr==ctr++?
• 08-19-2010, 09:41 PM
Sno
yes i do realize, and i apologize, I did that out of memory.

it should be this

Code:

`temp = a[ctr] + a[++ctr];`
and to prove it...

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:
Code:

```7 6 10 14```
• 08-19-2010, 09:50 PM
Norm
Quote:

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.
• 08-20-2010, 09:34 PM
blindfolded916
Thank you guys, specially to "Sno".. I'll try that and show some improvement here. I 'am trying to create a triangle with numbers....
• 08-20-2010, 11:00 PM
blindfolded916
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.
• 08-20-2010, 11:08 PM
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.
• 08-20-2010, 11:25 PM
blindfolded916
Quote:

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? :))
• 08-20-2010, 11:46 PM
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.

Quote:

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.
• 08-21-2010, 12:08 AM
blindfolded916
Quote:

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 :)
• 08-21-2010, 12:10 AM
Norm
[Code ]
... code ...
[/code ]
without the spaces before the ]

Quote:

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
• 08-21-2010, 12:18 AM
blindfolded916
Quote:

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

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 :)
• 08-21-2010, 12:28 AM
blindfolded916
Quote:

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....
• 08-21-2010, 01:38 AM
Norm
Quote:

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
• 08-21-2010, 03:16 AM
blindfolded916
I don't know why.. but it doesn't give me any output when I add 1...
• 08-21-2010, 03:38 AM
Norm
Quote:

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
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last